Skip to content
Permalink
Browse files

Add viewClass map method to RequestHandler component, to map content …

…types to viewclass.
  • Loading branch information...
ceeram committed Aug 10, 2012
1 parent e2e42ee commit 21431cba64a05e2098b9e9be42f177c6491dc498
Showing with 42 additions and 2 deletions.
  1. +42 −2 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 @@ public function initialize(Controller $controller, $settings = array()) {
}
$this->params = $controller->params;
$this->_set($settings);
if (!empty($settings['viewClassMap'])) {
$this->viewClassMap($settings['viewClassMap']);
}
}
/**
@@ -581,10 +595,16 @@ public function renderAs(Controller $controller, $type, $options = array()) {
}
$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 @@ public function addInputType($type, $handler) {
$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;
}
}

0 comments on commit 21431cb

Please sign in to comment.
You can’t perform that action at this time.