mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
Making greedy -> greedyNamed its clearer and doesn't consume a possibly easy to use named parameter key.
Expanding tests for named parameter rules/conditions.
This commit is contained in:
parent
3eef281e1c
commit
97175aac90
3 changed files with 50 additions and 6 deletions
|
@ -252,10 +252,10 @@ class CakeRoute {
|
||||||
|
|
||||||
$context = compact('controller', 'action');
|
$context = compact('controller', 'action');
|
||||||
$namedConfig = Router::namedConfig();
|
$namedConfig = Router::namedConfig();
|
||||||
$greedy = isset($this->options['greedy']) ? $this->options['greedy'] : $namedConfig['greedy'];
|
$greedy = isset($this->options['greedyNamed']) ? $this->options['greedyNamed'] : $namedConfig['greedy'];
|
||||||
$rules = $namedConfig['rules'];
|
$rules = $namedConfig['rules'];
|
||||||
if (isset($this->options['named'])) {
|
if (isset($this->options['named'])) {
|
||||||
$greedy = isset($this->options['greedy']) && $this->options['greedy'] === true;
|
$greedy = isset($this->options['greedyNamed']) && $this->options['greedyNamed'] === true;
|
||||||
foreach ((array)$this->options['named'] as $key => $val) {
|
foreach ((array)$this->options['named'] as $key => $val) {
|
||||||
if (is_numeric($key)) {
|
if (is_numeric($key)) {
|
||||||
$rules[$val] = true;
|
$rules[$val] = true;
|
||||||
|
|
|
@ -395,7 +395,7 @@ class Router {
|
||||||
self::$_namedConfig['rules'][$key] = $val;
|
self::$_namedConfig['rules'][$key] = $val;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self::$_namedConfig['greedy'] = $options['greedy'];
|
self::$_namedConfig['greedyNamed'] = $options['greedy'];
|
||||||
return self::$_namedConfig;
|
return self::$_namedConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -514,14 +514,15 @@ class CakeRouteTestCase extends CakeTestCase {
|
||||||
'named' => array(
|
'named' => array(
|
||||||
'wibble',
|
'wibble',
|
||||||
'fish' => array('action' => 'index'),
|
'fish' => array('action' => 'index'),
|
||||||
'fizz' => array('controller' => 'comments')
|
'fizz' => array('controller' => 'comments'),
|
||||||
|
'pattern' => 'val-[\d]+'
|
||||||
)
|
)
|
||||||
));
|
));
|
||||||
$result = $route->parse('/posts/display/wibble:spin/fish:trout/fizz:buzz');
|
$result = $route->parse('/posts/display/wibble:spin/fish:trout/fizz:buzz/unknown:value');
|
||||||
$expected = array(
|
$expected = array(
|
||||||
'controller' => 'posts',
|
'controller' => 'posts',
|
||||||
'action' => 'display',
|
'action' => 'display',
|
||||||
'pass' => array('fish:trout', 'fizz:buzz'),
|
'pass' => array('fish:trout', 'fizz:buzz', 'unknown:value'),
|
||||||
'named' => array(
|
'named' => array(
|
||||||
'wibble' => 'spin'
|
'wibble' => 'spin'
|
||||||
)
|
)
|
||||||
|
@ -552,6 +553,49 @@ class CakeRouteTestCase extends CakeTestCase {
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$this->assertEquals($expected, $result, 'All params should be parsed as conditions were met.');
|
$this->assertEquals($expected, $result, 'All params should be parsed as conditions were met.');
|
||||||
|
|
||||||
|
$result = $route->parse('/comments/index/pattern:val--');
|
||||||
|
$expected = array(
|
||||||
|
'controller' => 'comments',
|
||||||
|
'action' => 'index',
|
||||||
|
'pass' => array('pattern:val--'),
|
||||||
|
'named' => array()
|
||||||
|
);
|
||||||
|
$this->assertEquals($expected, $result, 'Named parameter pattern unmet.');
|
||||||
|
|
||||||
|
$result = $route->parse('/comments/index/pattern:val-2');
|
||||||
|
$expected = array(
|
||||||
|
'controller' => 'comments',
|
||||||
|
'action' => 'index',
|
||||||
|
'pass' => array(),
|
||||||
|
'named' => array('pattern' => 'val-2')
|
||||||
|
);
|
||||||
|
$this->assertEquals($expected, $result, 'Named parameter pattern met.');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test that greedyNamed ignores rules.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function testParseGreedyNamed() {
|
||||||
|
$route = new CakeRoute('/:controller/:action/*', array(), array(
|
||||||
|
'named' => array(
|
||||||
|
'fizz' => array('controller' => 'comments'),
|
||||||
|
'pattern' => 'val-[\d]+',
|
||||||
|
),
|
||||||
|
'greedyNamed' => true
|
||||||
|
));
|
||||||
|
$result = $route->parse('/posts/display/wibble:spin/fizz:buzz/pattern:ignored');
|
||||||
|
$expected = array(
|
||||||
|
'controller' => 'posts',
|
||||||
|
'action' => 'display',
|
||||||
|
'pass' => array('fizz:buzz', 'pattern:ignored'),
|
||||||
|
'named' => array(
|
||||||
|
'wibble' => 'spin',
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$this->assertEquals($expected, $result, 'Greedy named grabs everything, rules are followed');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue