From 4fd032bc1036d210afe4a344ff83fb641af6b183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lorenzo=20Rodr=C3=ADguez?= Date: Mon, 2 Aug 2010 19:07:26 -0430 Subject: [PATCH] Refactoring RequestHandlerComponent::mapType() and added response object property to the component --- cake/libs/cake_response.php | 45 +++++++++++++- .../controller/components/request_handler.php | 62 +++---------------- .../components/request_handler.test.php | 39 +++--------- 3 files changed, 61 insertions(+), 85 deletions(-) diff --git a/cake/libs/cake_response.php b/cake/libs/cake_response.php index 0bebf3ad9..66d3914a8 100644 --- a/cake/libs/cake_response.php +++ b/cake/libs/cake_response.php @@ -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 diff --git a/cake/libs/controller/components/request_handler.php b/cake/libs/controller/components/request_handler.php index 8afe5a1c1..ff3411b36 100644 --- a/cake/libs/controller/components/request_handler.php +++ b/cake/libs/controller/components/request_handler.php @@ -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); } /** diff --git a/cake/tests/cases/libs/controller/components/request_handler.test.php b/cake/tests/cases/libs/controller/components/request_handler.test.php index b929793da..86c2513a4 100644 --- a/cake/tests/cases/libs/controller/components/request_handler.test.php +++ b/cake/tests/cases/libs/controller/components/request_handler.test.php @@ -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()) {