From c0ac61117e041b3d832454d38e5ddf66e9f33065 Mon Sep 17 00:00:00 2001 From: mark_story Date: Mon, 10 Mar 2014 21:42:26 -0400 Subject: [PATCH] Only sort the keys once per request instead of on each match. Sorting the keys property by value sorts keys with the same prefix for free. This does change the order of the keys, but I don't think that is actually a large issue as it is just a list. Refs #2991 --- lib/Cake/Routing/Route/CakeRoute.php | 16 ++++++---------- .../Test/Case/Routing/Route/CakeRouteTest.php | 12 ++++++------ 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/lib/Cake/Routing/Route/CakeRoute.php b/lib/Cake/Routing/Route/CakeRoute.php index b40529c9c9c..74595c40092 100644 --- a/lib/Cake/Routing/Route/CakeRoute.php +++ b/lib/Cake/Routing/Route/CakeRoute.php @@ -173,6 +173,10 @@ protected function _writeRoute() { foreach ($this->keys as $key) { unset($this->defaults[$key]); } + + $keys = $this->keys; + sort($keys); + $this->keys = array_reverse($keys); } /** @@ -420,7 +424,6 @@ public function match($url) { $named = $pass = array(); foreach ($url as $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) { @@ -517,16 +520,10 @@ protected function _writeUrl($params) { } $out = $this->template; - if ($this->keys !== array()) { - + if (!empty($this->keys)) { $search = $replace = array(); - $lengths = array_map('strlen', $this->keys); - $flipped = array_combine($this->keys, $lengths); - arsort($flipped); - $keys = array_keys($flipped); - - foreach ($keys as $key) { + foreach ($this->keys as $key) { $string = null; if (isset($params[$key])) { $string = $params[$key]; @@ -537,7 +534,6 @@ protected function _writeUrl($params) { $replace[] = $string; } $out = str_replace($search, $replace, $out); - } if (strpos($this->template, '*')) { diff --git a/lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php b/lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php index 16bed4cd6f5..375cf4d5969 100644 --- a/lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php +++ b/lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php @@ -118,7 +118,7 @@ public function testRouteCompilingWithParamPatterns() { $this->assertRegExp($result, '/posts/view/518098'); $this->assertNotRegExp($result, '/posts/edit/name-of-post'); $this->assertNotRegExp($result, '/posts/edit/4/other:param'); - $this->assertEquals(array('controller', 'action', 'id'), $route->keys); + $this->assertEquals(array('id', 'controller', 'action'), $route->keys); $route = new CakeRoute( '/:lang/:controller/:action/:id', @@ -130,7 +130,7 @@ public function testRouteCompilingWithParamPatterns() { $this->assertRegExp($result, '/cze/articles/view/1'); $this->assertNotRegExp($result, '/language/articles/view/2'); $this->assertNotRegExp($result, '/eng/articles/view/name-of-article'); - $this->assertEquals(array('lang', 'controller', 'action', 'id'), $route->keys); + $this->assertEquals(array('lang', 'id', 'controller', 'action'), $route->keys); foreach (array(':', '@', ';', '$', '-') as $delim) { $route = new CakeRoute('/posts/:id' . $delim . ':title'); @@ -141,7 +141,7 @@ public function testRouteCompilingWithParamPatterns() { $this->assertNotRegExp($result, '/posts/11!nameofarticle'); $this->assertNotRegExp($result, '/posts/11'); - $this->assertEquals(array('id', 'title'), $route->keys); + $this->assertEquals(array('title', 'id'), $route->keys); } $route = new CakeRoute( @@ -155,7 +155,7 @@ public function testRouteCompilingWithParamPatterns() { $this->assertNotRegExp($result, '/posts/hey_now:nameofarticle'); $this->assertNotRegExp($result, '/posts/:nameofarticle/2009'); $this->assertNotRegExp($result, '/posts/:nameofarticle/01'); - $this->assertEquals(array('id', 'title', 'year'), $route->keys); + $this->assertEquals(array('year', 'title', 'id'), $route->keys); $route = new CakeRoute( '/posts/:url_title-(uuid::id)', @@ -204,7 +204,7 @@ public function testComplexRouteCompilingAndParsing() { $this->assertRegExp($result, '/some_extra/page/this_is_the_slug'); $this->assertRegExp($result, '/page/this_is_the_slug'); - $this->assertEquals(array('extra', 'slug'), $route->keys); + $this->assertEquals(array('slug', 'extra'), $route->keys); $this->assertEquals(array('extra' => '[a-z1-9_]*', 'slug' => '[a-z1-9_]+', 'action' => 'view'), $route->options); $expected = array( 'controller' => 'pages', @@ -864,8 +864,8 @@ public function testMatchSimilarParameters() { $route = new CakeRoute('/:thisParam/:thisParamIsLonger'); $url = array( + 'thisParamIsLonger' => 'bar', 'thisParam' => 'foo', - 'thisParamIsLonger' => 'bar' ); $result = $route->match($url);