1 | <?php namespace Comodojo\Dispatcher; |
||
44 | class Dispatcher { |
||
45 | |||
46 | use DataAccessTrait; |
||
47 | use TimestampTrait; |
||
48 | |||
49 | /** |
||
50 | * The main dispatcher constructor. |
||
51 | * |
||
52 | * @property Configuration $configuration |
||
53 | * @property LoggerInterface $logger |
||
54 | * @property EventsManager $events |
||
55 | * @property Cache $cache |
||
56 | * @property Extra $extra |
||
57 | * @property Request $request |
||
58 | * @property Router $router |
||
59 | * @property Response $response |
||
60 | */ |
||
61 | 1 | public function __construct( |
|
99 | |||
100 | 1 | public function dispatch() { |
|
101 | |||
102 | 1 | $this->logger->debug("Starting to dispatch."); |
|
103 | |||
104 | 1 | $this->logger->debug("Emitting global dispatcher event."); |
|
105 | |||
106 | 1 | $this->events->emit( new DispatcherEvent($this) ); |
|
107 | |||
108 | 1 | if ( $this->configuration->get('enabled') === false ) { |
|
109 | |||
110 | $this->logger->debug("Dispatcher disabled, shutting down gracefully."); |
||
111 | |||
112 | $status = $this->configuration->get('disabled-status'); |
||
113 | |||
114 | $content = $this->configuration->get('disabled-message'); |
||
115 | |||
116 | $this->response->status->set($status); |
||
117 | |||
118 | $this->response->content->set($content); |
||
119 | |||
120 | return $this->shutdown(); |
||
121 | |||
122 | } |
||
123 | |||
124 | 1 | $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request') ); |
|
125 | |||
126 | 1 | $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.'.$this->request->method->get()) ); |
|
127 | |||
128 | 1 | $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.#') ); |
|
129 | |||
130 | 1 | if ( $this->readCache() ) { |
|
131 | |||
132 | return $this->shutdown(); |
||
133 | |||
134 | } |
||
135 | |||
136 | 1 | $this->logger->debug("Starting router."); |
|
137 | |||
138 | try { |
||
139 | |||
140 | 1 | $route = $this->router->route($this->request); |
|
141 | |||
142 | $this->route = $route; |
||
143 | |||
144 | 1 | } catch (DispatcherException $de) { |
|
145 | |||
146 | 1 | $this->logger->debug("Route error (".$de->getStatus()."), shutting down dispatcher."); |
|
147 | |||
148 | 1 | $this->processDispatcherException($de); |
|
149 | |||
150 | 1 | return $this->shutdown(); |
|
151 | |||
152 | } |
||
153 | |||
154 | $route_type = $route->getType(); |
||
155 | |||
156 | $route_service = $route->getServiceName(); |
||
157 | |||
158 | $this->logger->debug("Route acquired, type $route_type directed to $route_service."); |
||
159 | |||
160 | $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route') ); |
||
161 | |||
162 | $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$route_type) ); |
||
163 | |||
164 | $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$route_service) ); |
||
165 | |||
166 | $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.#') ); |
||
167 | |||
168 | // translate route to service |
||
169 | |||
170 | $this->logger->debug("Running $route_service service."); |
||
171 | |||
172 | try { |
||
173 | |||
174 | $this->router->compose($this->response); |
||
175 | |||
176 | } catch (DispatcherException $de) { |
||
177 | |||
178 | $this->logger->debug("Service exception (".$de->getStatus()."), shutting down dispatcher."); |
||
179 | |||
180 | $this->processDispatcherException($de); |
||
181 | |||
182 | } |
||
183 | |||
184 | $this->processConfigurationParameters($route); |
||
185 | |||
186 | $this->dumpCache($route); |
||
187 | |||
188 | return $this->shutdown(); |
||
189 | |||
190 | } |
||
191 | |||
192 | 1 | private function readCache() { |
|
193 | |||
194 | 1 | $name = (string) $this->request->uri; |
|
195 | |||
196 | 1 | $cache = $this->cache->setNamespace('dispatcherservice')->get($name); |
|
197 | |||
198 | 1 | if ( is_null($cache) ) return false; |
|
199 | |||
200 | $this->response->import($cache); |
||
201 | |||
202 | return true; |
||
203 | |||
204 | } |
||
205 | |||
206 | private function dumpCache($route) { |
||
232 | |||
233 | private function processConfigurationParameters($route) { |
||
243 | |||
244 | 1 | private function emitServiceSpecializedEvents($name) { |
|
245 | |||
258 | |||
259 | 1 | private function processDispatcherException(DispatcherException $de) { |
|
274 | |||
275 | 1 | private function shutdown() { |
|
297 | |||
298 | } |
||
299 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.