Permalink
Browse files

Continuing to refactor Router methods into RouterRoute.

Fixed issues with some routes having trailing / and others not.
All existing router tests are passing.
  • Loading branch information...
1 parent cc5c5a5 commit aeb61f3750742a10e2fcea30c50e802ae6599108 @markstory markstory committed Oct 2, 2009
Showing with 44 additions and 31 deletions.
  1. +28 −25 cake/libs/router.php
  2. +16 −6 cake/tests/cases/libs/router.test.php
View
@@ -844,7 +844,6 @@ function url($url = null, $full = false) {
if (isset($route->params['persist'], $_this->__params[0])) {
$url = array_merge(array_intersect_key($params, Set::combine($route->params['persist'], '/')), $url);
}
- // replace with RouterRoute::match
if ($match = $route->match($url)) {
$output = trim($match, '/');
$url = array();
@@ -896,7 +895,7 @@ function url($url = null, $full = false) {
break;
}
}
- $output = join('/', $urlOut) . '/';
+ $output = join('/', $urlOut);
}
if (!empty($args)) {
@@ -1454,22 +1453,27 @@ function match($url) {
if (!$this->compiled()) {
$this->compile();
}
- if (isset($this->defaults['prefix'])) {
- $prefix = $this->defaults['prefix'];
+ $url += array('controller' => null, 'plugin' => null);
+ $defaults = $this->defaults;
+ $routeParams = $this->keys;
+ $routeOptions = $this->params;
+
+ if (isset($defaults['prefix'])) {
+ $prefix = $defaults['prefix'];
+ unset($defaults['prefix']);
}
- $url += array('plugin' => null, 'controller' => null);
$pass = array();
- $params = Set::diff($url, $this->defaults);
+ $params = Set::diff($url, $defaults);
$urlInv = array_combine(array_values($url), array_keys($url));
$i = 0;
- while (isset($this->defaults[$i])) {
- if (isset($urlInv[$this->defaults[$i]])) {
- if (!in_array($this->defaults[$i], $url) && is_int($urlInv[$this->defaults[$i]])) {
+ while (isset($defaults[$i])) {
+ if (isset($urlInv[$defaults[$i]])) {
+ if (!in_array($defaults[$i], $url) && is_int($urlInv[$defaults[$i]])) {
return false;
}
- unset($urlInv[$this->defaults[$i]], $this->defaults[$i]);
+ unset($urlInv[$defaults[$i]], $defaults[$i]);
} else {
return false;
}
@@ -1490,56 +1494,55 @@ function match($url) {
$urlKeys = array_keys($url);
$paramsKeys = array_keys($params);
- $defaultsKeys = array_keys($this->defaults);
+ $defaultsKeys = array_keys($defaults);
if (!empty($params)) {
- if (array_diff($paramsKeys, $this->keys) != array()) {
+ if (array_diff($paramsKeys, $routeParams) != array()) {
return false;
}
-
- $required = array_values(array_diff($this->keys, $urlKeys));
+ $required = array_values(array_diff($routeParams, $urlKeys));
$reqCount = count($required);
for ($i = 0; $i < $reqCount; $i++) {
- if (array_key_exists($required[$i], $this->defaults) && $this->defaults[$required[$i]] === null) {
+ if (array_key_exists($required[$i], $defaults) && $defaults[$required[$i]] === null) {
unset($required[$i]);
}
}
}
$isFilled = true;
- if (!empty($this->keys)) {
- $filled = array_intersect_key($url, array_combine($this->keys, array_keys($this->keys)));
- $isFilled = (array_diff($this->keys, array_keys($filled)) === array());
+ if (!empty($routeParams)) {
+ $filled = array_intersect_key($url, array_combine($routeParams, array_keys($routeParams)));
+ $isFilled = (array_diff($routeParams, array_keys($filled)) === array());
if (!$isFilled && empty($params)) {
return false;
}
}
if (empty($params)) {
return $this->__mapRoute(array_merge($url, compact('pass', 'named', 'prefix')));
- } elseif (!empty($this->keys) && !empty($this->defaults)) {
+ } elseif (!empty($routeParams) && !empty($defaults)) {
if (!empty($required)) {
return false;
}
foreach ($params as $key => $val) {
- if ((!isset($url[$key]) || $url[$key] != $val) || (!isset($this->defaults[$key]) || $this->defaults[$key] != $val) && !in_array($key, $this->keys)) {
- if (!isset($this->defaults[$key])) {
+ if ((!isset($url[$key]) || $url[$key] != $val) || (!isset($defaults[$key]) || $defaults[$key] != $val) && !in_array($key, $routeParams)) {
+ if (!isset($defaults[$key])) {
continue;
}
return false;
}
}
} else {
- if (empty($required) && $this->defaults['plugin'] === $url['plugin'] && $this->defaults['controller'] === $url['controller'] && $this->defaults['action'] === $url['action']) {
- return $this->__mapRoute($route, array_merge($url, compact('pass', 'named', 'prefix')));
+ if (empty($required) && $defaults['plugin'] === $url['plugin'] && $defaults['controller'] === $url['controller'] && $defaults['action'] === $url['action']) {
+ return $this->__mapRoute(array_merge($url, compact('pass', 'named', 'prefix')));
}
return false;
}
- if (!empty($this->params)) {
- foreach ($this->params as $key => $reg) {
+ if (!empty($routeOptions)) {
+ foreach ($routeOptions as $key => $reg) {
if (array_key_exists($key, $url) && !preg_match('#' . $reg . '#', $url[$key])) {
return false;
}
@@ -636,7 +636,7 @@ function testUrlGeneration() {
));
$result = Router::url(array('plugin' => null, 'controller' => 'myothercontroller'));
- $expected = '/myothercontroller/';
+ $expected = '/myothercontroller';
$this->assertEqual($result, $expected);
Configure::write('Routing.admin', 'admin');
@@ -1582,11 +1582,11 @@ function testRemoveBase() {
));
$result = Router::url(array('controller' => 'my_controller', 'action' => 'my_action'));
- $expected = '/base/my_controller/my_action/';
+ $expected = '/base/my_controller/my_action';
$this->assertEqual($result, $expected);
$result = Router::url(array('controller' => 'my_controller', 'action' => 'my_action', 'base' => false));
- $expected = '/my_controller/my_action/';
+ $expected = '/my_controller/my_action';
$this->assertEqual($result, $expected);
$result = Router::url(array('controller' => 'my_controller', 'action' => 'my_action', 'base' => true));
@@ -1789,11 +1789,11 @@ function testPassedArgsOrder() {
$expected = '/test2/whatever';
$this->assertEqual($result, $expected);
- Configure::write('Routing.admin', 'admin');
+ Configure::write('Routing.prefixes', array('admin'));
Router::reload();
Router::setRequestInfo(array(
- array('plugin' => null, 'controller' => 'images', 'action' => 'index', 'pass' => array(), 'named' => array(), 'prefix' => 'protected', 'admin' => false, 'form' => array(), 'url' => array ('url' => 'protected/images/index')),
+ array('plugin' => null, 'controller' => 'images', 'action' => 'index', 'pass' => array(), 'named' => array(), 'prefix' => 'protected', 'protected' => true, 'form' => array(), 'url' => array ('url' => 'protected/images/index')),
array('plugin' => null, 'controller' => null, 'action' => null, 'base' => '', 'here' => '/protected/images/index', 'webroot' => '/')
));
@@ -1843,7 +1843,7 @@ function testRegexRouteMatching() {
));
$result = Router::url(array('action' => 'test_another_action'));
- $expected = '/test/test_another_action/';
+ $expected = '/test/test_another_action';
$this->assertEqual($result, $expected);
$result = Router::url(array('action' => 'test_another_action', 'locale' => 'eng'));
@@ -2040,8 +2040,18 @@ function testMatching() {
$route =& new RouterRoute('/blog/:action', array('controller' => 'posts'));
$result = $route->match(array('controller' => 'posts', 'action' => 'view'));
$this->assertEqual($result, '/blog/view');
+
+ $route =& new RouterRoute('/admin/subscriptions/:action/*', array(
+ 'controller' => 'subscribe', 'admin' => true, 'prefix' => 'admin'
+ ));
+
+ $url = array('plugin' => null, 'controller' => 'subscribe', 'admin' => true, 'action' => 'edit', 1);
+ $result = $route->match($url);
+ $expected = '/admin/subscriptions/edit/1/';
+ $this->assertEqual($result, $expected);
}
}
+
?>

0 comments on commit aeb61f3

Please sign in to comment.