Refactoring RequestHandlerComponent::mapType() and added response object property to the component

This commit is contained in:
José Lorenzo Rodríguez 2010-08-02 19:07:26 -04:30
parent 040740c05d
commit 4fd032bc10
3 changed files with 61 additions and 85 deletions

View file

@ -507,7 +507,7 @@ class CakeResponse {
* e.g `type(array('jpg' => 'text/plain'));` * e.g `type(array('jpg' => 'text/plain'));`
* *
* @param string $contentType * @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) { public function type($contentType = null) {
if (is_null($contentType)) { if (is_null($contentType)) {
@ -523,9 +523,52 @@ class CakeResponse {
$contentType = $this->_mimeTypes[$contentType]; $contentType = $this->_mimeTypes[$contentType];
$contentType = is_array($contentType) ? current($contentType) : $contentType; $contentType = is_array($contentType) ? current($contentType) : $contentType;
} }
if (strpos($contentType, '/') === false) {
return false;
}
return $this->_contentType = $contentType; 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 * Sets the response charset
* if $charset is null the current charset is returned * if $charset is null the current charset is returned

View file

@ -57,7 +57,7 @@ class RequestHandlerComponent extends Object {
private $__responseTypeSet = null; private $__responseTypeSet = null;
/** /**
* Holds the copy of Controller::$request * Holds the reference to Controller::$request
* *
* @var CakeRequest * @var CakeRequest
* @access public * @access public
@ -65,42 +65,12 @@ class RequestHandlerComponent extends Object {
public $request; public $request;
/** /**
* Friendly content-type mappings used to set response types and determine * Holds the reference to Controller::$response
* request types. Can be modified with RequestHandler::setContent()
* *
* @var array * @var CakeResponse
* @access private * @access public
* @see RequestHandlerComponent::setContent
*/ */
protected $_contentTypeMap = array( public $response;
'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'
);
/** /**
* The template to use when rendering the given content type. * The template to use when rendering the given content type.
@ -132,6 +102,7 @@ class RequestHandlerComponent extends Object {
*/ */
public function initialize(&$controller, $settings = array()) { public function initialize(&$controller, $settings = array()) {
$this->request = $controller->request; $this->request = $controller->request;
$this->response = $controller->response;
if (isset($controller->params['url']['ext'])) { if (isset($controller->params['url']['ext'])) {
$this->ext = $controller->params['url']['ext']; $this->ext = $controller->params['url']['ext'];
} }
@ -359,7 +330,7 @@ class RequestHandlerComponent extends Object {
* @return void * @return void
*/ */
public function setContent($name, $type = null) { 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 * 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. * @return mixed Aliases for the types provided.
*/ */
public function mapType($ctype) { public function mapType($cType) {
if (is_array($ctype)) { return $this->response->mapType($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;
} }
/** /**

View file

@ -19,6 +19,7 @@
*/ */
App::import('Controller', 'Controller', false); App::import('Controller', 'Controller', false);
App::import('Component', array('RequestHandler')); App::import('Component', array('RequestHandler'));
App::import('Core', array('CakeRequest', 'CakeResponse'));
/** /**
* RequestHandlerTestController class * RequestHandlerTestController class
@ -44,20 +45,6 @@ class RequestHandlerTestController extends Controller {
*/ */
public $uses = null; 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 * test method for ajax redirection
* *
@ -106,20 +93,6 @@ class RequestHandlerTestDisabledController extends Controller {
*/ */
public $uses = null; 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 * beforeFilter method
* *
@ -155,12 +128,12 @@ class RequestHandlerComponentTest extends CakeTestCase {
public $RequestHandler; public $RequestHandler;
/** /**
* startTest method * setUp method
* *
* @access public * @access public
* @return void * @return void
*/ */
function startTest() { function setUp() {
$this->_server = $_SERVER; $this->_server = $_SERVER;
$this->_init(); $this->_init();
} }
@ -173,9 +146,11 @@ class RequestHandlerComponentTest extends CakeTestCase {
*/ */
function _init() { function _init() {
$request = new CakeRequest('controller_posts/index'); $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 = new RequestHandlerComponent();
$this->RequestHandler->request = $request; $this->RequestHandler->request = $request;
$this->RequestHandler->response = $response;
} }
/** /**
@ -184,7 +159,7 @@ class RequestHandlerComponentTest extends CakeTestCase {
* @access public * @access public
* @return void * @return void
*/ */
function endTest() { function tearDown() {
unset($this->RequestHandler); unset($this->RequestHandler);
unset($this->Controller); unset($this->Controller);
if (!headers_sent()) { if (!headers_sent()) {