RequestHandlerComponent.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 0.10.4
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Controller\Component;
  16. use Cake\Controller\Component;
  17. use Cake\Controller\ComponentRegistry;
  18. use Cake\Controller\Controller;
  19. use Cake\Core\App;
  20. use Cake\Core\Configure;
  21. use Cake\Core\Exception\Exception;
  22. use Cake\Event\Event;
  23. use Cake\Http\Response;
  24. use Cake\Routing\Router;
  25. use Cake\Utility\Exception\XmlException;
  26. use Cake\Utility\Inflector;
  27. use Cake\Utility\Xml;
  28. use RuntimeException;
  29. /**
  30. * Request object for handling alternative HTTP requests
  31. *
  32. * Alternative HTTP requests can come from wireless units like mobile phones, palmtop computers,
  33. * and the like. These units have no use for AJAX requests, and this Component can tell how Cake
  34. * should respond to the different needs of a handheld computer and a desktop machine.
  35. *
  36. * @link https://book.cakephp.org/3.0/en/controllers/components/request-handling.html
  37. */
  38. class RequestHandlerComponent extends Component
  39. {
  40. /**
  41. * @var bool
  42. * @deprecated 3.4.0 Unused. Will be removed in 4.0.0
  43. */
  44. public $enabled = true;
  45. /**
  46. * Contains the file extension parsed out by the Router
  47. *
  48. * @var string|null
  49. * @see \Cake\Routing\Router::extensions()
  50. */
  51. public $ext;
  52. /**
  53. * The template to use when rendering the given content type.
  54. *
  55. * @var string|null
  56. */
  57. protected $_renderType;
  58. /**
  59. * Default config
  60. *
  61. * These are merged with user-provided config when the component is used.
  62. *
  63. * - `checkHttpCache` - Whether to check for HTTP cache.
  64. * - `viewClassMap` - Mapping between type and view classes. If undefined
  65. * json, xml, and ajax will be mapped. Defining any types will omit the defaults.
  66. * - `inputTypeMap` - A mapping between types and deserializers for request bodies.
  67. * If undefined json & xml will be mapped. Defining any types will omit the defaults.
  68. * - `enableBeforeRedirect` - Set to false to disable the `beforeRedirect` callback. The
  69. * `beforeRedirect` functionality has been deprecated.
  70. *
  71. * @var array
  72. */
  73. protected $_defaultConfig = [
  74. 'checkHttpCache' => true,
  75. 'viewClassMap' => [],
  76. 'inputTypeMap' => [],
  77. 'enableBeforeRedirect' => true
  78. ];
  79. /**
  80. * Set the layout to be used when rendering the AuthComponent's ajaxLogin element.
  81. *
  82. * @var string
  83. * @deprecated 3.3.11 This feature property is not supported and will
  84. * be removed in 4.0.0
  85. */
  86. public $ajaxLayout;
  87. /**
  88. * Constructor. Parses the accepted content types accepted by the client using HTTP_ACCEPT
  89. *
  90. * @param \Cake\Controller\ComponentRegistry $registry ComponentRegistry object.
  91. * @param array $config Array of config.
  92. */
  93. public function __construct(ComponentRegistry $registry, array $config = [])
  94. {
  95. $config += [
  96. 'viewClassMap' => [
  97. 'json' => 'Json',
  98. 'xml' => 'Xml',
  99. 'ajax' => 'Ajax'
  100. ],
  101. 'inputTypeMap' => [
  102. 'json' => ['json_decode', true],
  103. 'xml' => [[$this, 'convertXml']],
  104. ]
  105. ];
  106. parent::__construct($registry, $config);
  107. }
  108. /**
  109. * Events supported by this component.
  110. *
  111. * @return array
  112. */
  113. public function implementedEvents()
  114. {
  115. return [
  116. 'Controller.startup' => 'startup',
  117. 'Controller.beforeRender' => 'beforeRender',
  118. 'Controller.beforeRedirect' => 'beforeRedirect',
  119. ];
  120. }
  121. /**
  122. * @param array $config The config data.
  123. * @return void
  124. * @deprecated 3.4.0 Unused. To be removed in 4.0.0
  125. */
  126. public function initialize(array $config)
  127. {
  128. }
  129. /**
  130. * Set the extension based on the accept headers.
  131. * Compares the accepted types and configured extensions.
  132. * If there is one common type, that is assigned as the ext/content type for the response.
  133. * The type with the highest weight will be set. If the highest weight has more
  134. * than one type matching the extensions, the order in which extensions are specified
  135. * determines which type will be set.
  136. *
  137. * If html is one of the preferred types, no content type will be set, this
  138. * is to avoid issues with browsers that prefer HTML and several other content types.
  139. *
  140. * @param \Cake\Http\ServerRequest $request The request instance.
  141. * @param \Cake\Http\Response $response The response instance.
  142. * @return void
  143. */
  144. protected function _setExtension($request, $response)
  145. {
  146. $accept = $request->parseAccept();
  147. if (empty($accept) || current($accept)[0] === 'text/html') {
  148. return;
  149. }
  150. $accepts = $response->mapType($accept);
  151. $preferredTypes = current($accepts);
  152. if (array_intersect($preferredTypes, ['html', 'xhtml'])) {
  153. return;
  154. }
  155. $extensions = array_unique(
  156. array_merge(Router::extensions(), array_keys($this->getConfig('viewClassMap')))
  157. );
  158. foreach ($accepts as $types) {
  159. $ext = array_intersect($extensions, $types);
  160. if ($ext) {
  161. $this->ext = current($ext);
  162. break;
  163. }
  164. }
  165. }
  166. /**
  167. * The startup method of the RequestHandler enables several automatic behaviors
  168. * related to the detection of certain properties of the HTTP request, including:
  169. *
  170. * If the XML data is POSTed, the data is parsed into an XML object, which is assigned
  171. * to the $data property of the controller, which can then be saved to a model object.
  172. *
  173. * @param \Cake\Event\Event $event The startup event that was fired.
  174. * @return void
  175. */
  176. public function startup(Event $event)
  177. {
  178. /** @var \Cake\Controller\Controller $controller */
  179. $controller = $event->getSubject();
  180. $request = $controller->request;
  181. $response = $controller->response;
  182. if ($request->getParam('_ext')) {
  183. $this->ext = $request->getParam('_ext');
  184. }
  185. if (!$this->ext || in_array($this->ext, ['html', 'htm'])) {
  186. $this->_setExtension($request, $response);
  187. }
  188. $isAjax = $request->is('ajax');
  189. $controller->request = $request->withParam('isAjax', $isAjax);
  190. if (!$this->ext && $isAjax) {
  191. $this->ext = 'ajax';
  192. }
  193. if ($request->is(['get', 'head', 'options'])) {
  194. return;
  195. }
  196. if ($request->getParsedBody() !== []) {
  197. return;
  198. }
  199. foreach ($this->getConfig('inputTypeMap') as $type => $handler) {
  200. if (!is_callable($handler[0])) {
  201. throw new RuntimeException(sprintf("Invalid callable for '%s' type.", $type));
  202. }
  203. if ($this->requestedWith($type)) {
  204. $input = $request->input(...$handler);
  205. $controller->request = $request->withParsedBody((array)$input);
  206. }
  207. }
  208. }
  209. /**
  210. * Helper method to parse xml input data, due to lack of anonymous functions
  211. * this lives here.
  212. *
  213. * @param string $xml XML string.
  214. * @return array Xml array data
  215. */
  216. public function convertXml($xml)
  217. {
  218. try {
  219. $xml = Xml::build($xml, ['return' => 'domdocument', 'readFile' => false]);
  220. // We might not get child nodes if there are nested inline entities.
  221. if ($xml->childNodes->length > 0) {
  222. return Xml::toArray($xml);
  223. }
  224. return [];
  225. } catch (XmlException $e) {
  226. return [];
  227. }
  228. }
  229. /**
  230. * Handles (fakes) redirects for AJAX requests using requestAction()
  231. *
  232. * @param \Cake\Event\Event $event The Controller.beforeRedirect event.
  233. * @param string|array $url A string or array containing the redirect location
  234. * @param \Cake\Http\Response $response The response object.
  235. * @return \Cake\Http\Response|null The response object if the redirect is caught.
  236. * @deprecated 3.3.5 This functionality will be removed in 4.0.0. You can disable this function
  237. * now by setting the `enableBeforeRedirect` config option to false.
  238. */
  239. public function beforeRedirect(Event $event, $url, Response $response)
  240. {
  241. if (!$this->getConfig('enableBeforeRedirect')) {
  242. return null;
  243. }
  244. deprecationWarning(
  245. 'RequestHandlerComponent::beforeRedirect() is deprecated. ' .
  246. 'This functionality will be removed in 4.0.0. Set the `enableBeforeRedirect` ' .
  247. 'option to `false` to disable this warning.'
  248. );
  249. $request = $this->request;
  250. if (!$request->is('ajax')) {
  251. return null;
  252. }
  253. if (empty($url)) {
  254. return null;
  255. }
  256. if (is_array($url)) {
  257. $url = Router::url($url + ['_base' => false]);
  258. }
  259. $query = [];
  260. if (strpos($url, '?') !== false) {
  261. list($url, $querystr) = explode('?', $url, 2);
  262. parse_str($querystr, $query);
  263. }
  264. /** @var \Cake\Controller\Controller $controller */
  265. $controller = $event->getSubject();
  266. $response->body($controller->requestAction($url, [
  267. 'return',
  268. 'bare' => false,
  269. 'environment' => [
  270. 'REQUEST_METHOD' => 'GET'
  271. ],
  272. 'query' => $query,
  273. 'cookies' => $request->getCookieParams()
  274. ]));
  275. return $response->withStatus(200);
  276. }
  277. /**
  278. * Checks if the response can be considered different according to the request
  279. * headers, and the caching response headers. If it was not modified, then the
  280. * render process is skipped. And the client will get a blank response with a
  281. * "304 Not Modified" header.
  282. *
  283. * - If Router::extensions() is enabled, the layout and template type are
  284. * switched based on the parsed extension or `Accept` header. For example,
  285. * if `controller/action.xml` is requested, the view path becomes
  286. * `app/View/Controller/xml/action.ctp`. Also if `controller/action` is
  287. * requested with `Accept: application/xml` in the headers the view
  288. * path will become `app/View/Controller/xml/action.ctp`. Layout and template
  289. * types will only switch to mime-types recognized by Cake\Http\Response.
  290. * If you need to declare additional mime-types, you can do so using
  291. * Cake\Http\Response::type() in your controller's beforeFilter() method.
  292. * - If a helper with the same name as the extension exists, it is added to
  293. * the controller.
  294. * - If the extension is of a type that RequestHandler understands, it will
  295. * set that Content-type in the response header.
  296. *
  297. * @param \Cake\Event\Event $event The Controller.beforeRender event.
  298. * @return bool false if the render process should be aborted
  299. */
  300. public function beforeRender(Event $event)
  301. {
  302. /** @var \Cake\Controller\Controller $controller */
  303. $controller = $event->getSubject();
  304. $response = $controller->response;
  305. $request = $controller->request;
  306. $isRecognized = (
  307. !in_array($this->ext, ['html', 'htm']) &&
  308. $response->getMimeType($this->ext)
  309. );
  310. if ($this->ext && $isRecognized) {
  311. $this->renderAs($controller, $this->ext);
  312. $response = $controller->response;
  313. } else {
  314. $response = $response->withCharset(Configure::read('App.encoding'));
  315. }
  316. if ($this->_config['checkHttpCache'] &&
  317. $response->checkNotModified($request)
  318. ) {
  319. $controller->response = $response;
  320. return false;
  321. }
  322. $controller->response = $response;
  323. }
  324. /**
  325. * Returns true if the current call accepts an XML response, false otherwise
  326. *
  327. * @return bool True if client accepts an XML response
  328. */
  329. public function isXml()
  330. {
  331. return $this->prefers('xml');
  332. }
  333. /**
  334. * Returns true if the current call accepts an RSS response, false otherwise
  335. *
  336. * @return bool True if client accepts an RSS response
  337. */
  338. public function isRss()
  339. {
  340. return $this->prefers('rss');
  341. }
  342. /**
  343. * Returns true if the current call accepts an Atom response, false otherwise
  344. *
  345. * @return bool True if client accepts an RSS response
  346. */
  347. public function isAtom()
  348. {
  349. return $this->prefers('atom');
  350. }
  351. /**
  352. * Returns true if user agent string matches a mobile web browser, or if the
  353. * client accepts WAP content.
  354. *
  355. * @return bool True if user agent is a mobile web browser
  356. */
  357. public function isMobile()
  358. {
  359. $request = $this->request;
  360. return $request->is('mobile') || $this->accepts('wap');
  361. }
  362. /**
  363. * Returns true if the client accepts WAP content
  364. *
  365. * @return bool
  366. */
  367. public function isWap()
  368. {
  369. return $this->prefers('wap');
  370. }
  371. /**
  372. * Determines which content types the client accepts. Acceptance is based on
  373. * the file extension parsed by the Router (if present), and by the HTTP_ACCEPT
  374. * header. Unlike Cake\Http\ServerRequest::accepts() this method deals entirely with mapped content types.
  375. *
  376. * Usage:
  377. *
  378. * ```
  379. * $this->RequestHandler->accepts(['xml', 'html', 'json']);
  380. * ```
  381. *
  382. * Returns true if the client accepts any of the supplied types.
  383. *
  384. * ```
  385. * $this->RequestHandler->accepts('xml');
  386. * ```
  387. *
  388. * Returns true if the client accepts xml.
  389. *
  390. * @param string|array|null $type Can be null (or no parameter), a string type name, or an
  391. * array of types
  392. * @return mixed If null or no parameter is passed, returns an array of content
  393. * types the client accepts. If a string is passed, returns true
  394. * if the client accepts it. If an array is passed, returns true
  395. * if the client accepts one or more elements in the array.
  396. */
  397. public function accepts($type = null)
  398. {
  399. $controller = $this->getController();
  400. $request = $controller->request;
  401. $response = $controller->response;
  402. $accepted = $request->accepts();
  403. if (!$type) {
  404. return $response->mapType($accepted);
  405. }
  406. if (is_array($type)) {
  407. foreach ($type as $t) {
  408. $t = $this->mapAlias($t);
  409. if (in_array($t, $accepted)) {
  410. return true;
  411. }
  412. }
  413. return false;
  414. }
  415. if (is_string($type)) {
  416. return in_array($this->mapAlias($type), $accepted);
  417. }
  418. return false;
  419. }
  420. /**
  421. * Determines the content type of the data the client has sent (i.e. in a POST request)
  422. *
  423. * @param string|array|null $type Can be null (or no parameter), a string type name, or an array of types
  424. * @return mixed If a single type is supplied a boolean will be returned. If no type is provided
  425. * The mapped value of CONTENT_TYPE will be returned. If an array is supplied the first type
  426. * in the request content type will be returned.
  427. */
  428. public function requestedWith($type = null)
  429. {
  430. $controller = $this->getController();
  431. $request = $controller->request;
  432. $response = $controller->response;
  433. if (!$request->is('post') &&
  434. !$request->is('put') &&
  435. !$request->is('patch') &&
  436. !$request->is('delete')
  437. ) {
  438. return null;
  439. }
  440. if (is_array($type)) {
  441. foreach ($type as $t) {
  442. if ($this->requestedWith($t)) {
  443. return $t;
  444. }
  445. }
  446. return false;
  447. }
  448. list($contentType) = explode(';', $request->contentType());
  449. if ($type === null) {
  450. return $response->mapType($contentType);
  451. }
  452. if (is_string($type)) {
  453. return ($type === $response->mapType($contentType));
  454. }
  455. }
  456. /**
  457. * Determines which content-types the client prefers. If no parameters are given,
  458. * the single content-type that the client most likely prefers is returned. If $type is
  459. * an array, the first item in the array that the client accepts is returned.
  460. * Preference is determined primarily by the file extension parsed by the Router
  461. * if provided, and secondarily by the list of content-types provided in
  462. * HTTP_ACCEPT.
  463. *
  464. * @param string|array|null $type An optional array of 'friendly' content-type names, i.e.
  465. * 'html', 'xml', 'js', etc.
  466. * @return mixed If $type is null or not provided, the first content-type in the
  467. * list, based on preference, is returned. If a single type is provided
  468. * a boolean will be returned if that type is preferred.
  469. * If an array of types are provided then the first preferred type is returned.
  470. * If no type is provided the first preferred type is returned.
  471. */
  472. public function prefers($type = null)
  473. {
  474. $controller = $this->getController();
  475. $request = $controller->request;
  476. $response = $controller->response;
  477. $acceptRaw = $request->parseAccept();
  478. if (empty($acceptRaw)) {
  479. return $type ? $type === $this->ext : $this->ext;
  480. }
  481. $accepts = $response->mapType(array_shift($acceptRaw));
  482. if (!$type) {
  483. if (empty($this->ext) && !empty($accepts)) {
  484. return $accepts[0];
  485. }
  486. return $this->ext;
  487. }
  488. $types = (array)$type;
  489. if (count($types) === 1) {
  490. if ($this->ext) {
  491. return in_array($this->ext, $types);
  492. }
  493. return in_array($types[0], $accepts);
  494. }
  495. $intersect = array_values(array_intersect($accepts, $types));
  496. if (!$intersect) {
  497. return false;
  498. }
  499. return $intersect[0];
  500. }
  501. /**
  502. * Sets either the view class if one exists or the layout and template path of the view.
  503. * The names of these are derived from the $type input parameter.
  504. *
  505. * ### Usage:
  506. *
  507. * Render the response as an 'ajax' response.
  508. *
  509. * ```
  510. * $this->RequestHandler->renderAs($this, 'ajax');
  511. * ```
  512. *
  513. * Render the response as an xml file and force the result as a file download.
  514. *
  515. * ```
  516. * $this->RequestHandler->renderAs($this, 'xml', ['attachment' => 'myfile.xml'];
  517. * ```
  518. *
  519. * @param \Cake\Controller\Controller $controller A reference to a controller object
  520. * @param string $type Type of response to send (e.g: 'ajax')
  521. * @param array $options Array of options to use
  522. * @return void
  523. * @see \Cake\Controller\Component\RequestHandlerComponent::respondAs()
  524. */
  525. public function renderAs(Controller $controller, $type, array $options = [])
  526. {
  527. $defaults = ['charset' => 'UTF-8'];
  528. $viewClassMap = $this->getConfig('viewClassMap');
  529. if (Configure::read('App.encoding') !== null) {
  530. $defaults['charset'] = Configure::read('App.encoding');
  531. }
  532. $options += $defaults;
  533. $builder = $controller->viewBuilder();
  534. if (array_key_exists($type, $viewClassMap)) {
  535. $view = $viewClassMap[$type];
  536. } else {
  537. $view = Inflector::classify($type);
  538. }
  539. $viewClass = null;
  540. if ($builder->getClassName() === null) {
  541. $viewClass = App::className($view, 'View', 'View');
  542. }
  543. if ($viewClass) {
  544. $controller->viewClass = $viewClass;
  545. $builder->setClassName($viewClass);
  546. } else {
  547. if (!$this->_renderType) {
  548. $builder->setTemplatePath($builder->getTemplatePath() . DIRECTORY_SEPARATOR . $type);
  549. } else {
  550. $builder->setTemplatePath(preg_replace(
  551. "/([\/\\\\]{$this->_renderType})$/",
  552. DIRECTORY_SEPARATOR . $type,
  553. $builder->getTemplatePath()
  554. ));
  555. }
  556. $this->_renderType = $type;
  557. $builder->setLayoutPath($type);
  558. }
  559. $response = $controller->response;
  560. if ($response->getMimeType($type)) {
  561. $this->respondAs($type, $options);
  562. }
  563. }
  564. /**
  565. * Sets the response header based on type map index name. This wraps several methods
  566. * available on Cake\Http\Response. It also allows you to use Content-Type aliases.
  567. *
  568. * @param string|array $type Friendly type name, i.e. 'html' or 'xml', or a full content-type,
  569. * like 'application/x-shockwave'.
  570. * @param array $options If $type is a friendly type name that is associated with
  571. * more than one type of content, $index is used to select which content-type to use.
  572. * @return bool Returns false if the friendly type name given in $type does
  573. * not exist in the type map, or if the Content-type header has
  574. * already been set by this method.
  575. */
  576. public function respondAs($type, array $options = [])
  577. {
  578. $defaults = ['index' => null, 'charset' => null, 'attachment' => false];
  579. $options += $defaults;
  580. $cType = $type;
  581. $controller = $this->getController();
  582. $response = $controller->response;
  583. $request = $controller->request;
  584. if (strpos($type, '/') === false) {
  585. $cType = $response->getMimeType($type);
  586. }
  587. if (is_array($cType)) {
  588. if (isset($cType[$options['index']])) {
  589. $cType = $cType[$options['index']];
  590. }
  591. if ($this->prefers($cType)) {
  592. $cType = $this->prefers($cType);
  593. } else {
  594. $cType = $cType[0];
  595. }
  596. }
  597. if (!$type) {
  598. return false;
  599. }
  600. if (!$request->getParam('requested')) {
  601. $response = $response->withType($cType);
  602. }
  603. if (!empty($options['charset'])) {
  604. $response = $response->withCharset($options['charset']);
  605. }
  606. if (!empty($options['attachment'])) {
  607. $response = $response->withDownload($options['attachment']);
  608. }
  609. $controller->response = $response;
  610. return true;
  611. }
  612. /**
  613. * Returns the current response type (Content-type header), or null if not alias exists
  614. *
  615. * @return mixed A string content type alias, or raw content type if no alias map exists,
  616. * otherwise null
  617. */
  618. public function responseType()
  619. {
  620. $response = $this->getController()->response;
  621. return $response->mapType($response->getType());
  622. }
  623. /**
  624. * Maps a content type alias back to its mime-type(s)
  625. *
  626. * @param string|array $alias String alias to convert back into a content type. Or an array of aliases to map.
  627. * @return string|null|array Null on an undefined alias. String value of the mapped alias type. If an
  628. * alias maps to more than one content type, the first one will be returned. If an array is provided
  629. * for $alias, an array of mapped types will be returned.
  630. */
  631. public function mapAlias($alias)
  632. {
  633. if (is_array($alias)) {
  634. return array_map([$this, 'mapAlias'], $alias);
  635. }
  636. $response = $this->getController()->response;
  637. $type = $response->getMimeType($alias);
  638. if ($type) {
  639. if (is_array($type)) {
  640. return $type[0];
  641. }
  642. return $type;
  643. }
  644. return null;
  645. }
  646. /**
  647. * Add a new mapped input type. Mapped input types are automatically
  648. * converted by RequestHandlerComponent during the startup() callback.
  649. *
  650. * @param string $type The type alias being converted, ie. json
  651. * @param array $handler The handler array for the type. The first index should
  652. * be the handling callback, all other arguments should be additional parameters
  653. * for the handler.
  654. * @return void
  655. * @throws \Cake\Core\Exception\Exception
  656. * @deprecated 3.1.0 Use setConfig('addInputType', ...) instead.
  657. */
  658. public function addInputType($type, $handler)
  659. {
  660. deprecationWarning(
  661. 'RequestHandlerComponent::addInputType() is deprecated. Use setConfig("inputTypeMap", ...) instead.'
  662. );
  663. if (!is_array($handler) || !isset($handler[0]) || !is_callable($handler[0])) {
  664. throw new Exception('You must give a handler callback.');
  665. }
  666. $this->setConfig('inputTypeMap.' . $type, $handler);
  667. }
  668. /**
  669. * Getter/setter for viewClassMap
  670. *
  671. * @param array|string|null $type The type string or array with format `['type' => 'viewClass']` to map one or more
  672. * @param array|null $viewClass The viewClass to be used for the type without `View` appended
  673. * @return array|string Returns viewClass when only string $type is set, else array with viewClassMap
  674. * @deprecated 3.1.0 Use setConfig('viewClassMap', ...) instead.
  675. */
  676. public function viewClassMap($type = null, $viewClass = null)
  677. {
  678. deprecationWarning(
  679. 'RequestHandlerComponent::viewClassMap() is deprecated. Use setConfig("viewClassMap", ...) instead.'
  680. );
  681. if (!$viewClass && is_string($type)) {
  682. return $this->getConfig('viewClassMap.' . $type);
  683. }
  684. if (is_string($type)) {
  685. $this->setConfig('viewClassMap.' . $type, $viewClass);
  686. } elseif (is_array($type)) {
  687. $this->setConfig('viewClassMap', $type, true);
  688. }
  689. return $this->getConfig('viewClassMap');
  690. }
  691. }