From ad6a0c12c5db23abde69a7049e9692ba8880cd6c Mon Sep 17 00:00:00 2001 From: gwoo Date: Wed, 26 Sep 2007 09:49:01 +0000 Subject: [PATCH] refactor args generation git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5694 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/dispatcher.php | 5 +- cake/libs/router.php | 69 +++++++-------------------- cake/tests/cases/dispatcher.test.php | 4 +- cake/tests/cases/libs/router.test.php | 37 ++++++++++++++ 4 files changed, 59 insertions(+), 56 deletions(-) diff --git a/cake/dispatcher.php b/cake/dispatcher.php index f4d312e74..0e38b6543 100644 --- a/cake/dispatcher.php +++ b/cake/dispatcher.php @@ -189,9 +189,8 @@ class Dispatcher extends Object { $controller->action = $this->params['action']; $controller->webservices = $this->params['webservices']; - list($passedArgs, $namedArgs) = Router::getArgs($this->params); - $controller->passedArgs = $passedArgs; - $controller->namedArgs = $namedArgs; + $controller->passedArgs = $this->params['pass']; + $controller->namedArgs = Set::diff(Set::extract($this->params['pass'], '{n}'), $this->params['pass']); if (!empty($controller->params['data'])) { $controller->data =& $controller->params['data']; diff --git a/cake/libs/router.php b/cake/libs/router.php index 9ab302700..05849de74 100644 --- a/cake/libs/router.php +++ b/cake/libs/router.php @@ -260,7 +260,7 @@ class Router extends Object { ); $prefix = $options['prefix']; - foreach((array)$controller as $ctlName) { + foreach ((array)$controller as $ctlName) { $urlName = Inflector::underscore($ctlName); foreach ($_this->__resourceMap as $params) { extract($params); @@ -377,16 +377,13 @@ class Router extends Object { } elseif (isset($names[$key]) && empty($names[$key]) && empty($out[$names[$key]])) { break; //leave the default values; } else { - // unnamed elements go in as 'pass' - $out['pass'] = am($out['pass'], array_map( - array(&$_this, 'stripEscape'), - Set::filter(explode('/', $found), true) - )); + $out['pass'] = am($out['pass'], $_this->getArgs($found)); } } break; } } + if (!empty($ext)) { $out['url']['ext'] = $ext; } @@ -1073,58 +1070,28 @@ class Router extends Object { } /** - * Takes an array of params and converts it to named args + * Takes an passed params and converts it to args * * @access public * @param array $params - * @param mixed $named - * @param string $separator * @static */ - function getArgs($params, $named = true) { + function getArgs($pass) { $_this =& Router::getInstance(); - $passedArgs = $namedArgs = array(); - $separator = $_this->__argSeparator; - $namedArgs = true; - - if (is_array($named)) { - if (array_key_exists($params['action'], $named)) { - $named = $named[$params['action']]; - } - $namedArgs = true; - } - if (!empty($params['pass'])) { - $passedArgs = $params['pass']; - if ($namedArgs === true || $named == true) { - $namedArgs = array(); - $c = count($passedArgs); - for ($i = 0; $i <= $c; $i++) { - if (isset($passedArgs[$i]) && strpos($passedArgs[$i], $separator) !== false) { - list($argKey, $argVal) = explode($separator, $passedArgs[$i]); - if ($named === true || (!empty($named) && in_array($argKey, array_keys($named)))) { - $passedArgs[$argKey] = $argVal; - $namedArgs[$argKey] = $argVal; - unset($passedArgs[$i]); - unset($params['pass'][$i]); - } - } elseif ($separator === '/') { - $ii = $i + 1; - if (isset($passedArgs[$i]) && isset($passedArgs[$ii])) { - $argKey = $passedArgs[$i]; - $argVal = $passedArgs[$ii]; - if (empty($namedArgs) || (!empty($namedArgs) && in_array($argKey, array_keys($namedArgs)))) { - $passedArgs[$argKey] = $argVal; - $namedArgs[$argKey] = $argVal; - unset($passedArgs[$i], $passedArgs[$ii]); - unset($params['pass'][$i], $params['pass'][$ii]); - } - } - } - } + $args = array(); + $pass = array_map( + array(&$_this, 'stripEscape'), + Set::filter(explode('/', $pass), true) + ); + foreach ($pass as $param) { + if (strpos($param, $_this->__argSeparator)) { + $param = explode($_this->__argSeparator, $param); + $args[$param[0]] = $param[1]; + } else { + $args[] = $param; } } - return array($passedArgs, $namedArgs); + return $args; } } - -?> +?> \ No newline at end of file diff --git a/cake/tests/cases/dispatcher.test.php b/cake/tests/cases/dispatcher.test.php index bf44ce88e..09970349b 100644 --- a/cake/tests/cases/dispatcher.test.php +++ b/cake/tests/cases/dispatcher.test.php @@ -62,7 +62,7 @@ class TestDispatcher extends Dispatcher { } -class MyPluginAppController extends Controller { +class MyPluginAppController extends AppController { } @@ -483,7 +483,7 @@ class DispatcherTest extends UnitTestCase { $result = $dispatcher->parseParams($url); - $expected = array('pass' => array('home', 'param:value', 'param2:value2'), + $expected = array('pass' => array('home', 'param'=> 'value', 'param2'=> 'value2'), 'plugin'=> 'my_plugin', 'controller'=> 'some_pages', 'action'=> 'display', 'form'=> null, //array('testdata'=> 'My Posted Data'), 'url'=> array('url'=> 'my_plugin/some_pages/home/param:value/param2:value2'), diff --git a/cake/tests/cases/libs/router.test.php b/cake/tests/cases/libs/router.test.php index 32a010ccc..c1a497cb5 100644 --- a/cake/tests/cases/libs/router.test.php +++ b/cake/tests/cases/libs/router.test.php @@ -686,6 +686,8 @@ class RouterTest extends UnitTestCase { $expected = '/posts/index/published:0/deleted:0'; $this->assertEqual($result, $expected); + Configure::write('Routing.admin', 'admin'); + $this->router->reload(); $this->router->setRequestInfo(array( array('admin' => true, 'controller' => 'controller', 'action' => 'index', 'form' => array(), 'url' => array(), 'bare' => 0, 'webservices' => null, 'plugin' => null), @@ -696,6 +698,20 @@ class RouterTest extends UnitTestCase { $result = $this->router->url(array('page' => 1, 0 => null, 'sort' => 'controller', 'direction' => 'asc', 'order' => null)); $expected = "/admin/controller/index/page:1/sort:controller/direction:asc"; $this->assertEqual($result, $expected); + + $this->router->reload(); + $this->router->setRequestInfo(array( + array('admin' => true, 'controller' => 'controller', 'action' => 'index', 'form' => array(), 'url' => array(), 'bare' => 0, 'webservices' => null, 'plugin' => null), + array('base' => '/', 'here' => '/', 'webroot' => '/base/', 'passedArgs' => array('type'=> 'whatever'), 'argSeparator' => ':', 'namedArgs' => array('type'=> 'whatever'), 'webservices' => null) + )); + + $this->router->connectNamed(array('type')); + + $this->router->parse('/admin/controller/index/type:whatever'); + + $result = $this->router->url(array('type'=> 'new')); + $expected = "/admin/controller/index/type:new"; + $this->assertEqual($result, $expected); } function testParamsUrlParsing() { @@ -763,7 +779,28 @@ class RouterTest extends UnitTestCase { $this->router->connect('/test2/*', array('controller' => 'pages', 'action' => 'display', 2)); $this->router->connect('/test/*', array('controller' => 'pages', 'action' => 'display', 1)); $this->router->parse('/'); + + $this->router->reload(); + + $this->router->setRequestInfo(array( + array ( 'plugin' => NULL, 'controller' => 'images', 'action' => 'index', 'pass' => array ( ), 'prefix' => 'protected', 'admin' => false, 'form' => array ( ), 'url' => array ( 'url' => 'protected/images/index', ), 'bare' => 0, 'webservices' => NULL, ), + array ( 'plugin' => NULL, 'controller' => NULL, 'action' => NULL, 'base' => '', 'here' => '/protected/images/index', 'webroot' => '/', ) + )); + + $this->router->connect('/protected/:controller/:action/*', array( + 'controller' => 'users', + 'action' => 'index', + 'prefix' => 'protected' + ) + ); + $this->router->parse('/'); + $result = $this->router->url(array('controller' => 'images', 'action' => 'add')); + $expected = '/protected/images/add'; + $this->assertEqual($result, $expected); + + //debug($this->router->prefixes()); } + } ?> \ No newline at end of file