mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Refactoring RequestHandler methods to use the response object
This commit is contained in:
parent
4fd032bc10
commit
5fb320f31d
3 changed files with 44 additions and 53 deletions
|
@ -142,7 +142,7 @@ class RequestHandlerComponent extends Object {
|
|||
$controller->request->params['isAjax'] = $this->request->is('ajax');
|
||||
$isRecognized = (
|
||||
!in_array($this->ext, array('html', 'htm')) &&
|
||||
in_array($this->ext, array_keys($this->_contentTypeMap))
|
||||
$this->response->getMimeType($this->ext)
|
||||
);
|
||||
|
||||
if (!empty($this->ext) && $isRecognized) {
|
||||
|
@ -183,12 +183,12 @@ class RequestHandlerComponent extends Object {
|
|||
$url = Router::url($url + array('base' => false));
|
||||
}
|
||||
if (!empty($status)) {
|
||||
$statusCode = $controller->httpCodes($status);
|
||||
$statusCode = $this->response->httpCodes($status);
|
||||
$code = key($statusCode);
|
||||
$msg = $statusCode[$code];
|
||||
$controller->header("HTTP/1.1 {$code} {$msg}");
|
||||
$this->response->statusCode($code);
|
||||
}
|
||||
echo $this->requestAction($url, array('return', 'bare' => false));
|
||||
$this->response->body($this->requestAction($url, array('return', 'bare' => false)));
|
||||
$this->response->send();
|
||||
$this->_stop();
|
||||
}
|
||||
|
||||
|
@ -502,7 +502,7 @@ class RequestHandlerComponent extends Object {
|
|||
$this->__renderType = $type;
|
||||
$controller->layoutPath = $type;
|
||||
|
||||
if (isset($this->_contentTypeMap[$type])) {
|
||||
if ($this->response->getMimeType($type)) {
|
||||
$this->respondAs($type, $options);
|
||||
}
|
||||
|
||||
|
@ -534,24 +534,18 @@ class RequestHandlerComponent extends Object {
|
|||
* @see RequestHandlerComponent::setContent()
|
||||
*/
|
||||
function respondAs($type, $options = array()) {
|
||||
if (!array_key_exists($type, $this->_contentTypeMap) && strpos($type, '/') === false) {
|
||||
return false;
|
||||
}
|
||||
$defaults = array('index' => 0, 'charset' => null, 'attachment' => false);
|
||||
$options = array_merge($defaults, $options);
|
||||
$defaults = array('index' => null, 'charset' => null, 'attachment' => false);
|
||||
$options = $options + $defaults;
|
||||
|
||||
if (strpos($type, '/') === false && isset($this->_contentTypeMap[$type])) {
|
||||
$cType = null;
|
||||
if (is_array($this->_contentTypeMap[$type]) && isset($this->_contentTypeMap[$type][$options['index']])) {
|
||||
$cType = $this->_contentTypeMap[$type][$options['index']];
|
||||
} elseif (is_array($this->_contentTypeMap[$type]) && isset($this->_contentTypeMap[$type][0])) {
|
||||
$cType = $this->_contentTypeMap[$type][0];
|
||||
} elseif (isset($this->_contentTypeMap[$type])) {
|
||||
$cType = $this->_contentTypeMap[$type];
|
||||
} else {
|
||||
$cType = null;
|
||||
if (strpos($type, '/') === false) {
|
||||
$cType = $this->response->getMimeType($type);
|
||||
if ($cType === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_array($cType) && isset($cType[$options['index']])) {
|
||||
$cType = $cType[$options['index']];
|
||||
}
|
||||
if (is_array($cType)) {
|
||||
if ($this->prefers($cType)) {
|
||||
$cType = $this->prefers($cType);
|
||||
|
@ -564,10 +558,10 @@ class RequestHandlerComponent extends Object {
|
|||
}
|
||||
|
||||
if ($cType != null) {
|
||||
$header = 'Content-type: ' . $cType;
|
||||
$this->response->type($cType);
|
||||
|
||||
if (!empty($options['charset'])) {
|
||||
$header .= '; charset=' . $options['charset'];
|
||||
$this->response->charset($options['charset']);
|
||||
}
|
||||
if (!empty($options['attachment'])) {
|
||||
$this->_header("Content-Disposition: attachment; filename=\"{$options['attachment']}\"");
|
||||
|
@ -625,12 +619,12 @@ class RequestHandlerComponent extends Object {
|
|||
if (is_array($alias)) {
|
||||
return array_map(array($this, 'mapAlias'), $alias);
|
||||
}
|
||||
if (isset($this->_contentTypeMap[$alias])) {
|
||||
$types = $this->_contentTypeMap[$alias];
|
||||
if (is_array($types)) {
|
||||
return $types[0];
|
||||
$type = $this->response->getMimeType($alias);
|
||||
if ($type) {
|
||||
if (is_array($type)) {
|
||||
return $type[0];
|
||||
}
|
||||
return $types;
|
||||
return $type;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -86,6 +86,8 @@ class CakeResponseTestCase extends CakeTestCase {
|
|||
|
||||
$response->type(array('keynote' => 'application/keynote'));
|
||||
$this->assertEquals($response->type('keynote'), 'application/keynote');
|
||||
|
||||
$this->assertFalse($response->type('wackytype'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -317,22 +317,17 @@ class RequestHandlerComponentTest extends CakeTestCase {
|
|||
* @return void
|
||||
*/
|
||||
function testRespondAs() {
|
||||
$debug = Configure::read('debug');
|
||||
Configure::write('debug', 0);
|
||||
$this->RequestHandler->response = $this->getMock('CakeResponse', array('type'));
|
||||
$this->RequestHandler->response->expects($this->at(0))->method('type')
|
||||
->with('application/json');
|
||||
$this->RequestHandler->response->expects($this->at(1))->method('type')
|
||||
->with('text/xml');
|
||||
|
||||
$RequestHandler = $this->getMock('RequestHandlerComponent', array('_header'));
|
||||
$RequestHandler->expects($this->at(0))->method('_header')
|
||||
->with('Content-type: application/json');
|
||||
$RequestHandler->expects($this->at(1))->method('_header')
|
||||
->with('Content-type: text/xml');
|
||||
|
||||
$result = $RequestHandler->respondAs('json');
|
||||
$result = $this->RequestHandler->respondAs('json');
|
||||
$this->assertTrue($result);
|
||||
|
||||
$result = $RequestHandler->respondAs('text/xml');
|
||||
$result = $this->RequestHandler->respondAs('text/xml');
|
||||
$this->assertTrue($result);
|
||||
|
||||
Configure::write('debug', $debug);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -341,19 +336,14 @@ class RequestHandlerComponentTest extends CakeTestCase {
|
|||
* @return void
|
||||
*/
|
||||
function testRespondAsWithAttachment() {
|
||||
$debug = Configure::read('debug');
|
||||
Configure::write('debug', 0);
|
||||
$this->RequestHandler->response = $this->getMock('CakeResponse', array('type', 'download'));
|
||||
$this->RequestHandler->response->expects($this->once())->method('download')
|
||||
->with('myfile.xml');
|
||||
$this->RequestHandler->response->expects($this->once())->method('type')
|
||||
->with('application/xml');
|
||||
|
||||
$RequestHandler = $this->getMock('RequestHandlerComponent', array('_header'));
|
||||
$RequestHandler->expects($this->at(0))->method('_header')
|
||||
->with('Content-Disposition: attachment; filename="myfile.xml"');
|
||||
$RequestHandler->expects($this->at(1))->method('_header')
|
||||
->with('Content-type: application/xml');
|
||||
|
||||
$result = $RequestHandler->respondAs('xml', array('attachment' => 'myfile.xml'));
|
||||
$result = $this->RequestHandler->respondAs('xml', array('attachment' => 'myfile.xml'));
|
||||
$this->assertTrue($result);
|
||||
|
||||
Configure::write('debug', $debug);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -618,10 +608,11 @@ class RequestHandlerComponentTest extends CakeTestCase {
|
|||
App::build(array(
|
||||
'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
|
||||
), true);
|
||||
|
||||
$this->Controller->request = $this->getMock('CakeRequest');
|
||||
$this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
|
||||
$this->Controller->RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'));
|
||||
$this->Controller->RequestHandler->request = $this->Controller->request;
|
||||
$this->Controller->RequestHandler->response = $this->Controller->response;
|
||||
|
||||
$this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
|
||||
$this->Controller->RequestHandler->expects($this->once())->method('_stop');
|
||||
|
@ -648,8 +639,10 @@ class RequestHandlerComponentTest extends CakeTestCase {
|
|||
), true);
|
||||
|
||||
$this->Controller->request = $this->getMock('CakeRequest');
|
||||
$this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
|
||||
$this->Controller->RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'));
|
||||
$this->Controller->RequestHandler->request = $this->Controller->request;
|
||||
$this->Controller->RequestHandler->response = $this->Controller->response;
|
||||
|
||||
$this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
|
||||
$this->Controller->RequestHandler->expects($this->once())->method('_stop');
|
||||
|
@ -682,6 +675,7 @@ class RequestHandlerComponentTest extends CakeTestCase {
|
|||
));
|
||||
|
||||
$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'));
|
||||
$RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader'));
|
||||
$RequestHandler->request = new CakeRequest('posts/index');
|
||||
|
||||
ob_start();
|
||||
|
@ -703,12 +697,13 @@ class RequestHandlerComponentTest extends CakeTestCase {
|
|||
|
||||
$controller = $this->getMock('Controller', array('header'));
|
||||
$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'));
|
||||
$RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader','statusCode'));
|
||||
$RequestHandler->request = $this->getMock('CakeRequest');
|
||||
$RequestHandler->request->expects($this->once())->method('is')
|
||||
->with('ajax')
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$controller->expects($this->once())->method('header')->with('HTTP/1.1 403 Forbidden');
|
||||
$RequestHandler->response->expects($this->once())->method('statusCode')->with(403);
|
||||
|
||||
ob_start();
|
||||
$RequestHandler->beforeRedirect($controller, 'request_handler_test/param_method/first/second', 403);
|
||||
|
|
Loading…
Add table
Reference in a new issue