2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2011-10-18 17:06:29 +00:00
|
|
|
* Dispatcher takes the URL information, parses it for parameters and
|
2008-05-30 11:40:08 +00:00
|
|
|
* tells the involved controllers what to do.
|
|
|
|
*
|
|
|
|
* This is the heart of Cake's operation.
|
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2012-03-13 02:46:46 +00:00
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2012-03-13 02:46:46 +00:00
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Routing
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 0.2.9
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-12-04 01:23:47 +00:00
|
|
|
App::uses('Router', 'Routing');
|
|
|
|
App::uses('CakeRequest', 'Network');
|
|
|
|
App::uses('CakeResponse', 'Network');
|
2010-12-03 21:24:50 +00:00
|
|
|
App::uses('Controller', 'Controller');
|
2010-12-03 22:38:52 +00:00
|
|
|
App::uses('Scaffold', 'Controller');
|
2010-12-03 21:24:50 +00:00
|
|
|
App::uses('View', 'View');
|
2010-12-04 07:04:30 +00:00
|
|
|
App::uses('Debugger', 'Utility');
|
2012-04-15 16:09:08 +00:00
|
|
|
App::uses('CakeEvent', 'Event');
|
|
|
|
App::uses('CakeEventManager', 'Event');
|
|
|
|
App::uses('CakeEventListener', 'Event');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-11-14 23:37:27 +00:00
|
|
|
* Dispatcher converts Requests into controller actions. It uses the dispatched Request
|
2011-04-17 11:13:02 +00:00
|
|
|
* to locate and load the correct controller. If found, the requested action is called on
|
2010-11-14 23:37:27 +00:00
|
|
|
* the controller.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Routing
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-04-15 16:09:08 +00:00
|
|
|
class Dispatcher implements CakeEventListener {
|
|
|
|
|
2012-04-15 16:17:41 +00:00
|
|
|
/**
|
|
|
|
* Event manager, used to handle dispatcher filters
|
|
|
|
*
|
2012-07-08 04:33:14 +00:00
|
|
|
* @var CakeEventManager
|
2012-04-24 02:01:45 +00:00
|
|
|
*/
|
2012-04-15 16:09:08 +00:00
|
|
|
protected $_eventManager;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
2011-07-28 17:10:40 +00:00
|
|
|
*
|
|
|
|
* @param string $base The base directory for the application. Writes `App.base` to Configure.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-07-28 17:10:40 +00:00
|
|
|
public function __construct($base = false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($base !== false) {
|
|
|
|
Configure::write('App.base', $base);
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-04-15 16:17:41 +00:00
|
|
|
/**
|
|
|
|
* Returns the CakeEventManager instance or creates one if none was
|
|
|
|
* creted. Attaches the default listeners and filters
|
|
|
|
*
|
2012-07-08 04:33:14 +00:00
|
|
|
* @return CakeEventManager
|
2012-04-24 02:01:45 +00:00
|
|
|
*/
|
2012-04-15 16:09:08 +00:00
|
|
|
public function getEventManager() {
|
|
|
|
if (!$this->_eventManager) {
|
|
|
|
$this->_eventManager = new CakeEventManager();
|
|
|
|
$this->_eventManager->attach($this);
|
2012-04-16 05:06:08 +00:00
|
|
|
$this->_attachFilters($this->_eventManager);
|
2012-04-15 16:09:08 +00:00
|
|
|
}
|
|
|
|
return $this->_eventManager;
|
|
|
|
}
|
|
|
|
|
2012-04-15 16:17:41 +00:00
|
|
|
/**
|
|
|
|
* Returns the list of events this object listents to.
|
|
|
|
*
|
|
|
|
* @return array
|
2012-04-24 02:01:45 +00:00
|
|
|
*/
|
2012-04-15 16:09:08 +00:00
|
|
|
public function implementedEvents() {
|
2012-04-17 05:12:18 +00:00
|
|
|
return array('Dispatcher.beforeDispatch' => 'parseParams');
|
2012-04-15 16:09:08 +00:00
|
|
|
}
|
|
|
|
|
2012-04-16 05:06:08 +00:00
|
|
|
/**
|
|
|
|
* Attaches all event listeners for this dispatcher instance. Loads the
|
|
|
|
* dispatcher filters from the configured locations.
|
|
|
|
*
|
|
|
|
* @param CakeEventManager $manager
|
|
|
|
* @return void
|
2012-04-24 02:01:45 +00:00
|
|
|
* @throws MissingDispatcherFilterException
|
|
|
|
*/
|
2012-04-16 05:06:08 +00:00
|
|
|
protected function _attachFilters($manager) {
|
|
|
|
$filters = Configure::read('Dispatcher.filters');
|
|
|
|
if (empty($filters)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($filters as $filter) {
|
|
|
|
if (is_string($filter)) {
|
|
|
|
$filter = array('callable' => $filter);
|
|
|
|
}
|
|
|
|
if (is_string($filter['callable'])) {
|
|
|
|
list($plugin, $callable) = pluginSplit($filter['callable'], true);
|
|
|
|
App::uses($callable, $plugin . 'Routing/Filter');
|
|
|
|
if (!class_exists($callable)) {
|
|
|
|
throw new MissingDispatcherFilterException($callable);
|
|
|
|
}
|
|
|
|
$manager->attach(new $callable);
|
|
|
|
} else {
|
|
|
|
$on = strtolower($filter['on']);
|
|
|
|
$options = array();
|
|
|
|
if (isset($filter['priority'])) {
|
2012-04-17 04:11:32 +00:00
|
|
|
$options = array('priority' => $filter['priority']);
|
2012-04-16 05:06:08 +00:00
|
|
|
}
|
2012-04-17 04:11:32 +00:00
|
|
|
$manager->attach($filter['callable'], 'Dispatcher.' . $on . 'Dispatch', $options);
|
2012-04-16 05:06:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-11-14 23:37:27 +00:00
|
|
|
* Dispatches and invokes given Request, handing over control to the involved controller. If the controller is set
|
|
|
|
* to autoRender, via Controller::$autoRender, then Dispatcher will render the view.
|
|
|
|
*
|
|
|
|
* Actions in CakePHP can be any public method on a controller, that is not declared in Controller. If you
|
2011-12-02 05:58:09 +00:00
|
|
|
* want controller methods to be public and in-accessible by URL, then prefix them with a `_`.
|
2010-11-14 23:37:27 +00:00
|
|
|
* For example `public function _loadPosts() { }` would not be accessible via URL. Private and protected methods
|
|
|
|
* are also not accessible via URL.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-11-14 23:37:27 +00:00
|
|
|
* If no controller of given name can be found, invoke() will throw an exception.
|
|
|
|
* If the controller is found, and the action is not found an exception will be thrown.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-11-14 05:06:01 +00:00
|
|
|
* @param CakeRequest $request Request object to dispatch.
|
2011-07-03 18:16:27 +00:00
|
|
|
* @param CakeResponse $response Response object to put the results of the dispatch into.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param array $additionalParams Settings array ("bare", "return") which is melded with the GET and POST params
|
2012-04-16 05:06:08 +00:00
|
|
|
* @return string|void if `$request['return']` is set then it returns response body, null otherwise
|
2012-03-04 01:28:22 +00:00
|
|
|
* @throws MissingControllerException When the controller is missing.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-07-03 18:16:27 +00:00
|
|
|
public function dispatch(CakeRequest $request, CakeResponse $response, $additionalParams = array()) {
|
2012-04-17 04:11:32 +00:00
|
|
|
$beforeEvent = new CakeEvent('Dispatcher.beforeDispatch', $this, compact('request', 'response', 'additionalParams'));
|
2012-04-15 16:09:08 +00:00
|
|
|
$this->getEventManager()->dispatch($beforeEvent);
|
|
|
|
|
|
|
|
$request = $beforeEvent->data['request'];
|
|
|
|
if ($beforeEvent->result instanceof CakeResponse) {
|
2012-04-16 05:06:08 +00:00
|
|
|
if (isset($request->params['return'])) {
|
2012-07-31 16:39:05 +00:00
|
|
|
return $beforeEvent->result->body();
|
2012-04-16 05:06:08 +00:00
|
|
|
}
|
2012-07-31 16:39:05 +00:00
|
|
|
$beforeEvent->result->send();
|
2010-08-05 03:44:48 +00:00
|
|
|
return;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-05-02 03:19:47 +00:00
|
|
|
|
2011-07-03 18:16:27 +00:00
|
|
|
$controller = $this->_getController($request, $response);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-03-11 03:04:46 +00:00
|
|
|
if (!($controller instanceof Controller)) {
|
2010-08-30 01:37:25 +00:00
|
|
|
throw new MissingControllerException(array(
|
2011-10-16 11:40:36 +00:00
|
|
|
'class' => Inflector::camelize($request->params['controller']) . 'Controller',
|
2011-10-16 11:42:50 +00:00
|
|
|
'plugin' => empty($request->params['plugin']) ? null : Inflector::camelize($request->params['plugin'])
|
2010-08-30 01:37:25 +00:00
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-11-14 23:38:10 +00:00
|
|
|
|
2012-04-15 16:09:08 +00:00
|
|
|
$response = $this->_invoke($controller, $request, $response);
|
|
|
|
if (isset($request->params['return'])) {
|
|
|
|
return $response->body();
|
|
|
|
}
|
|
|
|
|
2012-04-17 04:11:32 +00:00
|
|
|
$afterEvent = new CakeEvent('Dispatcher.afterDispatch', $this, compact('request', 'response'));
|
2012-04-15 16:09:08 +00:00
|
|
|
$this->getEventManager()->dispatch($afterEvent);
|
|
|
|
$afterEvent->data['response']->send();
|
2010-11-14 23:37:27 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-03-28 15:28:03 +00:00
|
|
|
* Initializes the components and models a controller will be using.
|
|
|
|
* Triggers the controller action, and invokes the rendering if Controller::$autoRender is true and echo's the output.
|
|
|
|
* Otherwise the return value of the controller action are returned.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-11-14 06:05:42 +00:00
|
|
|
* @param Controller $controller Controller to invoke
|
|
|
|
* @param CakeRequest $request The request object to invoke the controller for.
|
2011-07-30 21:02:25 +00:00
|
|
|
* @param CakeResponse $response The response object to receive the output
|
2012-04-15 16:09:08 +00:00
|
|
|
* @return CakeResponse te resulting response object
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-07-03 18:16:27 +00:00
|
|
|
protected function _invoke(Controller $controller, CakeRequest $request, CakeResponse $response) {
|
2008-07-26 20:09:20 +00:00
|
|
|
$controller->constructClasses();
|
2010-02-28 03:01:08 +00:00
|
|
|
$controller->startupProcess();
|
2008-07-26 20:09:20 +00:00
|
|
|
|
2011-07-09 18:21:32 +00:00
|
|
|
$render = true;
|
2011-07-09 17:56:51 +00:00
|
|
|
$result = $controller->invokeAction($request);
|
2011-07-07 00:41:38 +00:00
|
|
|
if ($result instanceof CakeResponse) {
|
2011-07-09 18:21:32 +00:00
|
|
|
$render = false;
|
2011-07-07 00:41:38 +00:00
|
|
|
$response = $result;
|
|
|
|
}
|
2011-08-16 03:55:08 +00:00
|
|
|
|
2011-07-09 18:21:32 +00:00
|
|
|
if ($render && $controller->autoRender) {
|
2011-07-07 00:41:38 +00:00
|
|
|
$response = $controller->render();
|
2010-08-22 16:40:06 +00:00
|
|
|
} elseif ($response->body() === null) {
|
|
|
|
$response->body($result);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-02-28 03:01:08 +00:00
|
|
|
$controller->shutdownProcess();
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2012-04-15 16:09:08 +00:00
|
|
|
return $response;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-11-14 23:43:46 +00:00
|
|
|
* Applies Routing and additionalParameters to the request to be dispatched.
|
2011-06-20 00:28:40 +00:00
|
|
|
* If Routes have not been loaded they will be loaded, and app/Config/routes.php will be run.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-04-15 16:09:08 +00:00
|
|
|
* @param CakeEvent $event containing the request, response and additional params
|
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-04-15 16:09:08 +00:00
|
|
|
public function parseParams($event) {
|
|
|
|
$request = $event->data['request'];
|
2012-05-11 15:12:07 +00:00
|
|
|
Router::setRequestInfo($request);
|
2012-09-14 17:42:25 +00:00
|
|
|
if (!count(Router::$routes)) {
|
2010-11-14 06:12:39 +00:00
|
|
|
$namedExpressions = Router::getNamedExpressions();
|
|
|
|
extract($namedExpressions);
|
2010-12-24 17:54:04 +00:00
|
|
|
$this->_loadRoutes();
|
2010-11-14 06:12:39 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-05-02 04:29:35 +00:00
|
|
|
$params = Router::parse($request->url);
|
|
|
|
$request->addParams($params);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-04-15 16:09:08 +00:00
|
|
|
if (!empty($event->data['additionalParams'])) {
|
|
|
|
$request->addParams($event->data['additionalParams']);
|
2009-11-27 06:49:20 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Get controller to use, either plugin controller or application controller
|
|
|
|
*
|
2011-07-03 18:16:27 +00:00
|
|
|
* @param CakeRequest $request Request object
|
|
|
|
* @param CakeResponse $response Response for the controller.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return mixed name of controller if not loaded, or object if loaded
|
|
|
|
*/
|
2011-07-03 18:16:27 +00:00
|
|
|
protected function _getController($request, $response) {
|
2010-11-14 06:08:13 +00:00
|
|
|
$ctrlClass = $this->_loadController($request);
|
2009-02-16 00:18:59 +00:00
|
|
|
if (!$ctrlClass) {
|
2010-11-14 23:43:46 +00:00
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-10-01 02:40:30 +00:00
|
|
|
$reflection = new ReflectionClass($ctrlClass);
|
|
|
|
if ($reflection->isAbstract() || $reflection->isInterface()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $reflection->newInstance($request, $response);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-03-28 15:28:03 +00:00
|
|
|
* Load controller and return controller classname
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-30 21:02:25 +00:00
|
|
|
* @param CakeRequest $request
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string|bool Name of controller class name
|
|
|
|
*/
|
2010-11-14 06:08:13 +00:00
|
|
|
protected function _loadController($request) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$pluginName = $pluginPath = $controller = null;
|
2010-05-07 03:18:50 +00:00
|
|
|
if (!empty($request->params['plugin'])) {
|
|
|
|
$pluginName = $controller = Inflector::camelize($request->params['plugin']);
|
2008-05-30 11:40:08 +00:00
|
|
|
$pluginPath = $pluginName . '.';
|
|
|
|
}
|
2010-05-07 03:18:50 +00:00
|
|
|
if (!empty($request->params['controller'])) {
|
|
|
|
$controller = Inflector::camelize($request->params['controller']);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if ($pluginPath . $controller) {
|
2010-12-05 15:24:42 +00:00
|
|
|
$class = $controller . 'Controller';
|
|
|
|
App::uses('AppController', 'Controller');
|
2011-02-22 05:14:33 +00:00
|
|
|
App::uses($pluginName . 'AppController', $pluginPath . 'Controller');
|
2010-12-05 15:24:42 +00:00
|
|
|
App::uses($class, $pluginPath . 'Controller');
|
|
|
|
if (class_exists($class)) {
|
|
|
|
return $class;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-12-15 03:58:27 +00:00
|
|
|
/**
|
|
|
|
* Loads route configuration
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-12-24 17:54:04 +00:00
|
|
|
protected function _loadRoutes() {
|
2011-04-17 11:13:02 +00:00
|
|
|
include APP . 'Config' . DS . 'routes.php';
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|