Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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