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 $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: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 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: 179: 180: 181: 182: 183: 184: 185: 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: 211: 212: 213: 214: 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: 229: 230: 231: 232: 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: 248: 249: 250: 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: }
274: