PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Routing/Dispatcher.php

https://github.com/ceeram/cakephp
PHP | 158 lines | 74 code | 17 blank | 67 comment | 11 complexity | 53e49e3a27b717d2bbd91d374dd98db7 MD5 | raw file
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 0.2.9
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Routing;
  16. use Cake\Controller\Controller;
  17. use Cake\Event\EventListenerInterface;
  18. use Cake\Event\EventManagerTrait;
  19. use Cake\Network\Request;
  20. use Cake\Network\Response;
  21. use Cake\Routing\Exception\MissingControllerException;
  22. use LogicException;
  23. /**
  24. * Dispatcher converts Requests into controller actions. It uses the dispatched Request
  25. * to locate and load the correct controller. If found, the requested action is called on
  26. * the controller
  27. *
  28. */
  29. class Dispatcher
  30. {
  31. use EventManagerTrait;
  32. /**
  33. * Connected filter objects
  34. *
  35. * @var array
  36. */
  37. protected $_filters = [];
  38. /**
  39. * Dispatches and invokes given Request, handing over control to the involved controller. If the controller is set
  40. * to autoRender, via Controller::$autoRender, then Dispatcher will render the view.
  41. *
  42. * Actions in CakePHP can be any public method on a controller, that is not declared in Controller. If you
  43. * want controller methods to be public and in-accessible by URL, then prefix them with a `_`.
  44. * For example `public function _loadPosts() { }` would not be accessible via URL. Private and protected methods
  45. * are also not accessible via URL.
  46. *
  47. * If no controller of given name can be found, invoke() will throw an exception.
  48. * If the controller is found, and the action is not found an exception will be thrown.
  49. *
  50. * @param \Cake\Network\Request $request Request object to dispatch.
  51. * @param \Cake\Network\Response $response Response object to put the results of the dispatch into.
  52. * @return string|void if `$request['return']` is set then it returns response body, null otherwise
  53. * @throws \Cake\Routing\Exception\MissingControllerException When the controller is missing.
  54. */
  55. public function dispatch(Request $request, Response $response)
  56. {
  57. $beforeEvent = $this->dispatchEvent('Dispatcher.beforeDispatch', compact('request', 'response'));
  58. $request = $beforeEvent->data['request'];
  59. if ($beforeEvent->result instanceof Response) {
  60. if (isset($request->params['return'])) {
  61. return $beforeEvent->result->body();
  62. }
  63. $beforeEvent->result->send();
  64. return;
  65. }
  66. $controller = false;
  67. if (isset($beforeEvent->data['controller'])) {
  68. $controller = $beforeEvent->data['controller'];
  69. }
  70. if (!($controller instanceof Controller)) {
  71. throw new MissingControllerException([
  72. 'class' => $request->params['controller'],
  73. 'plugin' => empty($request->params['plugin']) ? null : $request->params['plugin'],
  74. 'prefix' => empty($request->params['prefix']) ? null : $request->params['prefix'],
  75. '_ext' => empty($request->params['_ext']) ? null : $request->params['_ext']
  76. ]);
  77. }
  78. $response = $this->_invoke($controller);
  79. if (isset($request->params['return'])) {
  80. return $response->body();
  81. }
  82. $afterEvent = $this->dispatchEvent('Dispatcher.afterDispatch', compact('request', 'response'));
  83. $afterEvent->data['response']->send();
  84. }
  85. /**
  86. * Initializes the components and models a controller will be using.
  87. * Triggers the controller action and invokes the rendering if Controller::$autoRender
  88. * is true. If a response object is returned by controller action that is returned
  89. * else controller's $response property is returned.
  90. *
  91. * @param Controller $controller Controller to invoke
  92. * @return \Cake\Network\Response The resulting response object
  93. * @throws \LogicException If data returned by controller action is not an
  94. * instance of Response
  95. */
  96. protected function _invoke(Controller $controller)
  97. {
  98. $result = $controller->startupProcess();
  99. if ($result instanceof Response) {
  100. return $result;
  101. }
  102. $response = $controller->invokeAction();
  103. if ($response !== null && !($response instanceof Response)) {
  104. throw new LogicException('Controller action can only return an instance of Response');
  105. }
  106. if (!$response && $controller->autoRender) {
  107. $response = $controller->render();
  108. } elseif (!$response) {
  109. $response = $controller->response;
  110. }
  111. $result = $controller->shutdownProcess();
  112. if ($result instanceof Response) {
  113. return $result;
  114. }
  115. return $response;
  116. }
  117. /**
  118. * Add a filter to this dispatcher.
  119. *
  120. * The added filter will be attached to the event manager used
  121. * by this dispatcher.
  122. *
  123. * @param \Cake\Event\EventListenerInterface $filter The filter to connect. Can be
  124. * any EventListenerInterface. Typically an instance of \Cake\Routing\DispatcherFilter.
  125. * @return void
  126. */
  127. public function addFilter(EventListenerInterface $filter)
  128. {
  129. $this->_filters[] = $filter;
  130. $this->eventManager()->on($filter);
  131. }
  132. /**
  133. * Get the list of connected filters.
  134. *
  135. * @return array
  136. */
  137. public function filters()
  138. {
  139. return $this->_filters;
  140. }
  141. }