From 21431cba64a05e2098b9e9be42f177c6491dc498 Mon Sep 17 00:00:00 2001 From: Ceeram Date: Fri, 10 Aug 2012 09:54:22 +0200 Subject: [PATCH] Add viewClass map method to RequestHandler component, to map content types to viewclass. --- .../Component/RequestHandlerComponent.php | 44 ++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Controller/Component/RequestHandlerComponent.php b/lib/Cake/Controller/Component/RequestHandlerComponent.php index d6a182b7e..ee90814b3 100644 --- a/lib/Cake/Controller/Component/RequestHandlerComponent.php +++ b/lib/Cake/Controller/Component/RequestHandlerComponent.php @@ -89,6 +89,17 @@ class RequestHandlerComponent extends Component { 'json' => array('json_decode', true) ); +/** + * A mapping between type and viewClass + * By default only JSON and XML are mapped, use RequestHandlerComponent::viewClassMap() + * + * @var array + */ + protected $_viewClassMap = array( + 'json' => 'Json', + 'xml' => 'Xml' + ); + /** * Constructor. Parses the accepted content types accepted by the client using HTTP_ACCEPT * @@ -125,6 +136,9 @@ class RequestHandlerComponent extends Component { } $this->params = $controller->params; $this->_set($settings); + if (!empty($settings['viewClassMap'])) { + $this->viewClassMap($settings['viewClassMap']); + } } /** @@ -581,10 +595,16 @@ class RequestHandlerComponent extends Component { } $controller->ext = '.ctp'; - $viewClass = Inflector::classify($type); + $pluginDot = null; + $viewClassMap = $this->viewClassMap(); + if (array_key_exists($type, $viewClassMap)) { + list($pluginDot, $viewClass) = pluginSplit($viewClassMap[$type], true); + } else { + $viewClass = Inflector::classify($type); + } $viewName = $viewClass . 'View'; if (!class_exists($viewName)) { - App::uses($viewName, 'View'); + App::uses($viewName, $pluginDot . 'View'); } if (class_exists($viewName)) { $controller->viewClass = $viewClass; @@ -728,4 +748,24 @@ class RequestHandlerComponent extends Component { $this->_inputTypeMap[$type] = $handler; } +/** + * Getter/setter for viewClassMap + * + * @param array|string $type The type string or array with format `array('type' => 'viewClass')` to map one or more + * @param array $viewClass The viewClass to be used for the type without `View` appended + * @return array]string Returns viewClass when only string $type is set, else array with viewClassMap + */ + public function viewClassMap($type = null, $viewClass = null) { + if (!$viewClass && is_string($type) && isset($this->_viewClassMap[$type])) { + return $this->_viewClassMap[$type]; + } elseif (is_string($type)) { + $this->_viewClassMap[$type] = $viewClass; + } elseif (is_array($type)) { + foreach ($type as $key => $value) { + $this->viewClassMap($key, $value); + } + } + return $this->_viewClassMap; + } + }