Dispatcher.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. * Dispatcher takes the URL information, parses it for parameters and
  4. * tells the involved controllers what to do.
  5. *
  6. * This is the heart of Cake's operation.
  7. *
  8. * PHP 5
  9. *
  10. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  11. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  12. *
  13. * Licensed under The MIT License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. * @link http://cakephp.org CakePHP(tm) Project
  18. * @package Cake.Routing
  19. * @since CakePHP(tm) v 0.2.9
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. App::uses('Router', 'Routing');
  23. App::uses('CakeRequest', 'Network');
  24. App::uses('CakeResponse', 'Network');
  25. App::uses('Controller', 'Controller');
  26. App::uses('Scaffold', 'Controller');
  27. App::uses('View', 'View');
  28. App::uses('Debugger', 'Utility');
  29. App::uses('CakeEvent', 'Event');
  30. App::uses('CakeEventManager', 'Event');
  31. App::uses('CakeEventListener', 'Event');
  32. /**
  33. * Dispatcher converts Requests into controller actions. It uses the dispatched Request
  34. * to locate and load the correct controller. If found, the requested action is called on
  35. * the controller.
  36. *
  37. * @package Cake.Routing
  38. */
  39. class Dispatcher implements CakeEventListener {
  40. /**
  41. * Event manager, used to handle dispatcher filters
  42. *
  43. * @var CakeEventManager
  44. */
  45. protected $_eventManager;
  46. /**
  47. * Constructor.
  48. *
  49. * @param string $base The base directory for the application. Writes `App.base` to Configure.
  50. */
  51. public function __construct($base = false) {
  52. if ($base !== false) {
  53. Configure::write('App.base', $base);
  54. }
  55. }
  56. /**
  57. * Returns the CakeEventManager instance or creates one if none was
  58. * creted. Attaches the default listeners and filters
  59. *
  60. * @return CakeEventManager
  61. */
  62. public function getEventManager() {
  63. if (!$this->_eventManager) {
  64. $this->_eventManager = new CakeEventManager();
  65. $this->_eventManager->attach($this);
  66. $this->_attachFilters($this->_eventManager);
  67. }
  68. return $this->_eventManager;
  69. }
  70. /**
  71. * Returns the list of events this object listents to.
  72. *
  73. * @return array
  74. */
  75. public function implementedEvents() {
  76. return array('Dispatcher.beforeDispatch' => 'parseParams');
  77. }
  78. /**
  79. * Attaches all event listeners for this dispatcher instance. Loads the
  80. * dispatcher filters from the configured locations.
  81. *
  82. * @param CakeEventManager $manager
  83. * @return void
  84. * @throws MissingDispatcherFilterException
  85. */
  86. protected function _attachFilters($manager) {
  87. $filters = Configure::read('Dispatcher.filters');
  88. if (empty($filters)) {
  89. return;
  90. }
  91. foreach ($filters as $filter) {
  92. if (is_string($filter)) {
  93. $filter = array('callable' => $filter);
  94. }
  95. if (is_string($filter['callable'])) {
  96. list($plugin, $callable) = pluginSplit($filter['callable'], true);
  97. App::uses($callable, $plugin . 'Routing/Filter');
  98. if (!class_exists($callable)) {
  99. throw new MissingDispatcherFilterException($callable);
  100. }
  101. $manager->attach(new $callable);
  102. } else {
  103. $on = strtolower($filter['on']);
  104. $options = array();
  105. if (isset($filter['priority'])) {
  106. $options = array('priority' => $filter['priority']);
  107. }
  108. $manager->attach($filter['callable'], 'Dispatcher.' . $on . 'Dispatch', $options);
  109. }
  110. }
  111. }
  112. /**
  113. * Dispatches and invokes given Request, handing over control to the involved controller. If the controller is set
  114. * to autoRender, via Controller::$autoRender, then Dispatcher will render the view.
  115. *
  116. * Actions in CakePHP can be any public method on a controller, that is not declared in Controller. If you
  117. * want controller methods to be public and in-accessible by URL, then prefix them with a `_`.
  118. * For example `public function _loadPosts() { }` would not be accessible via URL. Private and protected methods
  119. * are also not accessible via URL.
  120. *
  121. * If no controller of given name can be found, invoke() will throw an exception.
  122. * If the controller is found, and the action is not found an exception will be thrown.
  123. *
  124. * @param CakeRequest $request Request object to dispatch.
  125. * @param CakeResponse $response Response object to put the results of the dispatch into.
  126. * @param array $additionalParams Settings array ("bare", "return") which is melded with the GET and POST params
  127. * @return string|void if `$request['return']` is set then it returns response body, null otherwise
  128. * @throws MissingControllerException When the controller is missing.
  129. */
  130. public function dispatch(CakeRequest $request, CakeResponse $response, $additionalParams = array()) {
  131. $beforeEvent = new CakeEvent('Dispatcher.beforeDispatch', $this, compact('request', 'response', 'additionalParams'));
  132. $this->getEventManager()->dispatch($beforeEvent);
  133. $request = $beforeEvent->data['request'];
  134. if ($beforeEvent->result instanceof CakeResponse) {
  135. if (isset($request->params['return'])) {
  136. return $beforeEvent->result->body();
  137. }
  138. $beforeEvent->result->send();
  139. return;
  140. }
  141. $controller = $this->_getController($request, $response);
  142. if (!($controller instanceof Controller)) {
  143. throw new MissingControllerException(array(
  144. 'class' => Inflector::camelize($request->params['controller']) . 'Controller',
  145. 'plugin' => empty($request->params['plugin']) ? null : Inflector::camelize($request->params['plugin'])
  146. ));
  147. }
  148. $response = $this->_invoke($controller, $request, $response);
  149. if (isset($request->params['return'])) {
  150. return $response->body();
  151. }
  152. $afterEvent = new CakeEvent('Dispatcher.afterDispatch', $this, compact('request', 'response'));
  153. $this->getEventManager()->dispatch($afterEvent);
  154. $afterEvent->data['response']->send();
  155. }
  156. /**
  157. * Initializes the components and models a controller will be using.
  158. * Triggers the controller action, and invokes the rendering if Controller::$autoRender is true and echo's the output.
  159. * Otherwise the return value of the controller action are returned.
  160. *
  161. * @param Controller $controller Controller to invoke
  162. * @param CakeRequest $request The request object to invoke the controller for.
  163. * @param CakeResponse $response The response object to receive the output
  164. * @return CakeResponse te resulting response object
  165. */
  166. protected function _invoke(Controller $controller, CakeRequest $request, CakeResponse $response) {
  167. $controller->constructClasses();
  168. $controller->startupProcess();
  169. $render = true;
  170. $result = $controller->invokeAction($request);
  171. if ($result instanceof CakeResponse) {
  172. $render = false;
  173. $response = $result;
  174. }
  175. if ($render && $controller->autoRender) {
  176. $response = $controller->render();
  177. } elseif ($response->body() === null) {
  178. $response->body($result);
  179. }
  180. $controller->shutdownProcess();
  181. return $response;
  182. }
  183. /**
  184. * Applies Routing and additionalParameters to the request to be dispatched.
  185. * If Routes have not been loaded they will be loaded, and app/Config/routes.php will be run.
  186. *
  187. * @param CakeEvent $event containing the request, response and additional params
  188. * @return void
  189. */
  190. public function parseParams($event) {
  191. $request = $event->data['request'];
  192. Router::setRequestInfo($request);
  193. $params = Router::parse($request->url);
  194. $request->addParams($params);
  195. if (!empty($event->data['additionalParams'])) {
  196. $request->addParams($event->data['additionalParams']);
  197. }
  198. }
  199. /**
  200. * Get controller to use, either plugin controller or application controller
  201. *
  202. * @param CakeRequest $request Request object
  203. * @param CakeResponse $response Response for the controller.
  204. * @return mixed name of controller if not loaded, or object if loaded
  205. */
  206. protected function _getController($request, $response) {
  207. $ctrlClass = $this->_loadController($request);
  208. if (!$ctrlClass) {
  209. return false;
  210. }
  211. $reflection = new ReflectionClass($ctrlClass);
  212. if ($reflection->isAbstract() || $reflection->isInterface()) {
  213. return false;
  214. }
  215. return $reflection->newInstance($request, $response);
  216. }
  217. /**
  218. * Load controller and return controller classname
  219. *
  220. * @param CakeRequest $request
  221. * @return string|bool Name of controller class name
  222. */
  223. protected function _loadController($request) {
  224. $pluginName = $pluginPath = $controller = null;
  225. if (!empty($request->params['plugin'])) {
  226. $pluginName = $controller = Inflector::camelize($request->params['plugin']);
  227. $pluginPath = $pluginName . '.';
  228. }
  229. if (!empty($request->params['controller'])) {
  230. $controller = Inflector::camelize($request->params['controller']);
  231. }
  232. if ($pluginPath . $controller) {
  233. $class = $controller . 'Controller';
  234. App::uses('AppController', 'Controller');
  235. App::uses($pluginName . 'AppController', $pluginPath . 'Controller');
  236. App::uses($class, $pluginPath . 'Controller');
  237. if (class_exists($class)) {
  238. return $class;
  239. }
  240. }
  241. return false;
  242. }
  243. }