Merge pull request #4953 from jrbasso/2.6-route-set-state

Implementing __set_state for CakeRoute
This commit is contained in:
José Lorenzo Rodríguez 2014-10-28 09:36:42 +01:00
commit 9d0390ca63
2 changed files with 48 additions and 0 deletions

View file

@ -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;
}
}

View file

@ -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('/'));
}
}