Skip to content

Commit

Permalink
Making Router::setRequestInfo() able to accept arrays. It will conver…
Browse files Browse the repository at this point in the history
…t older style arrays into a CakeRequest object for later use.

Updating uses of Router::setRequestInfo() to just pass the object where possible.
  • Loading branch information
markstory committed May 2, 2010
1 parent e336df6 commit 2627985
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
4 changes: 1 addition & 3 deletions cake/dispatcher.php
Expand Up @@ -116,9 +116,7 @@ public function dispatch($url = null, $additionalParams = array()) {
$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,
Expand Down
16 changes: 12 additions & 4 deletions cake/libs/router.php
Expand Up @@ -594,9 +594,17 @@ function __connectDefaultRoutes() {
* @param array $params Parameters and path information
* @return void
*/
public static function setRequestInfo(CakeRequest $request) {
public static function setRequestInfo($request) {
$self = Router::getInstance();
$self->__params[] = $request;
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;
}
}

/**
Expand All @@ -622,10 +630,10 @@ public static function getRequest($current = false) {
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();
}
Expand Down
2 changes: 1 addition & 1 deletion cake/libs/view/helpers/cache.php
Expand Up @@ -234,7 +234,7 @@ function __writeFile($content, $timestamp, $useCallbacks = false) {
$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 .= '
Expand Down
2 changes: 1 addition & 1 deletion cake/tests/cases/dispatcher.test.php
Expand Up @@ -1189,7 +1189,7 @@ public function testTestPluginDispatch() {
));
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));
Expand Down
17 changes: 9 additions & 8 deletions cake/tests/cases/libs/router.test.php
Expand Up @@ -1940,22 +1940,23 @@ public function testGetParams() {
$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);
}

/**
Expand Down

0 comments on commit 2627985

Please sign in to comment.