1
|
|
|
<?php namespace Comodojo\Dispatcher; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Dispatcher\Components\AbstractModel; |
4
|
|
|
use \Comodojo\Dispatcher\Components\DefaultConfiguration; |
5
|
|
|
|
6
|
|
|
// To Foundation // |
7
|
|
|
use \Comodojo\Dispatcher\Components\CacheManager as DispatcherCache; |
8
|
|
|
|
9
|
|
|
use \Comodojo\Dispatcher\Cache\ServerCache; |
10
|
|
|
use \Comodojo\Dispatcher\Request\Model as Request; |
11
|
|
|
use \Comodojo\Dispatcher\Router\Model as Router; |
12
|
|
|
use \Comodojo\Dispatcher\Response\Model as Response; |
13
|
|
|
use \Comodojo\Dispatcher\Extra\Model as Extra; |
14
|
|
|
use \Comodojo\Dispatcher\Output\Processor; |
15
|
|
|
use \Comodojo\Dispatcher\Events\DispatcherEvent; |
16
|
|
|
use \Comodojo\Dispatcher\Events\ServiceEvent; |
17
|
|
|
use \Comodojo\Foundation\Base\Configuration; |
18
|
|
|
use \Comodojo\Foundation\Timing\TimingTrait; |
19
|
|
|
use \Comodojo\Foundation\Events\Manager as EventsManager; |
20
|
|
|
use \Comodojo\Foundation\Logging\Manager as LogManager; |
21
|
|
|
use \Comodojo\Cache\Cache; |
22
|
|
|
use \Psr\Log\LoggerInterface; |
23
|
|
|
use \Comodojo\Exception\DispatcherException; |
24
|
|
|
use \Exception; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @package Comodojo Dispatcher |
28
|
|
|
* @author Marco Giovinazzi <[email protected]> |
29
|
|
|
* @author Marco Castiello <[email protected]> |
30
|
|
|
* @license GPL-3.0+ |
31
|
|
|
* |
32
|
|
|
* LICENSE: |
33
|
|
|
* |
34
|
|
|
* This program is free software: you can redistribute it and/or modify |
35
|
|
|
* it under the terms of the GNU Affero General Public License as |
36
|
|
|
* published by the Free Software Foundation, either version 3 of the |
37
|
|
|
* License, or (at your option) any later version. |
38
|
|
|
* |
39
|
|
|
* This program is distributed in the hope that it will be useful, |
40
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
41
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
42
|
|
|
* GNU Affero General Public License for more details. |
43
|
|
|
* |
44
|
|
|
* You should have received a copy of the GNU Affero General Public License |
45
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
46
|
|
|
*/ |
47
|
|
|
|
48
|
|
|
class Dispatcher extends AbstractModel { |
49
|
|
|
|
50
|
|
|
use TimingTrait; |
51
|
|
|
|
52
|
|
|
protected $mode = self::READONLY; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The main dispatcher constructor. |
56
|
|
|
* |
57
|
|
|
* @property Configuration $configuration |
58
|
|
|
* @property LoggerInterface $logger |
59
|
|
|
* @property EventsManager $events |
60
|
|
|
* @property Cache $cache |
61
|
|
|
* @property Extra $extra |
62
|
|
|
* @property Request $request |
63
|
|
|
* @property Router $router |
64
|
|
|
* @property Response $response |
65
|
|
|
*/ |
66
|
1 |
|
public function __construct( |
67
|
|
|
array $configuration = [], |
68
|
|
|
EventsManager $events = null, |
69
|
|
|
Cache $cache = null, |
70
|
|
|
LoggerInterface $logger = null |
71
|
|
|
) { |
72
|
|
|
|
73
|
|
|
// starting output buffer |
74
|
1 |
|
ob_start(); |
75
|
|
|
|
76
|
|
|
// fix current timestamp |
77
|
1 |
|
$this->setTiming(); |
78
|
|
|
|
79
|
|
|
// init core components |
80
|
|
|
// create new configuration object and merge configuration |
81
|
1 |
|
$configuration_object = new Configuration( DefaultConfiguration::get() ); |
82
|
1 |
|
$configuration_object->merge($configuration); |
83
|
1 |
|
$logger = is_null($logger) ? LogManager::createFromConfiguration($configuration_object)->getLogger() : $logger; |
84
|
|
|
|
85
|
1 |
|
parent::__construct($configuration_object, $logger); |
86
|
|
|
|
87
|
1 |
|
$this->setRaw('events', is_null($events) ? EventsManager::create($this->logger) : $events); |
|
|
|
|
88
|
1 |
|
$this->setRaw('cache', is_null($cache) ? DispatcherCache::create($this->configuration, $this->logger) : $cache); |
|
|
|
|
89
|
|
|
|
90
|
|
|
// init models |
91
|
1 |
|
$this->setRaw('extra', new Extra($this->logger)); |
|
|
|
|
92
|
1 |
|
$this->setRaw('request', new Request($this->configuration, $this->logger)); |
|
|
|
|
93
|
1 |
|
$this->setRaw('router', new Router($this->configuration, $this->logger, $this->cache, $this->extra)); |
|
|
|
|
94
|
1 |
|
$this->setRaw('response', new Response($this->configuration, $this->logger)); |
|
|
|
|
95
|
|
|
|
96
|
|
|
// we're ready! |
97
|
1 |
|
$this->logger->debug("Dispatcher ready"); |
|
|
|
|
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 |
|
$cache = new ServerCache($this->cache); |
|
|
|
|
126
|
|
|
|
127
|
1 |
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request') ); |
|
|
|
|
128
|
|
|
|
129
|
1 |
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.'.$this->request->method->get()) ); |
|
|
|
|
130
|
|
|
|
131
|
1 |
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.#') ); |
|
|
|
|
132
|
|
|
|
133
|
1 |
|
if ( $cache->read($this->request, $this->response) ) { |
|
|
|
|
134
|
|
|
|
135
|
|
|
return $this->shutdown(); |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
$this->logger->debug("Starting router."); |
|
|
|
|
140
|
|
|
|
141
|
|
|
try { |
142
|
|
|
|
143
|
1 |
|
$this->route = $this->router->route($this->request); |
|
|
|
|
144
|
|
|
|
145
|
1 |
|
} catch (DispatcherException $de) { |
146
|
|
|
|
147
|
1 |
|
$this->logger->debug("Route error (".$de->getStatus()."), shutting down dispatcher."); |
|
|
|
|
148
|
|
|
|
149
|
1 |
|
$this->processDispatcherException($de); |
150
|
|
|
|
151
|
1 |
|
return $this->shutdown(); |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$route_type = $route->getType(); |
|
|
|
|
156
|
|
|
|
157
|
|
|
$route_service = $route->getServiceName(); |
158
|
|
|
|
159
|
|
|
$this->logger->debug("Route acquired, type $route_type directed to $route_service."); |
|
|
|
|
160
|
|
|
|
161
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route') ); |
|
|
|
|
162
|
|
|
|
163
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$route_type) ); |
|
|
|
|
164
|
|
|
|
165
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$route_service) ); |
|
|
|
|
166
|
|
|
|
167
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.#') ); |
|
|
|
|
168
|
|
|
|
169
|
|
|
// translate route to service |
170
|
|
|
|
171
|
|
|
$this->logger->debug("Running $route_service service."); |
|
|
|
|
172
|
|
|
|
173
|
|
|
try { |
174
|
|
|
|
175
|
|
|
$this->router->compose($this->response); |
|
|
|
|
176
|
|
|
|
177
|
|
|
} catch (DispatcherException $de) { |
178
|
|
|
|
179
|
|
|
$this->logger->debug("Service exception (".$de->getStatus()."), shutting down dispatcher."); |
|
|
|
|
180
|
|
|
|
181
|
|
|
$this->processDispatcherException($de); |
182
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$this->processConfigurationParameters($this->route); |
|
|
|
|
186
|
|
|
|
187
|
|
|
$cache->dump($this->request, $this->response, $this->route); |
|
|
|
|
188
|
|
|
|
189
|
|
|
return $this->shutdown(); |
190
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
private function processConfigurationParameters($route) { |
194
|
|
|
|
195
|
|
|
$params = $route->getParameter('headers'); |
196
|
|
|
|
197
|
|
|
if ( !empty($params) && is_array($params) ) { |
198
|
|
|
|
199
|
|
|
foreach($params as $name=>$value) $this->response->headers->set($name, $value); |
|
|
|
|
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
1 |
|
private function emitServiceSpecializedEvents($name) { |
205
|
|
|
|
206
|
1 |
|
$this->logger->debug("Emitting $name service-event."); |
|
|
|
|
207
|
|
|
|
208
|
1 |
|
return new ServiceEvent( |
209
|
1 |
|
$name, |
210
|
1 |
|
$this->logger, |
|
|
|
|
211
|
1 |
|
$this->request, |
|
|
|
|
212
|
1 |
|
$this->router, |
|
|
|
|
213
|
1 |
|
$this->response, |
|
|
|
|
214
|
1 |
|
$this->extra |
|
|
|
|
215
|
1 |
|
); |
216
|
|
|
|
217
|
|
|
} |
218
|
|
|
|
219
|
1 |
|
private function processDispatcherException(DispatcherException $de) { |
220
|
|
|
|
221
|
1 |
|
$status = $de->getStatus(); |
222
|
|
|
|
223
|
1 |
|
$message = $de->getMessage(); |
224
|
|
|
|
225
|
1 |
|
$headers = $de->getHeaders(); |
226
|
|
|
|
227
|
1 |
|
$this->response->status->set($status); |
|
|
|
|
228
|
|
|
|
229
|
1 |
|
$this->response->content->set($message); |
|
|
|
|
230
|
|
|
|
231
|
1 |
|
$this->response->headers->merge($headers); |
|
|
|
|
232
|
|
|
|
233
|
1 |
|
} |
234
|
|
|
|
235
|
1 |
|
private function shutdown() { |
236
|
|
|
|
237
|
1 |
|
$this->response->consolidate($this->request, $this->route); |
|
|
|
|
238
|
|
|
|
239
|
1 |
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response') ); |
|
|
|
|
240
|
|
|
|
241
|
1 |
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.'.$this->response->status->get()) ); |
|
|
|
|
242
|
|
|
|
243
|
1 |
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.#') ); |
|
|
|
|
244
|
|
|
|
245
|
1 |
|
$this->logger->debug("Composing return value."); |
|
|
|
|
246
|
|
|
|
247
|
1 |
|
$return = Processor::parse($this->configuration, $this->logger, $this->request, $this->response); |
|
|
|
|
248
|
|
|
|
249
|
1 |
|
$this->logger->debug("Dispatcher run-cycle ends."); |
|
|
|
|
250
|
|
|
|
251
|
1 |
|
if ( function_exists('fastcgi_finish_request') ) fastcgi_finish_request(); |
252
|
1 |
|
else ob_end_clean(); |
253
|
|
|
|
254
|
1 |
|
return $return; |
255
|
|
|
|
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
} |
259
|
|
|
|
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.