diff --git a/lib/Cake/Routing/Route/CakeRoute.php b/lib/Cake/Routing/Route/CakeRoute.php index 53fc5e354..4354dd8d5 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 Key/Value of object attributes + * @return CakeRoute A new instance of the route + */ + 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..6a05b378c 100644 --- a/lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php +++ b/lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php @@ -977,4 +977,34 @@ class CakeRouteTest extends CakeTestCase { $this->assertEquals($expected, $result); } +/** + * Test for __set_state magic method on CakeRoute + * + * @return void + */ + public function testSetState() { + $route = CakeRoute::__set_state(array( + 'keys' => array(), + 'options' => array(), + 'defaults' => array( + 'controller' => 'pages', + 'action' => 'display', + 'home', + ), + 'template' => '/', + '_greedy' => false, + '_compiledRoute' => null, + '_headerMap' => array ( + 'type' => 'content_type', + 'method' => 'request_method', + 'server' => 'server_name', + ), + )); + $this->assertInstanceOf('CakeRoute', $route); + $this->assertSame('/', $route->match(array('controller' => 'pages', 'action' => 'display', 'home'))); + $this->assertFalse($route->match(array('controller' => 'pages', 'action' => 'display', 'about'))); + $expected = array('controller' => 'pages', 'action' => 'display', 'pass' => array('home'), 'named' => array()); + $this->assertEquals($expected, $route->parse('/')); + } + }