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