Skip to content

Commit

Permalink
Reworking parameter munging specific to requestAction into requestAct…
Browse files Browse the repository at this point in the history
…ion. Updating tests cases for Object. As request->data is an array() not null. And leading / is trimmed off of [url][url].
  • Loading branch information
markstory committed Nov 14, 2010
1 parent 55cc329 commit f02e048
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 35 deletions.
27 changes: 0 additions & 27 deletions cake/libs/dispatcher.php
Expand Up @@ -87,19 +87,8 @@ public function __construct($url = null, $base = false) {
* are encountered. * are encountered.
*/ */
public function dispatch(CakeRequest $request, $additionalParams = array()) { public function dispatch(CakeRequest $request, $additionalParams = array()) {
/* Should move to Object::requestAction()
if (is_array($url)) {
$url = $this->_extractParams($url, $additionalParams);
}
if ($url instanceof CakeRequest) {
$request = $url;
} else {
$request = new CakeRequest($url);
}
*/
$this->here = $request->here; $this->here = $request->here;



if ($this->asset($request->url) || $this->cached($request->url)) { if ($this->asset($request->url) || $this->cached($request->url)) {
return; return;
} }
Expand Down Expand Up @@ -180,22 +169,6 @@ protected function _invoke(&$controller, $request) {
$response->send(); $response->send();
} }


/**
* Sets the params when $url is passed as an array to Object::requestAction();
* Merges the $url and $additionalParams and creates a string url.
*
* @param array $url Array or request parameters
* @param array $additionalParams Array of additional parameters.
* @return string $url The generated url string.
*/
protected function _extractParams($url, $additionalParams = array()) {
$defaults = array('pass' => array(), 'named' => array(), 'form' => array());
$params = array_merge($defaults, $url, $additionalParams);

$params += array('base' => false, 'url' => array());
return ltrim(Router::reverse($params), '/');
}

/** /**
* Returns array of GET and POST parameters. GET parameters are taken from given URL. * Returns array of GET and POST parameters. GET parameters are taken from given URL.
* *
Expand Down
20 changes: 16 additions & 4 deletions cake/libs/object.php
Expand Up @@ -67,17 +67,29 @@ public function requestAction($url, $extra = array()) {
return false; return false;
} }
if (!class_exists('dispatcher')) { if (!class_exists('dispatcher')) {
require CAKE . 'dispatcher.php'; require LIBS . 'dispatcher.php';
} }
if (in_array('return', $extra, true)) { if (in_array('return', $extra, true)) {
$extra = array_merge($extra, array('return' => 0, 'autoRender' => 1)); $extra = array_merge($extra, array('return' => 0, 'autoRender' => 1));
} }
if (is_array($url) && !isset($extra['url'])) { if (is_array($url) && !isset($extra['url'])) {
$extra['url'] = array(); $extra['url'] = array();
} }
$params = array_merge(array('autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1), $extra); $extra = array_merge(array('autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1), $extra);
$dispatcher = new Dispatcher;
return $dispatcher->dispatch($url, $params); if (is_string($url)) {
$request = new CakeRequest($url);
} elseif (is_array($url)) {
$params = $url + array('pass' => array(), 'named' => array(), 'base' => false);
$params = array_merge($params, $extra);
$request = new CakeRequest(Router::reverse($params), false);
if (isset($params['data'])) {
$request->data = $params['data'];
}
}

$dispatcher = new Dispatcher();
return $dispatcher->dispatch($request, $extra);
} }


/** /**
Expand Down
11 changes: 7 additions & 4 deletions cake/tests/cases/libs/object.test.php
Expand Up @@ -716,7 +716,7 @@ function testRequestAction() {
$result = $this->object->requestAction('/request_action/normal_request_action'); $result = $this->object->requestAction('/request_action/normal_request_action');
$expected = 'Hello World'; $expected = 'Hello World';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);

App::build(); App::build();
} }


Expand Down Expand Up @@ -824,7 +824,7 @@ function testRequestActionArray() {
function testRequestActionParamParseAndPass() { function testRequestActionParamParseAndPass() {
$result = $this->object->requestAction('/request_action/params_pass'); $result = $this->object->requestAction('/request_action/params_pass');
$this->assertTrue(isset($result['url']['url'])); $this->assertTrue(isset($result['url']['url']));
$this->assertEqual($result['url']['url'], '/request_action/params_pass'); $this->assertEqual($result['url']['url'], 'request_action/params_pass');
$this->assertEqual($result['controller'], 'request_action'); $this->assertEqual($result['controller'], 'request_action');
$this->assertEqual($result['action'], 'params_pass'); $this->assertEqual($result['action'], 'params_pass');
$this->assertEqual($result['form'], array()); $this->assertEqual($result['form'], array());
Expand Down Expand Up @@ -855,9 +855,12 @@ function testRequestActionPostPassing() {
)); ));
$result = $this->object->requestAction(array('controller' => 'request_action', 'action' => 'post_pass')); $result = $this->object->requestAction(array('controller' => 'request_action', 'action' => 'post_pass'));
$expected = null; $expected = null;
$this->assertEqual($expected, $result); $this->assertEmpty($result);


$result = $this->object->requestAction(array('controller' => 'request_action', 'action' => 'post_pass'), array('data' => $_POST['data'])); $result = $this->object->requestAction(
array('controller' => 'request_action', 'action' => 'post_pass'),
array('data' => $_POST['data'])
);
$expected = $_POST['data']; $expected = $_POST['data'];
$this->assertEqual($expected, $result); $this->assertEqual($expected, $result);


Expand Down

0 comments on commit f02e048

Please sign in to comment.