mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Merge pull request #4953 from jrbasso/2.6-route-set-state
Implementing __set_state for CakeRoute
This commit is contained in:
commit
9d0390ca63
2 changed files with 48 additions and 0 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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('/'));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue