Making route matching fail even faster. This gives significant performance boosts to routes not matching.

This commit is contained in:
mark_story 2010-12-18 14:36:11 -05:00
parent 756b09849f
commit 3b0a3d4109
2 changed files with 6 additions and 4 deletions

View file

@ -265,7 +265,7 @@ class CakeRoute {
//check that all the key names are in the url //check that all the key names are in the url
$keyNames = array_flip($this->keys); $keyNames = array_flip($this->keys);
if (array_intersect_key($keyNames, $url) != $keyNames) { if (array_intersect_key($keyNames, $url) !== $keyNames) {
return false; return false;
} }
@ -287,6 +287,7 @@ class CakeRoute {
// keys that exist in the defaults and have different values cause match failures. // keys that exist in the defaults and have different values cause match failures.
$keyExists = array_key_exists($key, $defaults); $keyExists = array_key_exists($key, $defaults);
if ($keyExists && $defaults[$key] != $value) { if ($keyExists && $defaults[$key] != $value) {
return false;
$diff[$key] = $value; $diff[$key] = $value;
continue; continue;
} }
@ -308,6 +309,7 @@ class CakeRoute {
// keys that don't exist are different. // keys that don't exist are different.
if (!$keyExists && !empty($value)) { if (!$keyExists && !empty($value)) {
return false;
$diff[$key] = $value; $diff[$key] = $value;
} }
} }

View file

@ -219,7 +219,7 @@ class CakeRouteTestCase extends CakeTestCase {
* @return void * @return void
**/ **/
function testMatchBasic() { function testMatchBasic() {
/* $route = new CakeRoute('/:controller/:action/:id', array('plugin' => null)); $route = new CakeRoute('/:controller/:action/:id', array('plugin' => null));
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null)); $result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null));
$this->assertFalse($result); $this->assertFalse($result);
@ -235,7 +235,7 @@ class CakeRouteTestCase extends CakeTestCase {
$result = $route->match(array('controller' => 'pages', 'action' => 'display', 'about')); $result = $route->match(array('controller' => 'pages', 'action' => 'display', 'about'));
$this->assertFalse($result); $this->assertFalse($result);
*/
$route = new CakeRoute('/pages/*', array('controller' => 'pages', 'action' => 'display')); $route = new CakeRoute('/pages/*', array('controller' => 'pages', 'action' => 'display'));
$result = $route->match(array('controller' => 'pages', 'action' => 'display', 'home')); $result = $route->match(array('controller' => 'pages', 'action' => 'display', 'home'));
@ -369,7 +369,7 @@ class CakeRouteTestCase extends CakeTestCase {
* @return void * @return void
*/ */
function testNamedParamsWithNullFalse() { function testNamedParamsWithNullFalse() {
$route = new CakeRoute('/:controller/:action/*', array('plugin' => null)); $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);
} }