mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Add viewClass map method to RequestHandler component, to map content types to viewclass.
This commit is contained in:
parent
e2e42ee185
commit
21431cba64
1 changed files with 42 additions and 2 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue