1: <?php
2: /**
3: * Request object for handling alternative HTTP requests
4: *
5: * Alternative HTTP requests can come from wireless units like mobile phones, palmtop computers,
6: * and the like. These units have no use for Ajax requests, and this Component can tell how Cake
7: * should respond to the different needs of a handheld computer and a desktop machine.
8: *
9: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
10: * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11: *
12: * Licensed under The MIT License
13: * For full copyright and license information, please see the LICENSE.txt
14: * Redistributions of files must retain the above copyright notice.
15: *
16: * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
17: * @link http://cakephp.org CakePHP(tm) Project
18: * @package Cake.Controller.Component
19: * @since CakePHP(tm) v 0.10.4.1076
20: * @license http://www.opensource.org/licenses/mit-license.php MIT License
21: */
22:
23: App::uses('Component', 'Controller');
24: App::uses('Xml', 'Utility');
25:
26: /**
27: * Request object for handling alternative HTTP requests
28: *
29: * Alternative HTTP requests can come from wireless units like mobile phones, palmtop computers,
30: * and the like. These units have no use for Ajax requests, and this Component can tell how Cake
31: * should respond to the different needs of a handheld computer and a desktop machine.
32: *
33: * @package Cake.Controller.Component
34: * @link http://book.cakephp.org/2.0/en/core-libraries/components/request-handling.html
35: */
36: class RequestHandlerComponent extends Component {
37:
38: /**
39: * The layout that will be switched to for Ajax requests
40: *
41: * @var string
42: * @see RequestHandler::setAjax()
43: */
44: public $ajaxLayout = 'ajax';
45:
46: /**
47: * Determines whether or not callbacks will be fired on this component
48: *
49: * @var bool
50: */
51: public $enabled = true;
52:
53: /**
54: * Holds the reference to Controller::$request
55: *
56: * @var CakeRequest
57: */
58: public $request;
59:
60: /**
61: * Holds the reference to Controller::$response
62: *
63: * @var CakeResponse
64: */
65: public $response;
66:
67: /**
68: * Contains the file extension parsed out by the Router
69: *
70: * @var string
71: * @see Router::parseExtensions()
72: */
73: public $ext = null;
74:
75: /**
76: * The template to use when rendering the given content type.
77: *
78: * @var string
79: */
80: protected $_renderType = null;
81:
82: /**
83: * A mapping between extensions and deserializers for request bodies of that type.
84: * By default only JSON and XML are mapped, use RequestHandlerComponent::addInputType()
85: *
86: * @var array
87: */
88: protected $_inputTypeMap = array(
89: 'json' => array('json_decode', true)
90: );
91:
92: /**
93: * A mapping between type and viewClass
94: * By default only JSON and XML are mapped, use RequestHandlerComponent::viewClassMap()
95: *
96: * @var array
97: */
98: protected $_viewClassMap = array(
99: 'json' => 'Json',
100: 'xml' => 'Xml'
101: );
102:
103: /**
104: * Constructor. Parses the accepted content types accepted by the client using HTTP_ACCEPT
105: *
106: * @param ComponentCollection $collection ComponentCollection object.
107: * @param array $settings Array of settings.
108: */
109: public function __construct(ComponentCollection $collection, $settings = array()) {
110: parent::__construct($collection, $settings + array('checkHttpCache' => true));
111: $this->addInputType('xml', array(array($this, 'convertXml')));
112:
113: $Controller = $collection->getController();
114: $this->request = $Controller->request;
115: $this->response = $Controller->response;
116: }
117:
118: /**
119: * Checks to see if a file extension has been parsed by the Router, or if the
120: * HTTP_ACCEPT_TYPE has matches only one content type with the supported extensions.
121: * If there is only one matching type between the supported content types & extensions,
122: * and the requested mime-types, RequestHandler::$ext is set to that value.
123: *
124: * @param Controller $controller A reference to the controller
125: * @return void
126: * @see Router::parseExtensions()
127: */
128: public function initialize(Controller $controller) {
129: if (isset($this->request->params['ext'])) {
130: $this->ext = $this->request->params['ext'];
131: }
132: if (empty($this->ext) || $this->ext === 'html') {
133: $this->_setExtension();
134: }
135: $this->params = $controller->params;
136: if (!empty($this->settings['viewClassMap'])) {
137: $this->viewClassMap($this->settings['viewClassMap']);
138: }
139: }
140:
141: /**
142: * Set the extension based on the accept headers.
143: * Compares the accepted types and configured extensions.
144: * If there is one common type, that is assigned as the ext/content type
145: * for the response.
146: * Type with the highest weight will be set. If the highest weight has more
147: * then one type matching the extensions, the order in which extensions are specified
148: * determines which type will be set.
149: *
150: * If html is one of the preferred types, no content type will be set, this
151: * is to avoid issues with browsers that prefer html and several other content types.
152: *
153: * @return void
154: */
155: protected function _setExtension() {
156: $accept = $this->request->parseAccept();
157: if (empty($accept)) {
158: return;
159: }
160:
161: $accepts = $this->response->mapType($accept);
162: $preferedTypes = current($accepts);
163: if (array_intersect($preferedTypes, array('html', 'xhtml'))) {
164: return;
165: }
166:
167: $extensions = Router::extensions();
168: foreach ($accepts as $types) {
169: $ext = array_intersect($extensions, $types);
170: if ($ext) {
171: $this->ext = current($ext);
172: break;
173: }
174: }
175: }
176:
177: /**
178: * The startup method of the RequestHandler enables several automatic behaviors
179: * related to the detection of certain properties of the HTTP request, including:
180: *
181: * - Disabling layout rendering for Ajax requests (based on the HTTP_X_REQUESTED_WITH header)
182: * - If Router::parseExtensions() is enabled, the layout and template type are
183: * switched based on the parsed extension or Accept-Type header. For example, if `controller/action.xml`
184: * is requested, the view path becomes `app/View/Controller/xml/action.ctp`. Also if
185: * `controller/action` is requested with `Accept-Type: application/xml` in the headers
186: * the view path will become `app/View/Controller/xml/action.ctp`. Layout and template
187: * types will only switch to mime-types recognized by CakeResponse. If you need to declare
188: * additional mime-types, you can do so using CakeResponse::type() in your controllers beforeFilter()
189: * method.
190: * - If a helper with the same name as the extension exists, it is added to the controller.
191: * - If the extension is of a type that RequestHandler understands, it will set that
192: * Content-type in the response header.
193: * - If the XML data is POSTed, the data is parsed into an XML object, which is assigned
194: * to the $data property of the controller, which can then be saved to a model object.
195: *
196: * @param Controller $controller A reference to the controller
197: * @return void
198: */
199: public function startup(Controller $controller) {
200: $controller->request->params['isAjax'] = $this->request->is('ajax');
201: $isRecognized = (
202: !in_array($this->ext, array('html', 'htm')) &&
203: $this->response->getMimeType($this->ext)
204: );
205:
206: if (!empty($this->ext) && $isRecognized) {
207: $this->renderAs($controller, $this->ext);
208: } elseif ($this->request->is('ajax')) {
209: $this->renderAs($controller, 'ajax');
210: } elseif (empty($this->ext) || in_array($this->ext, array('html', 'htm'))) {
211: $this->respondAs('html', array('charset' => Configure::read('App.encoding')));
212: }
213:
214: foreach ($this->_inputTypeMap as $type => $handler) {
215: if ($this->requestedWith($type)) {
216: $input = call_user_func_array(array($controller->request, 'input'), $handler);
217: $controller->request->data = $input;
218: }
219: }
220: }
221:
222: /**
223: * Helper method to parse xml input data, due to lack of anonymous functions
224: * this lives here.
225: *
226: * @param string $xml XML string.
227: * @return array Xml array data
228: */
229: public function convertXml($xml) {
230: try {
231: $xml = Xml::build($xml, array('readFile' => false));
232: if (isset($xml->data)) {
233: return Xml::toArray($xml->data);
234: }
235: return Xml::toArray($xml);
236: } catch (XmlException $e) {
237: return array();
238: }
239: }
240:
241: /**
242: * Handles (fakes) redirects for Ajax requests using requestAction()
243: * Modifies the $_POST and $_SERVER['REQUEST_METHOD'] to simulate a new GET request.
244: *
245: * @param Controller $controller A reference to the controller
246: * @param string|array $url A string or array containing the redirect location
247: * @param int|array $status HTTP Status for redirect
248: * @param bool $exit Whether to exit script, defaults to `true`.
249: * @return void
250: */
251: public function beforeRedirect(Controller $controller, $url, $status = null, $exit = true) {
252: if (!$this->request->is('ajax')) {
253: return;
254: }
255: if (empty($url)) {
256: return;
257: }
258: $_SERVER['REQUEST_METHOD'] = 'GET';
259: foreach ($_POST as $key => $val) {
260: unset($_POST[$key]);
261: }
262: if (is_array($url)) {
263: $url = Router::url($url + array('base' => false));
264: }
265: if (!empty($status)) {
266: $statusCode = $this->response->httpCodes($status);
267: $code = key($statusCode);
268: $this->response->statusCode($code);
269: }
270: $this->response->body($this->requestAction($url, array('return', 'bare' => false)));
271: $this->response->send();
272: $this->_stop();
273: }
274:
275: /**
276: * Checks if the response can be considered different according to the request
277: * headers, and the caching response headers. If it was not modified, then the
278: * render process is skipped. And the client will get a blank response with a
279: * "304 Not Modified" header.
280: *
281: * @param Controller $controller Controller instance.
282: * @return bool False if the render process should be aborted.
283: */
284: public function beforeRender(Controller $controller) {
285: if ($this->settings['checkHttpCache'] && $this->response->checkNotModified($this->request)) {
286: return false;
287: }
288: }
289:
290: /**
291: * Returns true if the current HTTP request is Ajax, false otherwise
292: *
293: * @return bool True if call is Ajax
294: * @deprecated 3.0.0 Use `$this->request->is('ajax')` instead.
295: */
296: public function isAjax() {
297: return $this->request->is('ajax');
298: }
299:
300: /**
301: * Returns true if the current HTTP request is coming from a Flash-based client
302: *
303: * @return bool True if call is from Flash
304: * @deprecated 3.0.0 Use `$this->request->is('flash')` instead.
305: */
306: public function isFlash() {
307: return $this->request->is('flash');
308: }
309:
310: /**
311: * Returns true if the current request is over HTTPS, false otherwise.
312: *
313: * @return bool True if call is over HTTPS
314: * @deprecated 3.0.0 Use `$this->request->is('ssl')` instead.
315: */
316: public function isSSL() {
317: return $this->request->is('ssl');
318: }
319:
320: /**
321: * Returns true if the current call accepts an XML response, false otherwise
322: *
323: * @return bool True if client accepts an XML response
324: */
325: public function isXml() {
326: return $this->prefers('xml');
327: }
328:
329: /**
330: * Returns true if the current call accepts an RSS response, false otherwise
331: *
332: * @return bool True if client accepts an RSS response
333: */
334: public function isRss() {
335: return $this->prefers('rss');
336: }
337:
338: /**
339: * Returns true if the current call accepts an Atom response, false otherwise
340: *
341: * @return bool True if client accepts an RSS response
342: */
343: public function isAtom() {
344: return $this->prefers('atom');
345: }
346:
347: /**
348: * Returns true if user agent string matches a mobile web browser, or if the
349: * client accepts WAP content.
350: *
351: * @return bool True if user agent is a mobile web browser
352: */
353: public function isMobile() {
354: return $this->request->is('mobile') || $this->accepts('wap');
355: }
356:
357: /**
358: * Returns true if the client accepts WAP content
359: *
360: * @return bool
361: */
362: public function isWap() {
363: return $this->prefers('wap');
364: }
365:
366: /**
367: * Returns true if the current call a POST request
368: *
369: * @return bool True if call is a POST
370: * @deprecated 3.0.0 Use $this->request->is('post'); from your controller.
371: */
372: public function isPost() {
373: return $this->request->is('post');
374: }
375:
376: /**
377: * Returns true if the current call a PUT request
378: *
379: * @return bool True if call is a PUT
380: * @deprecated 3.0.0 Use $this->request->is('put'); from your controller.
381: */
382: public function isPut() {
383: return $this->request->is('put');
384: }
385:
386: /**
387: * Returns true if the current call a GET request
388: *
389: * @return bool True if call is a GET
390: * @deprecated 3.0.0 Use $this->request->is('get'); from your controller.
391: */
392: public function isGet() {
393: return $this->request->is('get');
394: }
395:
396: /**
397: * Returns true if the current call a DELETE request
398: *
399: * @return bool True if call is a DELETE
400: * @deprecated 3.0.0 Use $this->request->is('delete'); from your controller.
401: */
402: public function isDelete() {
403: return $this->request->is('delete');
404: }
405:
406: /**
407: * Gets Prototype version if call is Ajax, otherwise empty string.
408: * The Prototype library sets a special "Prototype version" HTTP header.
409: *
410: * @return string|bool When Ajax the prototype version of component making the call otherwise false
411: */
412: public function getAjaxVersion() {
413: $httpX = env('HTTP_X_PROTOTYPE_VERSION');
414: return ($httpX === null) ? false : $httpX;
415: }
416:
417: /**
418: * Adds/sets the Content-type(s) for the given name. This method allows
419: * content-types to be mapped to friendly aliases (or extensions), which allows
420: * RequestHandler to automatically respond to requests of that type in the
421: * startup method.
422: *
423: * @param string $name The name of the Content-type, i.e. "html", "xml", "css"
424: * @param string|array $type The Content-type or array of Content-types assigned to the name,
425: * i.e. "text/html", or "application/xml"
426: * @return void
427: * @deprecated 3.0.0 Use `$this->response->type()` instead.
428: */
429: public function setContent($name, $type = null) {
430: $this->response->type(array($name => $type));
431: }
432:
433: /**
434: * Gets the server name from which this request was referred
435: *
436: * @return string Server address
437: * @deprecated 3.0.0 Use $this->request->referer() from your controller instead
438: */
439: public function getReferer() {
440: return $this->request->referer(false);
441: }
442:
443: /**
444: * Gets remote client IP
445: *
446: * @param bool $safe Use safe = false when you think the user might manipulate
447: * their HTTP_CLIENT_IP header. Setting $safe = false will also look at HTTP_X_FORWARDED_FOR
448: * @return string Client IP address
449: * @deprecated 3.0.0 Use $this->request->clientIp() from your, controller instead.
450: */
451: public function getClientIP($safe = true) {
452: return $this->request->clientIp($safe);
453: }
454:
455: /**
456: * Determines which content types the client accepts. Acceptance is based on
457: * the file extension parsed by the Router (if present), and by the HTTP_ACCEPT
458: * header. Unlike CakeRequest::accepts() this method deals entirely with mapped content types.
459: *
460: * Usage:
461: *
462: * `$this->RequestHandler->accepts(array('xml', 'html', 'json'));`
463: *
464: * Returns true if the client accepts any of the supplied types.
465: *
466: * `$this->RequestHandler->accepts('xml');`
467: *
468: * Returns true if the client accepts xml.
469: *
470: * @param string|array $type Can be null (or no parameter), a string type name, or an
471: * array of types
472: * @return mixed If null or no parameter is passed, returns an array of content
473: * types the client accepts. If a string is passed, returns true
474: * if the client accepts it. If an array is passed, returns true
475: * if the client accepts one or more elements in the array.
476: * @see RequestHandlerComponent::setContent()
477: */
478: public function accepts($type = null) {
479: $accepted = $this->request->accepts();
480:
481: if (!$type) {
482: return $this->mapType($accepted);
483: }
484: if (is_array($type)) {
485: foreach ($type as $t) {
486: $t = $this->mapAlias($t);
487: if (in_array($t, $accepted)) {
488: return true;
489: }
490: }
491: return false;
492: }
493: if (is_string($type)) {
494: return in_array($this->mapAlias($type), $accepted);
495: }
496: return false;
497: }
498:
499: /**
500: * Determines the content type of the data the client has sent (i.e. in a POST request)
501: *
502: * @param string|array $type Can be null (or no parameter), a string type name, or an array of types
503: * @return mixed If a single type is supplied a boolean will be returned. If no type is provided
504: * The mapped value of CONTENT_TYPE will be returned. If an array is supplied the first type
505: * in the request content type will be returned.
506: */
507: public function requestedWith($type = null) {
508: if (
509: !$this->request->is('patch') &&
510: !$this->request->is('post') &&
511: !$this->request->is('put') &&
512: !$this->request->is('delete')
513: ) {
514: return null;
515: }
516: if (is_array($type)) {
517: foreach ($type as $t) {
518: if ($this->requestedWith($t)) {
519: return $t;
520: }
521: }
522: return false;
523: }
524:
525: list($contentType) = explode(';', env('CONTENT_TYPE'));
526: if ($contentType === '') {
527: list($contentType) = explode(';', CakeRequest::header('CONTENT_TYPE'));
528: }
529: if (!$type) {
530: return $this->mapType($contentType);
531: }
532: if (is_string($type)) {
533: return ($type === $this->mapType($contentType));
534: }
535: }
536:
537: /**
538: * Determines which content-types the client prefers. If no parameters are given,
539: * the single content-type that the client most likely prefers is returned. If $type is
540: * an array, the first item in the array that the client accepts is returned.
541: * Preference is determined primarily by the file extension parsed by the Router
542: * if provided, and secondarily by the list of content-types provided in
543: * HTTP_ACCEPT.
544: *
545: * @param string|array $type An optional array of 'friendly' content-type names, i.e.
546: * 'html', 'xml', 'js', etc.
547: * @return mixed If $type is null or not provided, the first content-type in the
548: * list, based on preference, is returned. If a single type is provided
549: * a boolean will be returned if that type is preferred.
550: * If an array of types are provided then the first preferred type is returned.
551: * If no type is provided the first preferred type is returned.
552: * @see RequestHandlerComponent::setContent()
553: */
554: public function prefers($type = null) {
555: $acceptRaw = $this->request->parseAccept();
556:
557: if (empty($acceptRaw)) {
558: return $this->ext;
559: }
560: $accepts = $this->mapType(array_shift($acceptRaw));
561:
562: if (!$type) {
563: if (empty($this->ext) && !empty($accepts)) {
564: return $accepts[0];
565: }
566: return $this->ext;
567: }
568:
569: $types = (array)$type;
570:
571: if (count($types) === 1) {
572: if (!empty($this->ext)) {
573: return in_array($this->ext, $types);
574: }
575: return in_array($types[0], $accepts);
576: }
577:
578: $intersect = array_values(array_intersect($accepts, $types));
579: if (empty($intersect)) {
580: return false;
581: }
582: return $intersect[0];
583: }
584:
585: /**
586: * Sets the layout and template paths for the content type defined by $type.
587: *
588: * ### Usage:
589: *
590: * Render the response as an 'ajax' response.
591: *
592: * `$this->RequestHandler->renderAs($this, 'ajax');`
593: *
594: * Render the response as an xml file and force the result as a file download.
595: *
596: * `$this->RequestHandler->renderAs($this, 'xml', array('attachment' => 'myfile.xml');`
597: *
598: * @param Controller $controller A reference to a controller object
599: * @param string $type Type of response to send (e.g: 'ajax')
600: * @param array $options Array of options to use
601: * @return void
602: * @see RequestHandlerComponent::setContent()
603: * @see RequestHandlerComponent::respondAs()
604: */
605: public function renderAs(Controller $controller, $type, $options = array()) {
606: $defaults = array('charset' => 'UTF-8');
607:
608: if (Configure::read('App.encoding') !== null) {
609: $defaults['charset'] = Configure::read('App.encoding');
610: }
611: $options += $defaults;
612:
613: if ($type === 'ajax') {
614: $controller->layout = $this->ajaxLayout;
615: return $this->respondAs('html', $options);
616: }
617:
618: $pluginDot = null;
619: $viewClassMap = $this->viewClassMap();
620: if (array_key_exists($type, $viewClassMap)) {
621: list($pluginDot, $viewClass) = pluginSplit($viewClassMap[$type], true);
622: } else {
623: $viewClass = Inflector::classify($type);
624: }
625: $viewName = $viewClass . 'View';
626: if (!class_exists($viewName)) {
627: App::uses($viewName, $pluginDot . 'View');
628: }
629: if (class_exists($viewName)) {
630: $controller->viewClass = $viewClass;
631: } elseif (empty($this->_renderType)) {
632: $controller->viewPath .= DS . $type;
633: } else {
634: $controller->viewPath = preg_replace(
635: "/([\/\\\\]{$this->_renderType})$/",
636: DS . $type,
637: $controller->viewPath
638: );
639: }
640: $this->_renderType = $type;
641: $controller->layoutPath = $type;
642:
643: if ($this->response->getMimeType($type)) {
644: $this->respondAs($type, $options);
645: }
646:
647: $helper = ucfirst($type);
648:
649: if (!in_array($helper, $controller->helpers) && empty($controller->helpers[$helper])) {
650: App::uses('AppHelper', 'View/Helper');
651: App::uses($helper . 'Helper', 'View/Helper');
652: if (class_exists($helper . 'Helper')) {
653: $controller->helpers[] = $helper;
654: }
655: }
656: }
657:
658: /**
659: * Sets the response header based on type map index name. This wraps several methods
660: * available on CakeResponse. It also allows you to use Content-Type aliases.
661: *
662: * @param string|array $type Friendly type name, i.e. 'html' or 'xml', or a full content-type,
663: * like 'application/x-shockwave'.
664: * @param array $options If $type is a friendly type name that is associated with
665: * more than one type of content, $index is used to select which content-type to use.
666: * @return bool Returns false if the friendly type name given in $type does
667: * not exist in the type map, or if the Content-type header has
668: * already been set by this method.
669: * @see RequestHandlerComponent::setContent()
670: */
671: public function respondAs($type, $options = array()) {
672: $defaults = array('index' => null, 'charset' => null, 'attachment' => false);
673: $options = $options + $defaults;
674:
675: $cType = $type;
676: if (strpos($type, '/') === false) {
677: $cType = $this->response->getMimeType($type);
678: }
679: if (is_array($cType)) {
680: if (isset($cType[$options['index']])) {
681: $cType = $cType[$options['index']];
682: }
683:
684: if ($this->prefers($cType)) {
685: $cType = $this->prefers($cType);
686: } else {
687: $cType = $cType[0];
688: }
689: }
690:
691: if (!$type) {
692: return false;
693: }
694: if (empty($this->request->params['requested'])) {
695: $this->response->type($cType);
696: }
697: if (!empty($options['charset'])) {
698: $this->response->charset($options['charset']);
699: }
700: if (!empty($options['attachment'])) {
701: $this->response->download($options['attachment']);
702: }
703: return true;
704: }
705:
706: /**
707: * Returns the current response type (Content-type header), or null if not alias exists
708: *
709: * @return mixed A string content type alias, or raw content type if no alias map exists,
710: * otherwise null
711: */
712: public function responseType() {
713: return $this->mapType($this->response->type());
714: }
715:
716: /**
717: * Maps a content-type back to an alias
718: *
719: * @param string|array $cType Either a string content type to map, or an array of types.
720: * @return string|array Aliases for the types provided.
721: * @deprecated 3.0.0 Use $this->response->mapType() in your controller instead.
722: */
723: public function mapType($cType) {
724: return $this->response->mapType($cType);
725: }
726:
727: /**
728: * Maps a content type alias back to its mime-type(s)
729: *
730: * @param string|array $alias String alias to convert back into a content type. Or an array of aliases to map.
731: * @return string|null Null on an undefined alias. String value of the mapped alias type. If an
732: * alias maps to more than one content type, the first one will be returned.
733: */
734: public function mapAlias($alias) {
735: if (is_array($alias)) {
736: return array_map(array($this, 'mapAlias'), $alias);
737: }
738: $type = $this->response->getMimeType($alias);
739: if ($type) {
740: if (is_array($type)) {
741: return $type[0];
742: }
743: return $type;
744: }
745: return null;
746: }
747:
748: /**
749: * Add a new mapped input type. Mapped input types are automatically
750: * converted by RequestHandlerComponent during the startup() callback.
751: *
752: * @param string $type The type alias being converted, ie. json
753: * @param array $handler The handler array for the type. The first index should
754: * be the handling callback, all other arguments should be additional parameters
755: * for the handler.
756: * @return void
757: * @throws CakeException
758: */
759: public function addInputType($type, $handler) {
760: if (!is_array($handler) || !isset($handler[0]) || !is_callable($handler[0])) {
761: throw new CakeException(__d('cake_dev', 'You must give a handler callback.'));
762: }
763: $this->_inputTypeMap[$type] = $handler;
764: }
765:
766: /**
767: * Getter/setter for viewClassMap
768: *
769: * @param array|string $type The type string or array with format `array('type' => 'viewClass')` to map one or more
770: * @param array $viewClass The viewClass to be used for the type without `View` appended
771: * @return array|string Returns viewClass when only string $type is set, else array with viewClassMap
772: */
773: public function viewClassMap($type = null, $viewClass = null) {
774: if (!$viewClass && is_string($type) && isset($this->_viewClassMap[$type])) {
775: return $this->_viewClassMap[$type];
776: }
777: if (is_string($type)) {
778: $this->_viewClassMap[$type] = $viewClass;
779: } elseif (is_array($type)) {
780: foreach ($type as $key => $value) {
781: $this->viewClassMap($key, $value);
782: }
783: }
784: return $this->_viewClassMap;
785: }
786:
787: }
788: