1 | <?php namespace Comodojo\Dispatcher; |
||
45 | class Dispatcher { |
||
46 | |||
47 | use DataAccessTrait; |
||
48 | use TimestampTrait; |
||
49 | |||
50 | /** |
||
51 | * The main dispatcher constructor. |
||
52 | * |
||
53 | * @property Configuration $configuration |
||
54 | * @property LoggerInterface $logger |
||
55 | * @property EventsManager $events |
||
56 | * @property Cache $cache |
||
57 | * @property Extra $extra |
||
58 | * @property Request $request |
||
59 | * @property Router $router |
||
60 | * @property Response $response |
||
61 | */ |
||
62 | 1 | public function __construct( |
|
63 | $configuration = array(), |
||
64 | EventsManager $events = null, |
||
65 | Cache $cache = null, |
||
66 | LoggerInterface $logger = null |
||
67 | ) { |
||
68 | |||
69 | // starting output buffer |
||
70 | 1 | ob_start(); |
|
71 | |||
72 | // fix current timestamp |
||
73 | 1 | $this->setTimestamp(); |
|
74 | |||
75 | // parsing configuration |
||
76 | 1 | $this->configuration = new Configuration( DefaultConfiguration::get() ); |
|
77 | |||
78 | 1 | $this->configuration->merge($configuration); |
|
|
|||
79 | |||
80 | // init core components |
||
81 | 1 | $this->logger = is_null($logger) ? LogManager::create($this->configuration) : $logger; |
|
82 | |||
83 | 1 | $this->events = is_null($events) ? new EventsManager($this->logger) : $events; |
|
84 | |||
85 | 1 | $this->cache = is_null($cache) ? DispatcherCache::create($this->configuration, $this->logger) : $cache; |
|
86 | |||
87 | // init models |
||
88 | 1 | $this->extra = new Extra($this->logger); |
|
89 | |||
90 | 1 | $this->request = new Request($this->configuration, $this->logger); |
|
91 | |||
92 | 1 | $this->router = new Router($this->configuration, $this->logger, $this->cache, $this->extra); |
|
93 | |||
94 | 1 | $this->response = new Response($this->configuration, $this->logger); |
|
95 | |||
96 | // we're ready! |
||
97 | 1 | $this->logger->debug("Dispatcher ready, current date ".date('c', $this->getTimestamp())); |
|
98 | |||
99 | 1 | } |
|
100 | |||
101 | 1 | public function dispatch() { |
|
102 | |||
103 | 1 | $this->logger->debug("Starting to dispatch."); |
|
104 | |||
105 | 1 | $this->logger->debug("Emitting global dispatcher event."); |
|
106 | |||
107 | 1 | $this->events->emit( new DispatcherEvent($this) ); |
|
108 | |||
109 | 1 | if ( $this->configuration->get('enabled') === false ) { |
|
110 | |||
111 | $this->logger->debug("Dispatcher disabled, shutting down gracefully."); |
||
112 | |||
113 | $status = $this->configuration->get('disabled-status'); |
||
114 | |||
115 | $content = $this->configuration->get('disabled-message'); |
||
116 | |||
117 | $this->response->status->set($status); |
||
118 | |||
119 | $this->response->content->set($content); |
||
120 | |||
121 | return $this->shutdown(); |
||
122 | |||
123 | } |
||
124 | |||
125 | 1 | $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request') ); |
|
126 | |||
127 | 1 | $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.'.$this->request->method->get()) ); |
|
128 | |||
129 | 1 | $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.#') ); |
|
130 | |||
131 | 1 | if ( ServerCache::read($this->request, $this->response, $this->cache) ) { |
|
132 | |||
133 | return $this->shutdown(); |
||
134 | |||
135 | } |
||
136 | |||
137 | 1 | $this->logger->debug("Starting router."); |
|
138 | |||
139 | try { |
||
140 | |||
141 | 1 | $this->route = $this->router->route($this->request); |
|
142 | |||
143 | 1 | } catch (DispatcherException $de) { |
|
144 | |||
145 | 1 | $this->logger->debug("Route error (".$de->getStatus()."), shutting down dispatcher."); |
|
146 | |||
147 | 1 | $this->processDispatcherException($de); |
|
148 | |||
149 | 1 | return $this->shutdown(); |
|
150 | |||
151 | } |
||
152 | |||
153 | $route_type = $route->getType(); |
||
154 | |||
155 | $route_service = $route->getServiceName(); |
||
156 | |||
157 | $this->logger->debug("Route acquired, type $route_type directed to $route_service."); |
||
158 | |||
159 | $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route') ); |
||
160 | |||
161 | $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$route_type) ); |
||
162 | |||
163 | $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$route_service) ); |
||
164 | |||
165 | $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.#') ); |
||
166 | |||
167 | // translate route to service |
||
168 | |||
169 | $this->logger->debug("Running $route_service service."); |
||
170 | |||
171 | try { |
||
172 | |||
173 | $this->router->compose($this->response); |
||
174 | |||
175 | } catch (DispatcherException $de) { |
||
176 | |||
177 | $this->logger->debug("Service exception (".$de->getStatus()."), shutting down dispatcher."); |
||
178 | |||
179 | $this->processDispatcherException($de); |
||
180 | |||
181 | } |
||
182 | |||
183 | $this->processConfigurationParameters($this->route); |
||
184 | |||
185 | ServerCache::dump($this->request, $this->response, $this->route, $this->cache); |
||
186 | |||
187 | return $this->shutdown(); |
||
188 | |||
189 | } |
||
190 | |||
191 | private function processConfigurationParameters($route) { |
||
201 | |||
202 | 1 | private function emitServiceSpecializedEvents($name) { |
|
203 | |||
216 | |||
217 | 1 | private function processDispatcherException(DispatcherException $de) { |
|
232 | |||
233 | 1 | private function shutdown() { |
|
255 | |||
256 | } |
||
257 |
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.