refactor args generation

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5694 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2007-09-26 09:49:01 +00:00
parent e5aef425a6
commit ad6a0c12c5
4 changed files with 59 additions and 56 deletions

View file

@ -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'];

View file

@ -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;
}
}
?>
?>

View file

@ -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'),

View file

@ -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());
}
}
?>