From b49b49a5ef4656f23d51bcfcf8553338a50df9bb Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 19 Dec 2010 22:17:57 -0500 Subject: [PATCH] Removing named parameter sigils. --- cake/libs/route/cake_route.php | 42 +++++++++++-------- cake/libs/router.php | 4 +- .../cases/libs/route/cake_route.test.php | 12 +++--- 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/cake/libs/route/cake_route.php b/cake/libs/route/cake_route.php index 7e4af3384..364b18641 100644 --- a/cake/libs/route/cake_route.php +++ b/cake/libs/route/cake_route.php @@ -284,27 +284,19 @@ class CakeRoute { return false; } + $greedyNamed = Router::$named['greedy']; + $allowedNamedParams = Router::$named['rules']; + $named = $pass = $_query = array(); 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. - $keyExists = array_key_exists($key, $defaults); - if ($keyExists && $defaults[$key] != $value) { + // keys that exist in the defaults and have different values is a match failure. + $defaultExists = array_key_exists($key, $defaults); + if ($defaultExists && $defaults[$key] != $value) { return false; + } elseif ($defaultExists) { + continue; } // If the key is a routed key, its not different yet. @@ -322,8 +314,24 @@ class CakeRoute { 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. - if (!$keyExists && !empty($value)) { + if (!$defaultExists && !empty($value)) { return false; } } diff --git a/cake/libs/router.php b/cake/libs/router.php index 29e38dc3a..76e801aec 100644 --- a/cake/libs/router.php +++ b/cake/libs/router.php @@ -914,10 +914,10 @@ class Router { $key = $keys[$i]; if (is_numeric($keys[$i])) { $args[] = $url[$key]; - } elseif ($key[0] === CakeRoute::SIGIL_NAMED) { - $named[substr($key, 1)] = $url[$key]; } elseif ($key[0] === CakeRoute::SIGIL_QUERYSTRING) { $query[substr($key, 1)] = $url[$key]; + } else { + $named[$key] = $url[$key]; } } diff --git a/cake/tests/cases/libs/route/cake_route.test.php b/cake/tests/cases/libs/route/cake_route.test.php index 3107e0084..0831ab814 100644 --- a/cake/tests/cases/libs/route/cake_route.test.php +++ b/cake/tests/cases/libs/route/cake_route.test.php @@ -310,7 +310,7 @@ class CakeRouteTestCase extends CakeTestCase { */ function testGreedyRouteFailureNamedParam() { $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); } @@ -334,9 +334,9 @@ class CakeRouteTestCase extends CakeTestCase { */ function testMatchWithNamedParametersAndPassedArgs() { Router::connectNamed(true); - +/* $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'); $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')); $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'); - +*/ $route = new CakeRoute('/test2/*', array('controller' => 'pages', 'action' => 'display', 2)); $result = $route->match(array('controller' => 'pages', 'action' => 'display', 1)); @@ -370,7 +370,7 @@ class CakeRouteTestCase extends CakeTestCase { */ function testNamedParamsWithNullFalse() { $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); }