1
|
|
|
<?php namespace Comodojo\Dispatcher; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Dispatcher\Components\AbstractModel; |
4
|
|
|
use \Comodojo\Dispatcher\Components\DefaultConfiguration; |
5
|
|
|
use \Comodojo\Dispatcher\Cache\ServerCache; |
6
|
|
|
use \Comodojo\Dispatcher\Request\Model as Request; |
7
|
|
|
use \Comodojo\Dispatcher\Router\Model as Router; |
8
|
|
|
use \Comodojo\Dispatcher\Response\Model as Response; |
9
|
|
|
use \Comodojo\Dispatcher\Extra\Model as Extra; |
10
|
|
|
use \Comodojo\Dispatcher\Output\Processor; |
11
|
|
|
use \Comodojo\Dispatcher\Events\DispatcherEvent; |
12
|
|
|
use \Comodojo\Dispatcher\Events\ServiceEvent; |
13
|
|
|
use \Comodojo\Foundation\Base\Configuration; |
14
|
|
|
use \Comodojo\Foundation\Timing\TimingTrait; |
15
|
|
|
use \Comodojo\Foundation\Events\Manager as EventsManager; |
16
|
|
|
use \Comodojo\Foundation\Logging\Manager as LogManager; |
17
|
|
|
use \Comodojo\SimpleCache\Manager as SimpleCacheManager; |
18
|
|
|
use \Psr\Log\LoggerInterface; |
19
|
|
|
use \Comodojo\Exception\DispatcherException; |
20
|
|
|
use \Exception; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @package Comodojo Dispatcher |
24
|
|
|
* @author Marco Giovinazzi <[email protected]> |
25
|
|
|
* @author Marco Castiello <[email protected]> |
26
|
|
|
* @license GPL-3.0+ |
27
|
|
|
* |
28
|
|
|
* LICENSE: |
29
|
|
|
* |
30
|
|
|
* This program is free software: you can redistribute it and/or modify |
31
|
|
|
* it under the terms of the GNU Affero General Public License as |
32
|
|
|
* published by the Free Software Foundation, either version 3 of the |
33
|
|
|
* License, or (at your option) any later version. |
34
|
|
|
* |
35
|
|
|
* This program is distributed in the hope that it will be useful, |
36
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
37
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
38
|
|
|
* GNU Affero General Public License for more details. |
39
|
|
|
* |
40
|
|
|
* You should have received a copy of the GNU Affero General Public License |
41
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
class Dispatcher extends AbstractModel { |
45
|
|
|
|
46
|
|
|
use TimingTrait; |
47
|
|
|
|
48
|
|
|
// protected $mode = self::PROTECTDATA; |
|
|
|
|
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
|
|
|
array $configuration = [], |
64
|
|
|
EventsManager $events = null, |
65
|
|
|
SimpleCacheManager $cache = null, |
66
|
|
|
LoggerInterface $logger = null |
67
|
|
|
) { |
68
|
|
|
|
69
|
|
|
// starting output buffer |
70
|
1 |
|
ob_start(); |
71
|
|
|
|
72
|
|
|
// fix current timestamp |
73
|
1 |
|
$this->setTiming(); |
74
|
|
|
|
75
|
|
|
// init core components |
76
|
|
|
// create new configuration object and merge configuration |
77
|
1 |
|
$configuration_object = new Configuration( DefaultConfiguration::get() ); |
78
|
1 |
|
$configuration_object->merge($configuration); |
79
|
1 |
|
$logger = is_null($logger) ? LogManager::createFromConfiguration($configuration_object)->getLogger() : $logger; |
80
|
|
|
|
81
|
1 |
|
parent::__construct($configuration_object, $logger); |
82
|
|
|
|
83
|
1 |
|
$this->logger->debug("--------------------------------------------------------"); |
|
|
|
|
84
|
1 |
|
$this->logger->debug("Dispatcher run-cycle starts at ".$this->getTime()->format('c')); |
|
|
|
|
85
|
|
|
|
86
|
1 |
|
try { |
87
|
|
|
|
88
|
1 |
|
$this->setRaw('events', is_null($events) ? EventsManager::create($this->logger) : $events); |
|
|
|
|
89
|
1 |
|
$this->setRaw('cache', is_null($cache) ? SimpleCacheManager::createFromConfiguration($this->configuration, $this->logger) : $cache); |
|
|
|
|
90
|
|
|
|
91
|
|
|
// init models |
92
|
1 |
|
$this->setRaw('extra', new Extra($this->logger)); |
|
|
|
|
93
|
1 |
|
$this->setRaw('request', new Request($this->configuration, $this->logger)); |
|
|
|
|
94
|
1 |
|
$this->setRaw('router', new Router($this->configuration, $this->logger, $this->cache, $this->extra)); |
|
|
|
|
95
|
1 |
|
$this->setRaw('response', new Response($this->configuration, $this->logger)); |
|
|
|
|
96
|
|
|
|
97
|
1 |
|
} catch (Exception $e) { |
98
|
|
|
$this->logger->critical($e->getMessage(),$e->getTrace()); |
|
|
|
|
99
|
1 |
|
throw $e; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// we're ready! |
103
|
1 |
|
$this->logger->debug("Dispatcher ready"); |
|
|
|
|
104
|
|
|
|
105
|
1 |
|
} |
106
|
|
|
|
107
|
1 |
|
public function dispatch() { |
108
|
|
|
|
109
|
1 |
|
$this->logger->debug("Starting to dispatch."); |
|
|
|
|
110
|
|
|
|
111
|
1 |
|
$this->logger->debug("Emitting global dispatcher event."); |
|
|
|
|
112
|
|
|
|
113
|
1 |
|
$this->events->emit( new DispatcherEvent($this) ); |
|
|
|
|
114
|
|
|
|
115
|
1 |
|
if ( $this->configuration->get('enabled') === false ) { |
|
|
|
|
116
|
|
|
|
117
|
|
|
$this->logger->debug("Dispatcher disabled, shutting down gracefully."); |
|
|
|
|
118
|
|
|
|
119
|
|
|
$status = $this->configuration->get('disabled-status'); |
|
|
|
|
120
|
|
|
|
121
|
|
|
$content = $this->configuration->get('disabled-message'); |
|
|
|
|
122
|
|
|
|
123
|
|
|
$this->response->status->set($status); |
|
|
|
|
124
|
|
|
|
125
|
|
|
$this->response->content->set($content); |
|
|
|
|
126
|
|
|
|
127
|
|
|
return $this->shutdown(); |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
$cache = new ServerCache($this->cache); |
|
|
|
|
132
|
|
|
|
133
|
1 |
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request') ); |
|
|
|
|
134
|
|
|
|
135
|
1 |
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.'.$this->request->method->get()) ); |
|
|
|
|
136
|
|
|
|
137
|
1 |
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.#') ); |
|
|
|
|
138
|
|
|
|
139
|
1 |
|
if ( $cache->read($this->request, $this->response) ) { |
|
|
|
|
140
|
|
|
|
141
|
|
|
return $this->shutdown(); |
142
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
1 |
|
$this->logger->debug("Starting router"); |
|
|
|
|
146
|
|
|
|
147
|
|
|
try { |
148
|
|
|
|
149
|
1 |
|
$this->route = $this->router->route($this->request); |
|
|
|
|
150
|
|
|
|
151
|
1 |
|
} catch (DispatcherException $de) { |
152
|
|
|
|
153
|
1 |
|
$this->logger->debug("Route error (".$de->getStatus()."), shutting down dispatcher"); |
|
|
|
|
154
|
|
|
|
155
|
1 |
|
$this->processDispatcherException($de); |
156
|
|
|
|
157
|
1 |
|
return $this->shutdown(); |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$route_type = $this->route->getType(); |
|
|
|
|
162
|
|
|
|
163
|
|
|
$route_service = $this->route->getServiceName(); |
|
|
|
|
164
|
|
|
|
165
|
|
|
$this->logger->debug("Route acquired, type $route_type directed to $route_service"); |
|
|
|
|
166
|
|
|
|
167
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route') ); |
|
|
|
|
168
|
|
|
|
169
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$route_type) ); |
|
|
|
|
170
|
|
|
|
171
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$route_service) ); |
|
|
|
|
172
|
|
|
|
173
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.#') ); |
|
|
|
|
174
|
|
|
|
175
|
|
|
// translate route to service |
176
|
|
|
|
177
|
|
|
$this->logger->debug("Running $route_service service"); |
|
|
|
|
178
|
|
|
|
179
|
|
|
try { |
180
|
|
|
|
181
|
|
|
$this->router->compose($this->response); |
|
|
|
|
182
|
|
|
|
183
|
|
|
} catch (DispatcherException $de) { |
184
|
|
|
|
185
|
|
|
$this->logger->debug("Service exception (".$de->getStatus()."), shutting down dispatcher"); |
|
|
|
|
186
|
|
|
|
187
|
|
|
$this->processDispatcherException($de); |
188
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$this->processConfigurationParameters($this->route); |
|
|
|
|
192
|
|
|
|
193
|
|
|
$cache->dump($this->request, $this->response, $this->route); |
|
|
|
|
194
|
|
|
|
195
|
|
|
return $this->shutdown(); |
196
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
private function processConfigurationParameters($route) { |
200
|
|
|
|
201
|
|
|
$params = $route->getParameter('headers'); |
202
|
|
|
|
203
|
|
|
if ( !empty($params) && is_array($params) ) { |
204
|
|
|
|
205
|
|
|
foreach($params as $name=>$value) $this->response->headers->set($name, $value); |
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
210
|
1 |
|
private function emitServiceSpecializedEvents($name) { |
211
|
|
|
|
212
|
1 |
|
$this->logger->debug("Emitting $name service-event"); |
|
|
|
|
213
|
|
|
|
214
|
1 |
|
return new ServiceEvent( |
215
|
1 |
|
$name, |
216
|
1 |
|
$this->logger, |
|
|
|
|
217
|
1 |
|
$this->request, |
|
|
|
|
218
|
1 |
|
$this->router, |
|
|
|
|
219
|
1 |
|
$this->response, |
|
|
|
|
220
|
1 |
|
$this->extra |
|
|
|
|
221
|
1 |
|
); |
222
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
1 |
|
private function processDispatcherException(DispatcherException $de) { |
226
|
|
|
|
227
|
1 |
|
$status = $de->getStatus(); |
228
|
|
|
|
229
|
1 |
|
$message = $de->getMessage(); |
230
|
|
|
|
231
|
1 |
|
$headers = $de->getHeaders(); |
232
|
|
|
|
233
|
1 |
|
$this->response->status->set($status); |
|
|
|
|
234
|
|
|
|
235
|
1 |
|
$this->response->content->set($message); |
|
|
|
|
236
|
|
|
|
237
|
1 |
|
$this->response->headers->merge($headers); |
|
|
|
|
238
|
|
|
|
239
|
1 |
|
} |
240
|
|
|
|
241
|
1 |
|
private function shutdown() { |
242
|
|
|
|
243
|
1 |
|
$this->response->consolidate($this->request, $this->route); |
|
|
|
|
244
|
|
|
|
245
|
1 |
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response') ); |
|
|
|
|
246
|
|
|
|
247
|
1 |
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.'.$this->response->status->get()) ); |
|
|
|
|
248
|
|
|
|
249
|
1 |
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.#') ); |
|
|
|
|
250
|
|
|
|
251
|
1 |
|
$this->logger->debug("Composing return value"); |
|
|
|
|
252
|
|
|
|
253
|
1 |
|
$return = Processor::parse($this->configuration, $this->logger, $this->request, $this->response); |
|
|
|
|
254
|
|
|
|
255
|
1 |
|
$this->logger->debug("Dispatcher run-cycle ends"); |
|
|
|
|
256
|
|
|
|
257
|
|
|
// This could cause WSOD with some PHP-FPM configurations |
258
|
|
|
// if ( function_exists('fastcgi_finish_request') ) fastcgi_finish_request(); |
|
|
|
|
259
|
|
|
// else ob_end_clean(); |
|
|
|
|
260
|
1 |
|
ob_end_clean(); |
261
|
|
|
|
262
|
1 |
|
return $return; |
263
|
|
|
|
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
} |
267
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.