Making Router::setRequestInfo() able to accept arrays. It will convert older style arrays into a CakeRequest object for later use.

Updating uses of Router::setRequestInfo() to just pass the object where possible.
This commit is contained in:
Mark Story 2010-05-02 01:59:56 -04:00
parent e336df6de8
commit 26279851e1
5 changed files with 24 additions and 17 deletions

View file

@ -116,9 +116,7 @@ class Dispatcher extends Object {
$controller = $this->_getController();
if (!is_object($controller)) {
Router::setRequestInfo(array(
$this->params, array('base' => $request->base, 'webroot' => $request->webroot)
));
Router::setRequestInfo($request);
return $this->cakeError('missingController', array(array(
'className' => Inflector::camelize($request->params['controller']) . 'Controller',
'webroot' => $request->webroot,

View file

@ -594,9 +594,17 @@ class Router {
* @param array $params Parameters and path information
* @return void
*/
public static function setRequestInfo(CakeRequest $request) {
public static function setRequestInfo($request) {
$self = Router::getInstance();
if ($request instanceof CakeRequest) {
$self->__params[] = $request;
} else {
$requestObj = new CakeRequest();
$request += array(array(), array());
$request[0] += array('controller' => false, 'action' => false, 'plugin' => null);
$requestObj->addParams($request[0])->addPaths($request[1]);
$self->__params[] = $requestObj;
}
}
/**
@ -622,10 +630,10 @@ class Router {
public static function getParams($current = false) {
$self = Router::getInstance();
if ($current) {
return $self->__params[count($self->__params) - 1];
return $self->__params[count($self->__params) - 1]->params;
}
if (isset($self->__params[0])) {
return $self->__params[0];
return $self->__params[0]->params;
}
return array();
}

View file

@ -234,7 +234,7 @@ class CacheHelper extends AppHelper {
$controller->action = $this->action = unserialize(\'' . serialize($this->action) . '\');
$controller->data = $this->data = unserialize(\'' . str_replace("'", "\\'", serialize($this->data)) . '\');
$controller->theme = $this->theme = \'' . $this->theme . '\';
Router::setRequestInfo(array($this->params, array(\'base\' => $this->base, \'webroot\' => $this->webroot)));';
Router::setRequestInfo($this->params);';
if ($useCallbacks == true) {
$file .= '

View file

@ -1189,7 +1189,7 @@ class DispatcherTest extends CakeTestCase {
));
App::objects('plugin', null, false);
Router::reload();
Router::parse(new CakeRequest('/'));
Router::parse('/');
$url = '/test_plugin/tests/index';
$result = $Dispatcher->dispatch($url, array('return' => 1));

View file

@ -1940,22 +1940,23 @@ class RouterTest extends CakeTestCase {
$paths = array('base' => '/', 'here' => '/products/display/5', 'webroot' => '/webroot');
$params = array('param1' => '1', 'param2' => '2');
Router::setRequestInfo(array($params, $paths));
$expected = array(
'plugin' => null, 'controller' => false, 'action' => false,
'param1' => '1', 'param2' => '2'
'param1' => '1', 'param2' => '2', 'form' => array()
);
$this->assertEqual(Router::getparams(), $expected);
$this->assertEqual(Router::getparam('controller'), false);
$this->assertEqual(Router::getparam('param1'), '1');
$this->assertEqual(Router::getparam('param2'), '2');
$this->assertEqual(Router::getParams(), $expected);
$this->assertEqual(Router::getParam('controller'), false);
$this->assertEqual(Router::getParam('param1'), '1');
$this->assertEqual(Router::getParam('param2'), '2');
Router::reload();
$params = array('controller' => 'pages', 'action' => 'display');
Router::setRequestInfo(array($params, $paths));
$expected = array('plugin' => null, 'controller' => 'pages', 'action' => 'display');
$this->assertEqual(Router::getparams(), $expected);
$this->assertEqual(Router::getparams(true), $expected);
$expected = array('plugin' => null, 'controller' => 'pages', 'action' => 'display', 'form' => array());
$this->assertEqual(Router::getParams(), $expected);
$this->assertEqual(Router::getParams(true), $expected);
}
/**