From b2a92f9cd87cea92f5b199fc2da672800a43835b Mon Sep 17 00:00:00 2001 From: Juan Basso Date: Tue, 21 Oct 2014 23:04:08 -0400 Subject: [PATCH] Implementing __set_state for CakeRoute It helps the applications to cache the routes using var_export when possible. --- lib/Cake/Routing/Route/CakeRoute.php | 18 ++++++++++++++++++ .../Test/Case/Routing/Route/CakeRouteTest.php | 11 +++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/Cake/Routing/Route/CakeRoute.php b/lib/Cake/Routing/Route/CakeRoute.php index 53fc5e354..e845d470d 100644 --- a/lib/Cake/Routing/Route/CakeRoute.php +++ b/lib/Cake/Routing/Route/CakeRoute.php @@ -546,4 +546,22 @@ class CakeRoute { return $out; } + /** + * Set state magic method to support var_export + * + * This method helps for applications that want to implement + * router caching. + * + * @param array $fields + * @return CakeRoute + */ + public static function __set_state($fields) { + $class = function_exists('get_called_class') ? get_called_class() : __CLASS__; + $obj = new $class(''); + foreach ($fields as $field => $value) { + $obj->$field = $value; + } + return $obj; + } + } diff --git a/lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php b/lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php index 46c293d95..d07aca907 100644 --- a/lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php +++ b/lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php @@ -977,4 +977,15 @@ class CakeRouteTest extends CakeTestCase { $this->assertEquals($expected, $result); } +/** + * Test for var_export on CakeRoute + * + * @return void + */ + public function testSetState() { + $route = new CakeRoute('/', array('controller' => 'pages', 'action' => 'display', 'home')); + $retrievedRoute = eval('return ' . var_export($route, true) . ';'); + $this->assertEquals($route, $retrievedRoute); + } + }