Permalink
Browse files

Modifying Dispatcher and its test cases to accept a CakeResponse as p…

…art of its

arguments.
  • Loading branch information...
1 parent 420a2f5 commit 9641bcc8dfa6a73312a39a3815219f648bbe1b23 @markstory markstory committed Jul 3, 2011
Showing with 117 additions and 95 deletions.
  1. +7 −4 lib/Cake/Controller/Controller.php
  2. +23 −23 lib/Cake/Routing/Dispatcher.php
  3. +87 −68 lib/Cake/Test/Case/Routing/DispatcherTest.php
@@ -291,10 +291,11 @@ class Controller extends Object {
/**
* Constructor.
*
- * @param CakeRequest $request Request object for this controller can be null for testing.
- * But expect that features that use the params will not work.
+ * @param CakeRequest $request Request object for this controller. Can be null for testing,
+ * but expect that features that use the request parameters will not work.
+ * @param CakeResponse $response Response object for this controller.
*/
- public function __construct($request = null) {
+ public function __construct($request = null, $response = null) {
if ($this->name === null) {
$this->name = substr(get_class($this), 0, strlen(get_class($this)) -10);
}
@@ -315,7 +316,9 @@ public function __construct($request = null) {
if ($request instanceof CakeRequest) {
$this->setRequest($request);
}
- $this->getResponse();
+ if ($response instanceof CakeResponse) {
+ $this->response = $response;
+ }
parent::__construct();
}
@@ -72,19 +72,20 @@ public function __construct($url = null, $base = false) {
* If the controller is found, and the action is not found an exception will be thrown.
*
* @param CakeRequest $request Request object to dispatch.
+ * @param CakeResponse $response Response object to put the results of the dispatch into.
* @param array $additionalParams Settings array ("bare", "return") which is melded with the GET and POST params
* @return boolean Success
* @throws MissingControllerException, MissingActionException, PrivateActionException if any of those error states
* are encountered.
*/
- public function dispatch(CakeRequest $request, $additionalParams = array()) {
- if ($this->asset($request->url) || $this->cached($request->here)) {
+ public function dispatch(CakeRequest $request, CakeResponse $response, $additionalParams = array()) {
+ if ($this->asset($request->url, $response) || $this->cached($request->here)) {
return;
}
$request = $this->parseParams($request, $additionalParams);
Router::setRequestInfo($request);
- $controller = $this->_getController($request);
+ $controller = $this->_getController($request, $response);
if (!($controller instanceof Controller)) {
throw new MissingControllerException(array(
@@ -99,7 +100,7 @@ public function dispatch(CakeRequest $request, $additionalParams = array()) {
));
}
- return $this->_invoke($controller, $request);
+ return $this->_invoke($controller, $request, $response);
}
/**
@@ -133,7 +134,7 @@ protected function _isPrivateAction($request) {
* @return string Output as sent by controller
* @throws MissingActionException when the action being called is missing.
*/
- protected function _invoke(Controller $controller, CakeRequest $request) {
+ protected function _invoke(Controller $controller, CakeRequest $request, CakeResponse $response) {
$controller->constructClasses();
$controller->startupProcess();
@@ -149,7 +150,6 @@ protected function _invoke(Controller $controller, CakeRequest $request) {
));
}
$result = call_user_func_array(array(&$controller, $request->params['action']), $request->params['pass']);
- $response = $controller->getResponse();
if ($controller->autoRender) {
$controller->render();
@@ -192,15 +192,16 @@ public function parseParams(CakeRequest $request, $additionalParams = array()) {
/**
* Get controller to use, either plugin controller or application controller
*
- * @param array $params Array of parameters
+ * @param CakeRequest $request Request object
+ * @param CakeResponse $response Response for the controller.
* @return mixed name of controller if not loaded, or object if loaded
*/
- protected function _getController($request) {
+ protected function _getController($request, $response) {
$ctrlClass = $this->_loadController($request);
if (!$ctrlClass) {
return false;
}
- return new $ctrlClass($request);
+ return new $ctrlClass($request, $response);
}
/**
@@ -270,10 +271,11 @@ public function cached($path) {
/**
* Checks if a requested asset exists and sends it to the browser
*
- * @param $url string $url Requested URL
+ * @param string $url Requested URL
+ * @param CakeResponse $response The response object to put the file contents in.
* @return boolean True on success if the asset file was found and sent
*/
- public function asset($url) {
+ public function asset($url, CakeResponse $response) {
if (strpos($url, '..') !== false || strpos($url, '.') === false) {
return false;
}
@@ -286,12 +288,9 @@ public function asset($url) {
strpos($url, 'cjs/') === 0 ||
preg_match('#^/((theme/[^/]+)/cjs/)|(([^/]+)(?<!js)/cjs)/#i', $url)
);
- if (!$this->response) {
- $this->response = new CakeResponse();
- }
if (($isCss && empty($filters['css'])) || ($isJs && empty($filters['js']))) {
- $this->response->statusCode(404);
- $this->response->send();
+ $response->statusCode(404);
+ $response->send();
return true;
} elseif ($isCss) {
include WWW_ROOT . DS . $filters['css'];
@@ -326,7 +325,7 @@ public function asset($url) {
}
if ($assetFile !== null) {
- $this->_deliverAsset($assetFile, $ext);
+ $this->_deliverAsset($response, $assetFile, $ext);
return true;
}
return false;
@@ -335,23 +334,24 @@ public function asset($url) {
/**
* Sends an asset file to the client
*
+ * @param CakeResponse $response The response object to use.
* @param string $assetFile Path to the asset file in the file system
* @param string $ext The extension of the file to determine its mime type
* @return void
*/
- protected function _deliverAsset($assetFile, $ext) {
+ protected function _deliverAsset(CakeResponse $response, $assetFile, $ext) {
ob_start();
- $compressionEnabled = Configure::read('Asset.compress') && $this->response->compress();
- if ($this->response->type($ext) == $ext) {
+ $compressionEnabled = Configure::read('Asset.compress') && $response->compress();
+ if ($response->type($ext) == $ext) {
$contentType = 'application/octet-stream';
$agent = env('HTTP_USER_AGENT');
if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent) || preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
$contentType = 'application/octetstream';
}
- $this->response->type($contentType);
+ $response->type($contentType);
}
- $this->response->cache(filemtime($assetFile));
- $this->response->send();
+ $response->cache(filemtime($assetFile));
+ $response->send();
ob_clean();
if ($ext === 'css' || $ext === 'js') {
include($assetFile);
Oops, something went wrong.

0 comments on commit 9641bcc

Please sign in to comment.