Continuing to refactor Router methods into RouterRoute.

Fixed issues with some routes having trailing / and others not.
All existing router tests are passing.
This commit is contained in:
mark_story 2009-10-02 14:00:33 -04:00
parent cc5c5a5ac9
commit aeb61f3750
2 changed files with 44 additions and 31 deletions

View file

@ -844,7 +844,6 @@ class Router {
if (isset($route->params['persist'], $_this->__params[0])) { if (isset($route->params['persist'], $_this->__params[0])) {
$url = array_merge(array_intersect_key($params, Set::combine($route->params['persist'], '/')), $url); $url = array_merge(array_intersect_key($params, Set::combine($route->params['persist'], '/')), $url);
} }
// replace with RouterRoute::match
if ($match = $route->match($url)) { if ($match = $route->match($url)) {
$output = trim($match, '/'); $output = trim($match, '/');
$url = array(); $url = array();
@ -896,7 +895,7 @@ class Router {
break; break;
} }
} }
$output = join('/', $urlOut) . '/'; $output = join('/', $urlOut);
} }
if (!empty($args)) { if (!empty($args)) {
@ -1454,22 +1453,27 @@ class RouterRoute {
if (!$this->compiled()) { if (!$this->compiled()) {
$this->compile(); $this->compile();
} }
if (isset($this->defaults['prefix'])) { $url += array('controller' => null, 'plugin' => null);
$prefix = $this->defaults['prefix']; $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(); $pass = array();
$params = Set::diff($url, $this->defaults); $params = Set::diff($url, $defaults);
$urlInv = array_combine(array_values($url), array_keys($url)); $urlInv = array_combine(array_values($url), array_keys($url));
$i = 0; $i = 0;
while (isset($this->defaults[$i])) { while (isset($defaults[$i])) {
if (isset($urlInv[$this->defaults[$i]])) { if (isset($urlInv[$defaults[$i]])) {
if (!in_array($this->defaults[$i], $url) && is_int($urlInv[$this->defaults[$i]])) { if (!in_array($defaults[$i], $url) && is_int($urlInv[$defaults[$i]])) {
return false; return false;
} }
unset($urlInv[$this->defaults[$i]], $this->defaults[$i]); unset($urlInv[$defaults[$i]], $defaults[$i]);
} else { } else {
return false; return false;
} }
@ -1490,27 +1494,26 @@ class RouterRoute {
$urlKeys = array_keys($url); $urlKeys = array_keys($url);
$paramsKeys = array_keys($params); $paramsKeys = array_keys($params);
$defaultsKeys = array_keys($this->defaults); $defaultsKeys = array_keys($defaults);
if (!empty($params)) { if (!empty($params)) {
if (array_diff($paramsKeys, $this->keys) != array()) { if (array_diff($paramsKeys, $routeParams) != array()) {
return false; return false;
} }
$required = array_values(array_diff($routeParams, $urlKeys));
$required = array_values(array_diff($this->keys, $urlKeys));
$reqCount = count($required); $reqCount = count($required);
for ($i = 0; $i < $reqCount; $i++) { 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]); unset($required[$i]);
} }
} }
} }
$isFilled = true; $isFilled = true;
if (!empty($this->keys)) { if (!empty($routeParams)) {
$filled = array_intersect_key($url, array_combine($this->keys, array_keys($this->keys))); $filled = array_intersect_key($url, array_combine($routeParams, array_keys($routeParams)));
$isFilled = (array_diff($this->keys, array_keys($filled)) === array()); $isFilled = (array_diff($routeParams, array_keys($filled)) === array());
if (!$isFilled && empty($params)) { if (!$isFilled && empty($params)) {
return false; return false;
} }
@ -1518,28 +1521,28 @@ class RouterRoute {
if (empty($params)) { if (empty($params)) {
return $this->__mapRoute(array_merge($url, compact('pass', 'named', 'prefix'))); 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)) { if (!empty($required)) {
return false; return false;
} }
foreach ($params as $key => $val) { 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($url[$key]) || $url[$key] != $val) || (!isset($defaults[$key]) || $defaults[$key] != $val) && !in_array($key, $routeParams)) {
if (!isset($this->defaults[$key])) { if (!isset($defaults[$key])) {
continue; continue;
} }
return false; return false;
} }
} }
} else { } else {
if (empty($required) && $this->defaults['plugin'] === $url['plugin'] && $this->defaults['controller'] === $url['controller'] && $this->defaults['action'] === $url['action']) { if (empty($required) && $defaults['plugin'] === $url['plugin'] && $defaults['controller'] === $url['controller'] && $defaults['action'] === $url['action']) {
return $this->__mapRoute($route, array_merge($url, compact('pass', 'named', 'prefix'))); return $this->__mapRoute(array_merge($url, compact('pass', 'named', 'prefix')));
} }
return false; return false;
} }
if (!empty($this->params)) { if (!empty($routeOptions)) {
foreach ($this->params as $key => $reg) { foreach ($routeOptions as $key => $reg) {
if (array_key_exists($key, $url) && !preg_match('#' . $reg . '#', $url[$key])) { if (array_key_exists($key, $url) && !preg_match('#' . $reg . '#', $url[$key])) {
return false; return false;
} }

View file

@ -636,7 +636,7 @@ class RouterTest extends CakeTestCase {
)); ));
$result = Router::url(array('plugin' => null, 'controller' => 'myothercontroller')); $result = Router::url(array('plugin' => null, 'controller' => 'myothercontroller'));
$expected = '/myothercontroller/'; $expected = '/myothercontroller';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
Configure::write('Routing.admin', 'admin'); Configure::write('Routing.admin', 'admin');
@ -1582,11 +1582,11 @@ class RouterTest extends CakeTestCase {
)); ));
$result = Router::url(array('controller' => 'my_controller', 'action' => 'my_action')); $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); $this->assertEqual($result, $expected);
$result = Router::url(array('controller' => 'my_controller', 'action' => 'my_action', 'base' => false)); $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); $this->assertEqual($result, $expected);
$result = Router::url(array('controller' => 'my_controller', 'action' => 'my_action', 'base' => true)); $result = Router::url(array('controller' => 'my_controller', 'action' => 'my_action', 'base' => true));
@ -1789,11 +1789,11 @@ class RouterTest extends CakeTestCase {
$expected = '/test2/whatever'; $expected = '/test2/whatever';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
Configure::write('Routing.admin', 'admin'); Configure::write('Routing.prefixes', array('admin'));
Router::reload(); Router::reload();
Router::setRequestInfo(array( 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' => '/') array('plugin' => null, 'controller' => null, 'action' => null, 'base' => '', 'here' => '/protected/images/index', 'webroot' => '/')
)); ));
@ -1843,7 +1843,7 @@ class RouterTest extends CakeTestCase {
)); ));
$result = Router::url(array('action' => 'test_another_action')); $result = Router::url(array('action' => 'test_another_action'));
$expected = '/test/test_another_action/'; $expected = '/test/test_another_action';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = Router::url(array('action' => 'test_another_action', 'locale' => 'eng')); $result = Router::url(array('action' => 'test_another_action', 'locale' => 'eng'));
@ -2040,8 +2040,18 @@ class RouterRouteTestCase extends CakeTestCase {
$route =& new RouterRoute('/blog/:action', array('controller' => 'posts')); $route =& new RouterRoute('/blog/:action', array('controller' => 'posts'));
$result = $route->match(array('controller' => 'posts', 'action' => 'view')); $result = $route->match(array('controller' => 'posts', 'action' => 'view'));
$this->assertEqual($result, '/blog/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);
} }
} }
?> ?>