Removing named parameter sigils.

This commit is contained in:
mark_story 2010-12-19 22:17:57 -05:00
parent d3fc29c8e8
commit b49b49a5ef
3 changed files with 33 additions and 25 deletions

View file

@ -284,27 +284,19 @@ class CakeRoute {
return false; return false;
} }
$greedyNamed = Router::$named['greedy'];
$allowedNamedParams = Router::$named['rules'];
$named = $pass = $_query = array(); $named = $pass = $_query = array();
foreach ($url as $key => $value) { foreach ($url as $key => $value) {
// pull out named params so comparisons later on are faster.
if ($key[0] === CakeRoute::SIGIL_NAMED && ($value !== false && $value !== null)) {
$named[substr($key, 1)] = $value;
unset($url[$key]);
continue;
}
// pull out querystring params
if ($key[0] === CakeRoute::SIGIL_QUERYSTRING && ($value !== false && $value !== null)) {
$_query[substr($key, 1)] = $value;
unset($url[$key]);
continue;
}
// keys that exist in the defaults and have different values cause match failures. // keys that exist in the defaults and have different values is a match failure.
$keyExists = array_key_exists($key, $defaults); $defaultExists = array_key_exists($key, $defaults);
if ($keyExists && $defaults[$key] != $value) { if ($defaultExists && $defaults[$key] != $value) {
return false; return false;
} elseif ($defaultExists) {
continue;
} }
// If the key is a routed key, its not different yet. // If the key is a routed key, its not different yet.
@ -322,8 +314,24 @@ class CakeRoute {
continue; continue;
} }
// pull out querystring params
if ($key[0] === CakeRoute::SIGIL_QUERYSTRING && ($value !== false && $value !== null)) {
$_query[substr($key, 1)] = $value;
unset($url[$key]);
continue;
}
// pull out named params if named params are greedy or a rule exists.
if (
($greedyNamed || isset($allowedNamedParams[$key])) &&
($value !== false && $value !== null)
) {
$named[$key] = $value;
continue;
}
// keys that don't exist are different. // keys that don't exist are different.
if (!$keyExists && !empty($value)) { if (!$defaultExists && !empty($value)) {
return false; return false;
} }
} }

View file

@ -914,10 +914,10 @@ class Router {
$key = $keys[$i]; $key = $keys[$i];
if (is_numeric($keys[$i])) { if (is_numeric($keys[$i])) {
$args[] = $url[$key]; $args[] = $url[$key];
} elseif ($key[0] === CakeRoute::SIGIL_NAMED) {
$named[substr($key, 1)] = $url[$key];
} elseif ($key[0] === CakeRoute::SIGIL_QUERYSTRING) { } elseif ($key[0] === CakeRoute::SIGIL_QUERYSTRING) {
$query[substr($key, 1)] = $url[$key]; $query[substr($key, 1)] = $url[$key];
} else {
$named[$key] = $url[$key];
} }
} }

View file

@ -310,7 +310,7 @@ class CakeRouteTestCase extends CakeTestCase {
*/ */
function testGreedyRouteFailureNamedParam() { function testGreedyRouteFailureNamedParam() {
$route = new CakeRoute('/:controller/:action', array('plugin' => null)); $route = new CakeRoute('/:controller/:action', array('plugin' => null));
$result = $route->match(array('controller' => 'posts', 'action' => 'view', ':page' => 1)); $result = $route->match(array('controller' => 'posts', 'action' => 'view', 'page' => 1));
$this->assertFalse($result); $this->assertFalse($result);
} }
@ -334,9 +334,9 @@ class CakeRouteTestCase extends CakeTestCase {
*/ */
function testMatchWithNamedParametersAndPassedArgs() { function testMatchWithNamedParametersAndPassedArgs() {
Router::connectNamed(true); Router::connectNamed(true);
/*
$route = new CakeRoute('/:controller/:action/*', array('plugin' => null)); $route = new CakeRoute('/:controller/:action/*', array('plugin' => null));
$result = $route->match(array('controller' => 'posts', 'action' => 'index', 'plugin' => null, ':page' => 1)); $result = $route->match(array('controller' => 'posts', 'action' => 'index', 'plugin' => null, 'page' => 1));
$this->assertEqual($result, '/posts/index/page:1'); $this->assertEqual($result, '/posts/index/page:1');
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 5)); $result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 5));
@ -348,9 +348,9 @@ class CakeRouteTestCase extends CakeTestCase {
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, '0')); $result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, '0'));
$this->assertEqual($result, '/posts/view/0'); $this->assertEqual($result, '/posts/view/0');
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 5, ':page' => 1, ':limit' => 20, ':order' => 'title')); $result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 5, 'page' => 1, 'limit' => 20, 'order' => 'title'));
$this->assertEqual($result, '/posts/view/5/page:1/limit:20/order:title'); $this->assertEqual($result, '/posts/view/5/page:1/limit:20/order:title');
*/
$route = new CakeRoute('/test2/*', array('controller' => 'pages', 'action' => 'display', 2)); $route = new CakeRoute('/test2/*', array('controller' => 'pages', 'action' => 'display', 2));
$result = $route->match(array('controller' => 'pages', 'action' => 'display', 1)); $result = $route->match(array('controller' => 'pages', 'action' => 'display', 1));
@ -370,7 +370,7 @@ class CakeRouteTestCase extends CakeTestCase {
*/ */
function testNamedParamsWithNullFalse() { function testNamedParamsWithNullFalse() {
$route = new CakeRoute('/:controller/:action/*'); $route = new CakeRoute('/:controller/:action/*');
$result = $route->match(array('controller' => 'posts', 'action' => 'index', ':page' => null, 'sort' => false)); $result = $route->match(array('controller' => 'posts', 'action' => 'index', 'page' => null, 'sort' => false));
$this->assertEquals('/posts/index/', $result); $this->assertEquals('/posts/index/', $result);
} }