1 | <?php namespace Comodojo\Dispatcher; |
||
50 | class Dispatcher extends AbstractModel { |
||
51 | |||
52 | use TimingTrait; |
||
53 | use CacheTrait; |
||
54 | use RequestTrait; |
||
55 | use ResponseTrait; |
||
56 | use RouterTrait; |
||
57 | use ExtraTrait; |
||
58 | use EventsTrait; |
||
59 | |||
60 | protected $route; |
||
61 | |||
62 | /** |
||
63 | * The main dispatcher constructor. |
||
64 | * |
||
65 | */ |
||
66 | public function __construct( |
||
67 | array $configuration = [], |
||
68 | EventsManager $events = null, |
||
69 | SimpleCacheManager $cache = null, |
||
70 | LoggerInterface $logger = null |
||
71 | ) { |
||
72 | |||
73 | // starting output buffer |
||
74 | ob_start(); |
||
75 | |||
76 | // fix current timestamp |
||
77 | $this->setTiming(); |
||
78 | |||
79 | // init core components |
||
80 | // create new configuration object and merge configuration |
||
81 | $configuration_object = new Configuration( DefaultConfiguration::get() ); |
||
82 | $configuration_object->merge($configuration); |
||
83 | |||
84 | $logger = is_null($logger) ? LogManager::createFromConfiguration($configuration_object)->getLogger() : $logger; |
||
85 | |||
86 | parent::__construct($configuration_object, $logger); |
||
87 | |||
88 | $this->logger->debug("--------------------------------------------------------"); |
||
89 | $this->logger->debug("Dispatcher run-cycle starts at ".$this->getTime()->format('c')); |
||
90 | |||
91 | try { |
||
92 | |||
93 | // init other components |
||
94 | $this->setEvents(is_null($events) ? EventsManager::create($this->logger) : $events); |
||
95 | $this->setCache(is_null($cache) ? SimpleCacheManager::createFromConfiguration($this->configuration, $this->logger) : $cache); |
||
96 | |||
97 | // init models |
||
98 | $this->setExtra(new Extra($this->logger)); |
||
|
|||
99 | $this->setRequest(new Request($this->configuration, $this->logger)); |
||
100 | $this->setRouter(new Router($this->configuration, $this->logger, $this->cache, $this->events, $this->extra)); |
||
101 | $this->setResponse(new Response($this->configuration, $this->logger)); |
||
102 | |||
103 | } catch (Exception $e) { |
||
104 | |||
105 | $this->logger->critical($e->getMessage(),$e->getTrace()); |
||
106 | |||
107 | throw $e; |
||
108 | |||
109 | } |
||
110 | |||
111 | // we're ready! |
||
112 | $this->logger->debug("Dispatcher ready"); |
||
113 | |||
114 | } |
||
115 | |||
116 | public function dispatch() { |
||
117 | |||
118 | $logger = $this->getLogger(); |
||
119 | $configuration = $this->getConfiguration(); |
||
120 | $events = $this->getEvents(); |
||
121 | |||
122 | $logger->debug("Starting to dispatch."); |
||
123 | |||
124 | $logger->debug("Emitting global dispatcher event."); |
||
125 | $events->emit( new DispatcherEvent($this) ); |
||
126 | |||
127 | if ( $configuration->get('enabled') === false ) { |
||
128 | |||
129 | $logger->debug("Dispatcher disabled, shutting down gracefully."); |
||
130 | |||
131 | $status = $configuration->get('disabled-status'); |
||
132 | $content = $configuration->get('disabled-message'); |
||
133 | |||
134 | $this->getResponse()->getStatus()->set($status); |
||
135 | $this->getResponse()->getContent()->set($content); |
||
136 | |||
137 | return $this->shutdown(); |
||
138 | |||
139 | } |
||
140 | |||
141 | $cache = new ServerCache($this->getCache()); |
||
142 | |||
143 | $events->emit( $this->createServiceSpecializedEvents('dispatcher.request') ); |
||
144 | $events->emit( $this->createServiceSpecializedEvents('dispatcher.request.'.$this->request->method->get()) ); |
||
145 | $events->emit( $this->createServiceSpecializedEvents('dispatcher.request.#') ); |
||
146 | |||
147 | if ( $cache->read($this->request, $this->response) ) { |
||
148 | // we have a cache! |
||
149 | // shutdown immediately |
||
150 | return $this->shutdown(); |
||
151 | } |
||
152 | |||
153 | $logger->debug("Starting router"); |
||
154 | |||
155 | try { |
||
156 | |||
157 | $this->route = $this->getRouter()->route($this->request); |
||
158 | |||
159 | } catch (DispatcherException $de) { |
||
160 | |||
161 | $logger->debug("Route error (".$de->getStatus()."), shutting down dispatcher"); |
||
162 | $this->processDispatcherException($de); |
||
163 | return $this->shutdown(); |
||
164 | |||
165 | } |
||
166 | |||
167 | $route_type = $this->route->getType(); |
||
168 | |||
169 | $route_service = $this->route->getServiceName(); |
||
170 | |||
171 | $logger->debug("Route acquired, type $route_type directed to $route_service"); |
||
172 | |||
173 | $events->emit( $this->createServiceSpecializedEvents('dispatcher.route') ); |
||
174 | $events->emit( $this->createServiceSpecializedEvents('dispatcher.route.'.$route_type) ); |
||
175 | $events->emit( $this->createServiceSpecializedEvents('dispatcher.route.'.$route_service) ); |
||
176 | $events->emit( $this->createServiceSpecializedEvents('dispatcher.route.#') ); |
||
177 | |||
178 | // translate route to service |
||
179 | $logger->debug("Running $route_service service"); |
||
180 | |||
181 | try { |
||
182 | |||
183 | $this->router->compose($this->response); |
||
184 | |||
185 | } catch (DispatcherException $de) { |
||
186 | |||
187 | $logger->debug("Service exception (".$de->getStatus()."), shutting down dispatcher"); |
||
188 | $this->processDispatcherException($de); |
||
189 | |||
190 | } |
||
191 | |||
192 | $this->processConfigurationParameters($this->route); |
||
193 | |||
194 | $cache->dump($this->request, $this->response, $this->route); |
||
195 | |||
196 | return $this->shutdown(); |
||
197 | |||
198 | } |
||
199 | |||
200 | private function processConfigurationParameters($route) { |
||
209 | |||
210 | private function createServiceSpecializedEvents($name) { |
||
211 | |||
227 | |||
228 | private function processDispatcherException(DispatcherException $de) { |
||
241 | |||
242 | private function shutdown() { |
||
268 | |||
269 | } |
||
270 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.