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