Moving Controller::httpCodes() to CakeResponse

This commit is contained in:
José Lorenzo Rodríguez 2010-08-01 00:09:09 -04:30
parent 754c0776f8
commit 3ab4e09e11
6 changed files with 86 additions and 79 deletions

View file

@ -256,9 +256,14 @@ class Dispatcher {
if (!$ctrlClass) { if (!$ctrlClass) {
return $controller; return $controller;
} }
if (!$this->response) {
$this->response = new CakeResponse(array(
'charset' => Configure::read('App.encoding')
));
}
$ctrlClass .= 'Controller'; $ctrlClass .= 'Controller';
if (class_exists($ctrlClass)) { if (class_exists($ctrlClass)) {
$controller = new $ctrlClass($this->request); $controller = new $ctrlClass($this->request, $this->response);
} }
return $controller; return $controller;
} }

View file

@ -454,6 +454,40 @@ class CakeResponse {
return $this->_status = $code; return $this->_status = $code;
} }
/**
* Queries & sets valid HTTP response codes & messages.
*
* @param mixed $code If $code is an integer, then the corresponding code/message is
* returned if it exists, null if it does not exist. If $code is an array,
* then the 'code' and 'message' keys of each nested array are added to the default
* HTTP codes. Example:
*
* httpCodes(404); // returns array(404 => 'Not Found')
*
* httpCodes(array(
* 701 => 'Unicorn Moved',
* 800 => 'Unexpected Minotaur'
* )); // sets these new values, and returns true
*
* @return mixed associative array of the HTTP codes as keys, and the message
* strings as values, or null of the given $code does not exist.
*/
public function httpCodes($code = null) {
if (empty($code)) {
return $this->_statusCodes;
}
if (is_array($code)) {
$this->_statusCodes = $code + $this->_statusCodes;
return true;
}
if (!isset($this->_statusCodes[$code])) {
return null;
}
return array($code => $this->_statusCodes[$code]);
}
/** /**
* Sets the response content type. It can be either a file extension * Sets the response content type. It can be either a file extension
* which will be mapped internally to a mime-type or a string representing a mime-type * which will be mapped internally to a mime-type or a string representing a mime-type

View file

@ -326,22 +326,14 @@ class Controller extends Object {
*/ */
public $validationErrors = null; public $validationErrors = null;
/**
* Contains a list of the HTTP codes that CakePHP recognizes. These may be
* queried and/or modified through Controller::httpCodes(), which is also
* tasked with their lazy-loading.
*
* @var array Associative array of HTTP codes and their associated messages.
*/
private $__httpCodes = null;
/** /**
* Constructor. * Constructor.
* *
* @param CakeRequest $request Request object for this controller can be null for testing. * @param CakeRequest $request Request object for this controller can be null for testing.
* But expect that features that use the params will not work. * But expect that features that use the params 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) { if ($this->name === null) {
$r = null; $r = null;
if (!preg_match('/(.*)Controller/i', get_class($this), $r)) { if (!preg_match('/(.*)Controller/i', get_class($this), $r)) {
@ -366,6 +358,7 @@ class Controller extends Object {
if ($request instanceof CakeRequest) { if ($request instanceof CakeRequest) {
$this->_setRequest($request); $this->_setRequest($request);
} }
$this->response = $response;
parent::__construct(); parent::__construct();
} }
@ -566,41 +559,7 @@ class Controller extends Object {
* strings as values, or null of the given $code does not exist. * strings as values, or null of the given $code does not exist.
*/ */
public function httpCodes($code = null) { public function httpCodes($code = null) {
if (empty($this->__httpCodes)) { return $this->response->httpCodes($code);
$this->__httpCodes = array(
100 => 'Continue', 101 => 'Switching Protocols',
200 => 'OK', 201 => 'Created', 202 => 'Accepted',
203 => 'Non-Authoritative Information', 204 => 'No Content',
205 => 'Reset Content', 206 => 'Partial Content',
300 => 'Multiple Choices', 301 => 'Moved Permanently',
302 => 'Found', 303 => 'See Other',
304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect',
400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required',
403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed',
406 => 'Not Acceptable', 407 => 'Proxy Authentication Required',
408 => 'Request Time-out', 409 => 'Conflict', 410 => 'Gone',
411 => 'Length Required', 412 => 'Precondition Failed',
413 => 'Request Entity Too Large', 414 => 'Request-URI Too Large',
415 => 'Unsupported Media Type', 416 => 'Requested range not satisfiable',
417 => 'Expectation Failed', 500 => 'Internal Server Error',
501 => 'Not Implemented', 502 => 'Bad Gateway',
503 => 'Service Unavailable', 504 => 'Gateway Time-out'
);
}
if (empty($code)) {
return $this->__httpCodes;
}
if (is_array($code)) {
$this->__httpCodes = $code + $this->__httpCodes;
return true;
}
if (!isset($this->__httpCodes[$code])) {
return null;
}
return array($code => $this->__httpCodes[$code]);
} }
/** /**

View file

@ -1384,7 +1384,6 @@ class DispatcherTest extends CakeTestCase {
$Dispatcher->asset('test_plugin/css/theme_one.htc'); $Dispatcher->asset('test_plugin/css/theme_one.htc');
$result = ob_get_clean(); $result = ob_get_clean();
$this->assertEqual('htc file', $result); $this->assertEqual('htc file', $result);
} }
/** /**

View file

@ -279,4 +279,40 @@ class CakeResponseTestCase extends CakeTestCase {
$this->assertTrue($result); $this->assertTrue($result);
$this->assertTrue(in_array('ob_gzhandler', ob_list_handlers())); $this->assertTrue(in_array('ob_gzhandler', ob_list_handlers()));
} }
/**
* Tests the httpCodes method
*
*/
function testHttpCodes() {
$response = new CakeResponse();
$result = $response->httpCodes();
$this->assertEqual(count($result), 39);
$result = $response->httpCodes(100);
$expected = array(100 => 'Continue');
$this->assertEqual($result, $expected);
$codes = array(
1337 => 'Undefined Unicorn',
1729 => 'Hardy-Ramanujan Located'
);
$result = $response->httpCodes($codes);
$this->assertTrue($result);
$this->assertEqual(count($response->httpCodes()), 41);
$result = $response->httpCodes(1337);
$expected = array(1337 => 'Undefined Unicorn');
$this->assertEqual($result, $expected);
$codes = array(404 => 'Sorry Bro');
$result = $response->httpCodes($codes);
$this->assertTrue($result);
$this->assertEqual(count($response->httpCodes()), 41);
$result = $response->httpCodes(404);
$expected = array(404 => 'Sorry Bro');
$this->assertEqual($result, $expected);
}
} }

View file

@ -18,7 +18,7 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/ */
App::import('Controller', 'Controller', false); App::import('Controller', 'Controller', false);
App::import('Core', 'CakeRequest'); App::import('Core', array('CakeRequest', 'CakeResponse'));
App::import('Component', 'Security'); App::import('Component', 'Security');
App::import('Component', 'Cookie'); App::import('Component', 'Cookie');
@ -1450,37 +1450,11 @@ class ControllerTest extends CakeTestCase {
* @return void * @return void
*/ */
function testControllerHttpCodes() { function testControllerHttpCodes() {
$request = new CakeRequest('controller_posts/index'); $Controller = new Controller(null, $this->getMock('CakeResponse', array('httpCodes')));
$Controller->response->expects($this->at(0))->method('httpCodes')->with(null);
$Controller = new Controller($request); $Controller->response->expects($this->at(1))->method('httpCodes')->with(100);
$result = $Controller->httpCodes(); $Controller->httpCodes();
$this->assertEqual(count($result), 39); $Controller->httpCodes(100);
$result = $Controller->httpCodes(100);
$expected = array(100 => 'Continue');
$this->assertEqual($result, $expected);
$codes = array(
1337 => 'Undefined Unicorn',
1729 => 'Hardy-Ramanujan Located'
);
$result = $Controller->httpCodes($codes);
$this->assertTrue($result);
$this->assertEqual(count($Controller->httpCodes()), 41);
$result = $Controller->httpCodes(1337);
$expected = array(1337 => 'Undefined Unicorn');
$this->assertEqual($result, $expected);
$codes = array(404 => 'Sorry Bro');
$result = $Controller->httpCodes($codes);
$this->assertTrue($result);
$this->assertEqual(count($Controller->httpCodes()), 41);
$result = $Controller->httpCodes(404);
$expected = array(404 => 'Sorry Bro');
$this->assertEqual($result, $expected);
} }
/** /**