統計
| ブランチ: | リビジョン:

pictcode_admin / lib / Cake / Routing / Dispatcher.php @ 5ad38a95

履歴 | 表示 | アノテート | ダウンロード (9.101 KB)

1 5ad38a95 spyder1211
<?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) && class_exists($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|null if `$request['return']` is set then it returns response body, null otherwise
141
 * @triggers Dispatcher.beforeDispatch $this, compact('request', 'response', 'additionalParams')
142
 * @triggers Dispatcher.afterDispatch $this, compact('request', 'response')
143
 * @throws MissingControllerException When the controller is missing.
144
 */
145
        public function dispatch(CakeRequest $request, CakeResponse $response, $additionalParams = array()) {
146
                $beforeEvent = new CakeEvent('Dispatcher.beforeDispatch', $this, compact('request', 'response', 'additionalParams'));
147
                $this->getEventManager()->dispatch($beforeEvent);
148
149
                $request = $beforeEvent->data['request'];
150
                if ($beforeEvent->result instanceof CakeResponse) {
151
                        if (isset($request->params['return'])) {
152
                                return $beforeEvent->result->body();
153
                        }
154
                        $beforeEvent->result->send();
155
                        return null;
156
                }
157
158
                $controller = $this->_getController($request, $response);
159
160
                if (!($controller instanceof Controller)) {
161
                        throw new MissingControllerException(array(
162
                                'class' => Inflector::camelize($request->params['controller']) . 'Controller',
163
                                'plugin' => empty($request->params['plugin']) ? null : Inflector::camelize($request->params['plugin'])
164
                        ));
165
                }
166
167
                $response = $this->_invoke($controller, $request);
168
                if (isset($request->params['return'])) {
169
                        return $response->body();
170
                }
171
172
                $afterEvent = new CakeEvent('Dispatcher.afterDispatch', $this, compact('request', 'response'));
173
                $this->getEventManager()->dispatch($afterEvent);
174
                $afterEvent->data['response']->send();
175
        }
176
177
/**
178
 * Initializes the components and models a controller will be using.
179
 * Triggers the controller action, and invokes the rendering if Controller::$autoRender
180
 * is true and echo's the output. Otherwise the return value of the controller
181
 * action are returned.
182
 *
183
 * @param Controller $controller Controller to invoke
184
 * @param CakeRequest $request The request object to invoke the controller for.
185
 * @return CakeResponse the resulting response object
186
 */
187
        protected function _invoke(Controller $controller, CakeRequest $request) {
188
                $controller->constructClasses();
189
                $controller->startupProcess();
190
191
                $response = $controller->response;
192
                $render = true;
193
                $result = $controller->invokeAction($request);
194
                if ($result instanceof CakeResponse) {
195
                        $render = false;
196
                        $response = $result;
197
                }
198
199
                if ($render && $controller->autoRender) {
200
                        $response = $controller->render();
201
                } elseif (!($result instanceof CakeResponse) && $response->body() === null) {
202
                        $response->body($result);
203
                }
204
                $controller->shutdownProcess();
205
206
                return $response;
207
        }
208
209
/**
210
 * Applies Routing and additionalParameters to the request to be dispatched.
211
 * If Routes have not been loaded they will be loaded, and app/Config/routes.php will be run.
212
 *
213
 * @param CakeEvent $event containing the request, response and additional params
214
 * @return void
215
 */
216
        public function parseParams($event) {
217
                $request = $event->data['request'];
218
                Router::setRequestInfo($request);
219
                $params = Router::parse($request->url);
220
                $request->addParams($params);
221
222
                if (!empty($event->data['additionalParams'])) {
223
                        $request->addParams($event->data['additionalParams']);
224
                }
225
        }
226
227
/**
228
 * Get controller to use, either plugin controller or application controller
229
 *
230
 * @param CakeRequest $request Request object
231
 * @param CakeResponse $response Response for the controller.
232
 * @return mixed name of controller if not loaded, or object if loaded
233
 */
234
        protected function _getController($request, $response) {
235
                $ctrlClass = $this->_loadController($request);
236
                if (!$ctrlClass) {
237
                        return false;
238
                }
239
                $reflection = new ReflectionClass($ctrlClass);
240
                if ($reflection->isAbstract() || $reflection->isInterface()) {
241
                        return false;
242
                }
243
                return $reflection->newInstance($request, $response);
244
        }
245
246
/**
247
 * Load controller and return controller class name
248
 *
249
 * @param CakeRequest $request Request instance.
250
 * @return string|bool Name of controller class name
251
 */
252
        protected function _loadController($request) {
253
                $pluginName = $pluginPath = $controller = null;
254
                if (!empty($request->params['plugin'])) {
255
                        $pluginName = $controller = Inflector::camelize($request->params['plugin']);
256
                        $pluginPath = $pluginName . '.';
257
                }
258
                if (!empty($request->params['controller'])) {
259
                        $controller = Inflector::camelize($request->params['controller']);
260
                }
261
                if ($pluginPath . $controller) {
262
                        $class = $controller . 'Controller';
263
                        App::uses('AppController', 'Controller');
264
                        App::uses($pluginName . 'AppController', $pluginPath . 'Controller');
265
                        App::uses($class, $pluginPath . 'Controller');
266
                        if (class_exists($class)) {
267
                                return $class;
268
                        }
269
                }
270
                return false;
271
        }
272
273
}