1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 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: 35: 36: 37: 38: 39:
40: class Dispatcher implements CakeEventListener {
41:
42: 43: 44: 45: 46:
47: protected $_eventManager;
48:
49: 50: 51: 52: 53:
54: public function __construct($base = false) {
55: if ($base !== false) {
56: Configure::write('App.base', $base);
57: }
58: }
59:
60: 61: 62: 63: 64: 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: 77: 78: 79:
80: public function implementedEvents() {
81: return array('Dispatcher.beforeDispatch' => 'parseParams');
82: }
83:
84: 85: 86: 87: 88: 89: 90: 91:
92: protected function _attachFilters($manager) {
93: $filters = Configure::read('Dispatcher.filters');
94: if (empty($filters)) {
95: return;
96: }
97:
98: foreach ($filters as $filter) {
99: if (is_string($filter)) {
100: $filter = array('callable' => $filter);
101: }
102: if (is_string($filter['callable'])) {
103: list($plugin, $callable) = pluginSplit($filter['callable'], true);
104: App::uses($callable, $plugin . 'Routing/Filter');
105: if (!class_exists($callable)) {
106: throw new MissingDispatcherFilterException($callable);
107: }
108: $manager->attach(new $callable);
109: } else {
110: $on = strtolower($filter['on']);
111: $options = array();
112: if (isset($filter['priority'])) {
113: $options = array('priority' => $filter['priority']);
114: }
115: $manager->attach($filter['callable'], 'Dispatcher.' . $on . 'Dispatch', $options);
116: }
117: }
118: }
119:
120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137:
138: public function dispatch(CakeRequest $request, CakeResponse $response, $additionalParams = array()) {
139: $beforeEvent = new CakeEvent('Dispatcher.beforeDispatch', $this, compact('request', 'response', 'additionalParams'));
140: $this->getEventManager()->dispatch($beforeEvent);
141:
142: $request = $beforeEvent->data['request'];
143: if ($beforeEvent->result instanceof CakeResponse) {
144: if (isset($request->params['return'])) {
145: return $beforeEvent->result->body();
146: }
147: $beforeEvent->result->send();
148: return;
149: }
150:
151: $controller = $this->_getController($request, $response);
152:
153: if (!($controller instanceof Controller)) {
154: throw new MissingControllerException(array(
155: 'class' => Inflector::camelize($request->params['controller']) . 'Controller',
156: 'plugin' => empty($request->params['plugin']) ? null : Inflector::camelize($request->params['plugin'])
157: ));
158: }
159:
160: $response = $this->_invoke($controller, $request, $response);
161: if (isset($request->params['return'])) {
162: return $response->body();
163: }
164:
165: $afterEvent = new CakeEvent('Dispatcher.afterDispatch', $this, compact('request', 'response'));
166: $this->getEventManager()->dispatch($afterEvent);
167: $afterEvent->data['response']->send();
168: }
169:
170: 171: 172: 173: 174: 175: 176: 177: 178: 179:
180: protected function _invoke(Controller $controller, CakeRequest $request, CakeResponse $response) {
181: $controller->constructClasses();
182: $controller->startupProcess();
183:
184: $render = true;
185: $result = $controller->invokeAction($request);
186: if ($result instanceof CakeResponse) {
187: $render = false;
188: $response = $result;
189: }
190:
191: if ($render && $controller->autoRender) {
192: $response = $controller->render();
193: } elseif (!($result instanceof CakeResponse) && $response->body() === null) {
194: $response->body($result);
195: }
196: $controller->shutdownProcess();
197:
198: return $response;
199: }
200:
201: 202: 203: 204: 205: 206: 207:
208: public function parseParams($event) {
209: $request = $event->data['request'];
210: Router::setRequestInfo($request);
211: $params = Router::parse($request->url);
212: $request->addParams($params);
213:
214: if (!empty($event->data['additionalParams'])) {
215: $request->addParams($event->data['additionalParams']);
216: }
217: }
218:
219: 220: 221: 222: 223: 224: 225:
226: protected function _getController($request, $response) {
227: $ctrlClass = $this->_loadController($request);
228: if (!$ctrlClass) {
229: return false;
230: }
231: $reflection = new ReflectionClass($ctrlClass);
232: if ($reflection->isAbstract() || $reflection->isInterface()) {
233: return false;
234: }
235: return $reflection->newInstance($request, $response);
236: }
237:
238: 239: 240: 241: 242: 243:
244: protected function _loadController($request) {
245: $pluginName = $pluginPath = $controller = null;
246: if (!empty($request->params['plugin'])) {
247: $pluginName = $controller = Inflector::camelize($request->params['plugin']);
248: $pluginPath = $pluginName . '.';
249: }
250: if (!empty($request->params['controller'])) {
251: $controller = Inflector::camelize($request->params['controller']);
252: }
253: if ($pluginPath . $controller) {
254: $class = $controller . 'Controller';
255: App::uses('AppController', 'Controller');
256: App::uses($pluginName . 'AppController', $pluginPath . 'Controller');
257: App::uses($class, $pluginPath . 'Controller');
258: if (class_exists($class)) {
259: return $class;
260: }
261: }
262: return false;
263: }
264:
265: }
266: