Skip to content

Commit

Permalink
Implementing __set_state for CakeRoute
Browse files Browse the repository at this point in the history
It helps the applications to cache the routes using var_export when possible.
  • Loading branch information
jrbasso committed Oct 22, 2014
1 parent 7930e68 commit b2a92f9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/Cake/Routing/Route/CakeRoute.php
Expand Up @@ -546,4 +546,22 @@ protected function _writeUrl($params) {
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;
}

}
11 changes: 11 additions & 0 deletions lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php
Expand Up @@ -977,4 +977,15 @@ public function testUTF8PatternOnSection() {
$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);
}

}

0 comments on commit b2a92f9

Please sign in to comment.