Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

More tests passing and more code moved from Router to RouterRoute. Sw…

…itching RouterRoute to used named capture groups. This simplifies the route processing internals as there are fewer loops used to merge array sets together.
  • Loading branch information...
commit 33c67f7c47181870da9f7257d86268a8c1d5ef44 1 parent 4cb055f
@markstory markstory authored
Showing with 43 additions and 43 deletions.
  1. +29 −36 cake/libs/router.php
  2. +14 −7 cake/tests/cases/libs/router.test.php
View
65 cake/libs/router.php
@@ -423,34 +423,14 @@ function parse($url) {
$argOptions['greedy'] = $params['greedy'];
unset($params['greedy']);
}
- array_shift($r);
-
- foreach ($names as $name) {
- $out[$name] = null;
- }
- if (is_array($defaults)) {
- foreach ($defaults as $name => $value) {
- if (preg_match('#[a-zA-Z_\-]#i', $name)) {
- $out[$name] = $value;
- } else {
- $out['pass'][] = $value;
- }
- }
- }
-
- foreach ($r as $key => $found) {
- if (empty($found) && $found != 0) {
- continue;
- }
-
- if (isset($names[$key])) {
- $out[$names[$key]] = $self->stripEscape($found);
- } else {
- $argOptions['context'] = array('action' => $out['action'], 'controller' => $out['controller']);
- extract($self->getArgs($found, $argOptions));
- $out['pass'] = array_merge($out['pass'], $pass);
- $out['named'] = $named;
- }
+ $out = $r;
+
+ if (isset($out['_args_'])) {
+ $argOptions['context'] = array('action' => $out['action'], 'controller' => $out['controller']);
+ $parsedArgs = $self->getArgs($out['_args_'], $argOptions);
+ $out['pass'] = array_merge($out['pass'], $parsedArgs['pass']);
+ $out['named'] = $parsedArgs['named'];
+ unset($out['_args_']);
}
if (isset($params['pass'])) {
@@ -1281,27 +1261,27 @@ function _writeRoute($route, $default, $params) {
preg_match_all('#:([A-Za-z0-9_-]+[A-Z0-9a-z])#', $route, $namedElements);
foreach ($namedElements[1] as $i => $name) {
- $option = null;
if (isset($params[$name])) {
+ $option = null;
if ($name !== 'plugin' && array_key_exists($name, $default)) {
$option = '?';
}
$slashParam = '/\\' . $namedElements[0][$i];
if (strpos($parsed, $slashParam) !== false) {
- $replacements[] = '(?:/(' . $params[$name] . ')' . $option . ')' . $option;
+ $replacements[] = '(?:/(?P<' . $name . '>' . $params[$name] . ')' . $option . ')' . $option;
$search[] = $slashParam;
} else {
$search[] = '\\' . $namedElements[0][$i];
- $replacements[] = '(?:(' . $params[$name] . ')' . $option . ')' . $option;
+ $replacements[] = '(?:(?P<' . $name . '>' . $params[$name] . ')' . $option . ')' . $option;
}
} else {
- $replacements[] = '(?:([^/]+))?';
+ $replacements[] = '(?:(?P<' . $name . '>[^/]+))?';
$search[] = '\\' . $namedElements[0][$i];
}
$names[] = $name;
}
if (preg_match('#\/\*#', $route, $m)) {
- $parsed = preg_replace('#/\\\\\*#', '(?:/(.*))?', $parsed);
+ $parsed = preg_replace('#/\\\\\*#', '(?:/(?P<_args_>.*))?', $parsed);
}
$parsed = str_replace($search, $replacements, $parsed);
$this->_compiledRoute = '#^' . $parsed . '[/]*$#';
@@ -1321,11 +1301,11 @@ function parse($url) {
$this->compile();
}
- if (!preg_match($this->_compiledRoute, $url, $r)) {
+ if (!preg_match($this->_compiledRoute, $url, $route)) {
return false;
} else {
foreach ($this->defaults as $key => $val) {
- if ($key{0} === '[' && preg_match('/^\[(\w+)\]$/', $key, $header)) {
+ if ($key[0] === '[' && preg_match('/^\[(\w+)\]$/', $key, $header)) {
if (isset($this->__headerMap[$header[1]])) {
$header = $this->__headerMap[$header[1]];
} else {
@@ -1345,8 +1325,21 @@ function parse($url) {
}
}
}
+ array_shift($route);
+ $count = count($this->keys);
+ for ($i = 0; $i <= $count; $i++) {
+ unset($route[$i]);
+ }
+ $route['pass'] = $route['named'] = array();
+ $route += $this->defaults;
+ foreach ($route as $key => $value) {
+ if (is_integer($key)) {
+ $route['pass'][] = $value;
+ unset($route[$key]);
+ }
+ }
+ return $route;
}
- return $r;
}
/**
View
21 cake/tests/cases/libs/router.test.php
@@ -1649,7 +1649,7 @@ function testPagesUrlParsing() {
Router::connect('/pages/*/:event', array('controller' => 'pages', 'action' => 'display'), array('event' => '[a-z0-9_-]+'));
$result = Router::parse('/');
- $expected = array('pass'=>array('home'), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
+ $expected = array('pass' => array('home'), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
$this->assertEqual($result, $expected);
$result = Router::parse('/pages/home');
@@ -1951,7 +1951,7 @@ function testRouterConnectDefaults() {
}
}
-// SimpleTest::ignore('RouterTest');
+
/**
* Test case for RouterRoute
*
@@ -2103,7 +2103,7 @@ function testRouteCompilingWithParamPatterns() {
/**
* test more complex route compiling & parsing with mid route greedy stars
- * and //
+ * and optional routing parameters
*
* @return void
*/
@@ -2127,7 +2127,14 @@ function testComplexRouteCompilingAndParsing() {
$result = $route->compile();
$this->assertPattern($result, '/posts/08/01/2007/title-of-post');
$result = $route->parse('/posts/08/01/2007/title-of-post');
- $this->assertEqual(count($result), 5);
+
+ $this->assertEqual(count($result), 9);
+ $this->assertEqual($result['controller'], 'posts');
+ $this->assertEqual($result['action'], 'view');
+ $this->assertEqual($result['plugin'], null);
+ $this->assertEqual($result['year'], '2007');
+ $this->assertEqual($result['month'], '08');
+ $this->assertEqual($result['day'], '01');
$route =& new RouterRoute(
"/:extra/page/:slug/*",
@@ -2179,9 +2186,9 @@ function testParse() {
$route = new RouterRoute('/:controller/:action/:id', array('controller' => 'testing4', 'id' => null), array('id' => $ID));
$route->compile();
$result = $route->parse('/posts/view/1');
- $this->assertEqual($result[1], 'posts');
- $this->assertEqual($result[2], 'view');
- $this->assertEqual($result[3], '1');
+ $this->assertEqual($result['controller'], 'posts');
+ $this->assertEqual($result['action'], 'view');
+ $this->assertEqual($result['id'], '1');
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.