mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Refactoring RequestHandlerComponent::mapType() and added response object property to the component
This commit is contained in:
parent
040740c05d
commit
4fd032bc10
3 changed files with 61 additions and 85 deletions
|
@ -507,7 +507,7 @@ class CakeResponse {
|
|||
* e.g `type(array('jpg' => 'text/plain'));`
|
||||
*
|
||||
* @param string $contentType
|
||||
* @return string current content type
|
||||
* @return mixed current content type or false if supplied an invalid content type
|
||||
*/
|
||||
public function type($contentType = null) {
|
||||
if (is_null($contentType)) {
|
||||
|
@ -523,9 +523,52 @@ class CakeResponse {
|
|||
$contentType = $this->_mimeTypes[$contentType];
|
||||
$contentType = is_array($contentType) ? current($contentType) : $contentType;
|
||||
}
|
||||
if (strpos($contentType, '/') === false) {
|
||||
return false;
|
||||
}
|
||||
return $this->_contentType = $contentType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the mime type definition for an alias
|
||||
*
|
||||
* e.g `getMimeType('pdf'); // returns 'application/pdf'`
|
||||
*
|
||||
* @param string $alias the content type alias to map
|
||||
* @return mixed string mapped mime type or false if $alias is not mapped
|
||||
*/
|
||||
public function getMimeType($alias) {
|
||||
if (isset($this->_mimeTypes[$alias])) {
|
||||
return $this->_mimeTypes[$alias];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps a content-type back to an alias
|
||||
*
|
||||
* e.g `mapType('application/pdf'); // returns 'pdf'`
|
||||
*
|
||||
* @param mixed $type Either a string content type to map, or an array of types.
|
||||
* @return mixed Aliases for the types provided.
|
||||
*/
|
||||
public function mapType($ctype) {
|
||||
if (is_array($ctype)) {
|
||||
return array_map(array($this, 'mapType'), $ctype);
|
||||
}
|
||||
$keys = array_keys($this->_mimeTypes);
|
||||
$count = count($keys);
|
||||
|
||||
foreach ($this->_mimeTypes as $alias => $types) {
|
||||
if (is_array($types) && in_array($ctype, $types)) {
|
||||
return $alias;
|
||||
} elseif (is_string($types) && $types == $ctype) {
|
||||
return $alias;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the response charset
|
||||
* if $charset is null the current charset is returned
|
||||
|
|
|
@ -57,7 +57,7 @@ class RequestHandlerComponent extends Object {
|
|||
private $__responseTypeSet = null;
|
||||
|
||||
/**
|
||||
* Holds the copy of Controller::$request
|
||||
* Holds the reference to Controller::$request
|
||||
*
|
||||
* @var CakeRequest
|
||||
* @access public
|
||||
|
@ -65,42 +65,12 @@ class RequestHandlerComponent extends Object {
|
|||
public $request;
|
||||
|
||||
/**
|
||||
* Friendly content-type mappings used to set response types and determine
|
||||
* request types. Can be modified with RequestHandler::setContent()
|
||||
* Holds the reference to Controller::$response
|
||||
*
|
||||
* @var array
|
||||
* @access private
|
||||
* @see RequestHandlerComponent::setContent
|
||||
* @var CakeResponse
|
||||
* @access public
|
||||
*/
|
||||
protected $_contentTypeMap = array(
|
||||
'javascript' => 'text/javascript',
|
||||
'js' => 'text/javascript',
|
||||
'json' => 'application/json',
|
||||
'css' => 'text/css',
|
||||
'html' => array('text/html', '*/*'),
|
||||
'text' => 'text/plain',
|
||||
'txt' => 'text/plain',
|
||||
'csv' => array('application/vnd.ms-excel', 'text/plain'),
|
||||
'form' => 'application/x-www-form-urlencoded',
|
||||
'file' => 'multipart/form-data',
|
||||
'xhtml' => array('application/xhtml+xml', 'application/xhtml', 'text/xhtml'),
|
||||
'xhtml-mobile' => 'application/vnd.wap.xhtml+xml',
|
||||
'xml' => array('application/xml', 'text/xml'),
|
||||
'rss' => 'application/rss+xml',
|
||||
'atom' => 'application/atom+xml',
|
||||
'amf' => 'application/x-amf',
|
||||
'wap' => array(
|
||||
'text/vnd.wap.wml',
|
||||
'text/vnd.wap.wmlscript',
|
||||
'image/vnd.wap.wbmp'
|
||||
),
|
||||
'wml' => 'text/vnd.wap.wml',
|
||||
'wmlscript' => 'text/vnd.wap.wmlscript',
|
||||
'wbmp' => 'image/vnd.wap.wbmp',
|
||||
'pdf' => 'application/pdf',
|
||||
'zip' => 'application/x-zip',
|
||||
'tar' => 'application/x-tar'
|
||||
);
|
||||
public $response;
|
||||
|
||||
/**
|
||||
* The template to use when rendering the given content type.
|
||||
|
@ -132,6 +102,7 @@ class RequestHandlerComponent extends Object {
|
|||
*/
|
||||
public function initialize(&$controller, $settings = array()) {
|
||||
$this->request = $controller->request;
|
||||
$this->response = $controller->response;
|
||||
if (isset($controller->params['url']['ext'])) {
|
||||
$this->ext = $controller->params['url']['ext'];
|
||||
}
|
||||
|
@ -359,7 +330,7 @@ class RequestHandlerComponent extends Object {
|
|||
* @return void
|
||||
*/
|
||||
public function setContent($name, $type = null) {
|
||||
$this->_contentTypeMap[$name] = $type;
|
||||
$this->response->type(array($name => $type));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -636,24 +607,11 @@ class RequestHandlerComponent extends Object {
|
|||
/**
|
||||
* Maps a content-type back to an alias
|
||||
*
|
||||
* @param mixed $type Either a string content type to map, or an array of types.
|
||||
* @param mixed $cType Either a string content type to map, or an array of types.
|
||||
* @return mixed Aliases for the types provided.
|
||||
*/
|
||||
public function mapType($ctype) {
|
||||
if (is_array($ctype)) {
|
||||
return array_map(array($this, 'mapType'), $ctype);
|
||||
}
|
||||
$keys = array_keys($this->_contentTypeMap);
|
||||
$count = count($keys);
|
||||
|
||||
foreach ($this->_contentTypeMap as $alias => $types) {
|
||||
if (is_array($types) && in_array($ctype, $types)) {
|
||||
return $alias;
|
||||
} elseif (is_string($types) && $types == $ctype) {
|
||||
return $alias;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
public function mapType($cType) {
|
||||
return $this->response->mapType($cType);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
*/
|
||||
App::import('Controller', 'Controller', false);
|
||||
App::import('Component', array('RequestHandler'));
|
||||
App::import('Core', array('CakeRequest', 'CakeResponse'));
|
||||
|
||||
/**
|
||||
* RequestHandlerTestController class
|
||||
|
@ -44,20 +45,6 @@ class RequestHandlerTestController extends Controller {
|
|||
*/
|
||||
public $uses = null;
|
||||
|
||||
/**
|
||||
* construct method
|
||||
*
|
||||
* @param array $params
|
||||
* @access private
|
||||
* @return void
|
||||
*/
|
||||
function __construct($request, $params = array()) {
|
||||
foreach ($params as $key => $val) {
|
||||
$this->{$key} = $val;
|
||||
}
|
||||
parent::__construct($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* test method for ajax redirection
|
||||
*
|
||||
|
@ -106,20 +93,6 @@ class RequestHandlerTestDisabledController extends Controller {
|
|||
*/
|
||||
public $uses = null;
|
||||
|
||||
/**
|
||||
* construct method
|
||||
*
|
||||
* @param array $params
|
||||
* @access private
|
||||
* @return void
|
||||
*/
|
||||
function __construct($request, $params = array()) {
|
||||
foreach ($params as $key => $val) {
|
||||
$this->{$key} = $val;
|
||||
}
|
||||
parent::__construct($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* beforeFilter method
|
||||
*
|
||||
|
@ -155,12 +128,12 @@ class RequestHandlerComponentTest extends CakeTestCase {
|
|||
public $RequestHandler;
|
||||
|
||||
/**
|
||||
* startTest method
|
||||
* setUp method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function startTest() {
|
||||
function setUp() {
|
||||
$this->_server = $_SERVER;
|
||||
$this->_init();
|
||||
}
|
||||
|
@ -173,9 +146,11 @@ class RequestHandlerComponentTest extends CakeTestCase {
|
|||
*/
|
||||
function _init() {
|
||||
$request = new CakeRequest('controller_posts/index');
|
||||
$this->Controller = new RequestHandlerTestController($request);
|
||||
$response = new CakeResponse();
|
||||
$this->Controller = new RequestHandlerTestController($request, $response);
|
||||
$this->RequestHandler = new RequestHandlerComponent();
|
||||
$this->RequestHandler->request = $request;
|
||||
$this->RequestHandler->response = $response;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -184,7 +159,7 @@ class RequestHandlerComponentTest extends CakeTestCase {
|
|||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function endTest() {
|
||||
function tearDown() {
|
||||
unset($this->RequestHandler);
|
||||
unset($this->Controller);
|
||||
if (!headers_sent()) {
|
||||
|
|
Loading…
Reference in a new issue