1
|
|
|
<?php namespace Comodojo\Dispatcher; |
2
|
|
|
|
3
|
|
|
use \Psr\Log\LoggerInterface; |
4
|
|
|
use \Comodojo\Dispatcher\Components\DataAccess as DataAccessTrait; |
5
|
|
|
use \Comodojo\Dispatcher\Components\Configuration; |
6
|
|
|
use \Comodojo\Dispatcher\Components\DefaultConfiguration; |
7
|
|
|
use \Comodojo\Dispatcher\Components\EventsManager; |
8
|
|
|
use \Comodojo\Dispatcher\Components\Timestamp as TimestampTrait; |
9
|
|
|
use \Comodojo\Dispatcher\Components\CacheManager as DispatcherCache; |
10
|
|
|
use \Comodojo\Dispatcher\Components\LogManager; |
11
|
|
|
use \Comodojo\Dispatcher\Request\Model as Request; |
12
|
|
|
use \Comodojo\Dispatcher\Router\Model as Router; |
13
|
|
|
use \Comodojo\Dispatcher\Response\Model as Response; |
14
|
|
|
use \Comodojo\Dispatcher\Extra\Model as Extra; |
15
|
|
|
use \Comodojo\Dispatcher\Output\Processor; |
16
|
|
|
use \Comodojo\Dispatcher\Events\DispatcherEvent; |
17
|
|
|
use \Comodojo\Dispatcher\Events\ServiceEvent; |
18
|
|
|
use \Comodojo\Cache\CacheManager; |
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 { |
45
|
|
|
|
46
|
|
|
use DataAccessTrait; |
47
|
|
|
use TimestampTrait; |
48
|
|
|
|
49
|
|
|
public function __construct( |
50
|
|
|
$configuration = array(), |
51
|
|
|
EventsManager $events = null, |
52
|
|
|
CacheManager $cache = null, |
53
|
|
|
LoggerInterface $logger = null |
54
|
|
|
) { |
55
|
|
|
|
56
|
|
|
// starting output buffer |
57
|
|
|
ob_start(); |
58
|
|
|
|
59
|
|
|
// fix current timestamp |
60
|
|
|
$this->setTimestamp(); |
61
|
|
|
|
62
|
|
|
// parsing configuration |
63
|
|
|
$this->configuration = new Configuration( DefaultConfiguration::get() ); |
|
|
|
|
64
|
|
|
|
65
|
1 |
|
$this->configuration->merge($configuration); |
|
|
|
|
66
|
|
|
|
67
|
|
|
// init core components |
68
|
|
|
$this->logger = is_null($logger) ? LogManager::create($this->configuration()) : $logger; |
|
|
|
|
69
|
|
|
|
70
|
|
|
$this->events = is_null($events) ? new EventsManager($this->logger()) : $events; |
|
|
|
|
71
|
|
|
|
72
|
|
|
$this->cache = is_null($cache) ? DispatcherCache::create($this->configuration(), $this->logger()) : $cache; |
|
|
|
|
73
|
1 |
|
|
74
|
|
|
// init models |
75
|
|
|
$this->extra = new Extra($this->logger()); |
|
|
|
|
76
|
1 |
|
|
77
|
|
|
$this->request = new Request($this->configuration(), $this->logger()); |
|
|
|
|
78
|
|
|
|
79
|
1 |
|
$this->router = new Router($this->configuration(), $this->logger(), $this->cache(), $this->extra()); |
|
|
|
|
80
|
|
|
|
81
|
1 |
|
$this->response = new Response($this->configuration(), $this->logger()); |
|
|
|
|
82
|
|
|
|
83
|
|
|
// we're ready! |
84
|
1 |
|
$this->logger->debug("Dispatcher ready, current date ".date('c', $this->getTimestamp())); |
|
|
|
|
85
|
|
|
|
86
|
1 |
|
} |
87
|
|
|
|
88
|
1 |
|
public function dispatch() { |
89
|
|
|
|
90
|
|
|
$this->logger->debug("Starting to dispatch."); |
|
|
|
|
91
|
1 |
|
|
92
|
|
|
$this->logger->debug("Emitting global dispatcher event."); |
|
|
|
|
93
|
1 |
|
|
94
|
|
|
$this->events->emit( new DispatcherEvent($this) ); |
|
|
|
|
95
|
1 |
|
|
96
|
|
|
if ( $this->configuration->get('enabled') === false ) { |
|
|
|
|
97
|
1 |
|
|
98
|
|
|
$this->logger->debug("Dispatcher disabled, shutting down gracefully."); |
|
|
|
|
99
|
|
|
|
100
|
1 |
|
$status = $this->configuration->get('disabled-status'); |
|
|
|
|
101
|
|
|
|
102
|
1 |
|
$content = $this->configuration->get('disabled-message'); |
|
|
|
|
103
|
|
|
|
104
|
1 |
|
$this->response->status->set($status); |
|
|
|
|
105
|
|
|
|
106
|
1 |
|
$this->response->content->set($content); |
|
|
|
|
107
|
|
|
|
108
|
|
|
return $this->shutdown(); |
109
|
|
|
|
110
|
1 |
|
} |
111
|
|
|
|
112
|
1 |
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request') ); |
|
|
|
|
113
|
|
|
|
114
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.'.$this->request->method->get()) ); |
|
|
|
|
115
|
|
|
|
116
|
1 |
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.#') ); |
|
|
|
|
117
|
|
|
|
118
|
1 |
|
if ( $this->readCache() ) { |
119
|
|
|
|
120
|
|
|
return $this->shutdown(); |
121
|
|
|
|
122
|
1 |
|
} |
123
|
|
|
|
124
|
1 |
|
$this->logger->debug("Starting router."); |
|
|
|
|
125
|
|
|
|
126
|
|
|
try { |
127
|
|
|
|
128
|
1 |
|
$route = $this->router->route($this->request); |
|
|
|
|
129
|
|
|
|
130
|
1 |
|
$this->route = $route; |
|
|
|
|
131
|
|
|
|
132
|
|
|
} catch (DispatcherException $de) { |
133
|
|
|
|
134
|
1 |
|
$this->logger->debug("Route error (".$de->getStatus()."), shutting down dispatcher."); |
|
|
|
|
135
|
|
|
|
136
|
1 |
|
$this->processDispatcherException($de); |
137
|
|
|
|
138
|
|
|
return $this->shutdown(); |
139
|
|
|
|
140
|
1 |
|
} |
141
|
|
|
|
142
|
1 |
|
$route_type = $route->getType(); |
143
|
|
|
|
144
|
|
|
$route_service = $route->getServiceName(); |
145
|
|
|
|
146
|
1 |
|
$this->logger->debug("Route acquired, type $route_type directed to $route_service."); |
|
|
|
|
147
|
|
|
|
148
|
1 |
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route') ); |
|
|
|
|
149
|
|
|
|
150
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$route_type) ); |
|
|
|
|
151
|
|
|
|
152
|
1 |
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$route_service) ); |
|
|
|
|
153
|
|
|
|
154
|
1 |
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.#') ); |
|
|
|
|
155
|
|
|
|
156
|
1 |
|
// translate route to service |
157
|
|
|
|
158
|
1 |
|
$this->logger->debug("Running $route_service service."); |
|
|
|
|
159
|
|
|
|
160
|
1 |
|
try { |
161
|
|
|
|
162
|
|
|
$this->router->compose($this->response()); |
|
|
|
|
163
|
|
|
|
164
|
|
|
} catch (DispatcherException $de) { |
165
|
|
|
|
166
|
|
|
$this->logger->debug("Service exception (".$de->getStatus()."), shutting down dispatcher."); |
|
|
|
|
167
|
|
|
|
168
|
|
|
$this->processDispatcherException($de); |
169
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$this->processConfigurationParameters($route); |
173
|
|
|
|
174
|
|
|
$this->dumpCache($route); |
175
|
|
|
|
176
|
1 |
|
return $this->shutdown(); |
177
|
|
|
|
178
|
1 |
|
} |
179
|
|
|
|
180
|
1 |
|
private function readCache() { |
181
|
|
|
|
182
|
1 |
|
$name = (string) $this->request->uri; |
|
|
|
|
183
|
|
|
|
184
|
|
|
$cache = $this->cache->setNamespace('dispatcherservice')->get($name); |
|
|
|
|
185
|
|
|
|
186
|
|
|
if ( is_null($cache) ) return false; |
187
|
|
|
|
188
|
1 |
|
$this->response->import($cache); |
|
|
|
|
189
|
|
|
|
190
|
|
|
return true; |
191
|
|
|
|
192
|
1 |
|
} |
193
|
|
|
|
194
|
|
|
private function dumpCache($route) { |
195
|
|
|
|
196
|
1 |
|
$cache = strtoupper($route->getParameter('cache')); |
197
|
|
|
$ttl = $route->getParameter('ttl'); |
198
|
1 |
|
$name = (string) $this->request->uri; |
|
|
|
|
199
|
|
|
$method = $this->request->method->get(); |
|
|
|
|
200
|
1 |
|
$status = $this->response->status->get(); |
|
|
|
|
201
|
|
|
|
202
|
1 |
|
// @NOTE: Server cache will not consider cacheable POST or PUT requests |
203
|
|
|
// because of dispatcher internal structure: if post request is cached |
204
|
|
|
// subsequent requests will never reach the service. |
205
|
|
|
if ( |
206
|
|
|
( $cache == 'SERVER' || $cache == 'BOTH' ) && |
207
|
|
|
in_array($request->method->get(), array('GET', 'HEAD')) && |
|
|
|
|
208
|
|
|
in_array($this->status->get(), array(200, 203, 300, 301, 302, 404, 410)) |
|
|
|
|
209
|
|
|
){ |
210
|
|
|
|
211
|
|
|
$this->cache->setNamespace('dispatcherservice')->set($name, $this->response->export(), $ttl); |
|
|
|
|
212
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
private function processConfigurationParameters($route) { |
218
|
|
|
|
219
|
|
|
$params = $route->getParameter('headers'); |
220
|
|
|
|
221
|
|
|
if ( !empty($params) && is_array($params) ) { |
222
|
|
|
|
223
|
|
|
foreach($params as $name=>$value) $this->response->headers->set($name, $value); |
|
|
|
|
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
private function emitServiceSpecializedEvents($name) { |
229
|
|
|
|
230
|
|
|
$this->logger->debug("Emitting $name service-event."); |
|
|
|
|
231
|
|
|
|
232
|
|
|
return new ServiceEvent( |
233
|
|
|
$name, |
234
|
|
|
$this->logger, |
|
|
|
|
235
|
|
|
$this->request, |
|
|
|
|
236
|
|
|
$this->router, |
|
|
|
|
237
|
|
|
$this->response, |
|
|
|
|
238
|
|
|
$this->extra |
|
|
|
|
239
|
|
|
); |
240
|
|
|
|
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
private function processDispatcherException(DispatcherException $de) { |
244
|
1 |
|
|
245
|
|
|
$status = $de->getStatus(); |
246
|
1 |
|
|
247
|
|
|
$message = $de->getMessage(); |
248
|
1 |
|
|
249
|
|
|
$headers = $de->getHeaders(); |
|
|
|
|
250
|
1 |
|
|
251
|
|
|
$this->response->status->set($status); |
|
|
|
|
252
|
|
|
|
253
|
|
|
$this->response->content->set($message); |
|
|
|
|
254
|
|
|
|
255
|
|
|
$this->response->headers->merge($headers); |
|
|
|
|
256
|
|
|
|
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
private function shutdown() { |
260
|
|
|
|
261
|
|
|
$this->response->consolidate($this->request, $this->route); |
|
|
|
|
262
|
|
|
|
263
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response') ); |
|
|
|
|
264
|
|
|
|
265
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.'.$this->response->status->get()) ); |
|
|
|
|
266
|
|
|
|
267
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.#') ); |
|
|
|
|
268
|
|
|
|
269
|
|
|
$this->logger->debug("Composing return value."); |
|
|
|
|
270
|
|
|
|
271
|
|
|
$return = Processor::parse($this->configuration, $this->logger, $this->request, $this->response); |
|
|
|
|
272
|
|
|
|
273
|
|
|
$this->logger->debug("Dispatcher run-cycle ends."); |
|
|
|
|
274
|
|
|
|
275
|
|
|
if ( function_exists('fastcgi_finish_request') ) fastcgi_finish_request(); |
276
|
|
|
else ob_end_clean(); |
277
|
|
|
|
278
|
|
|
return $return; |
279
|
|
|
|
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
} |
283
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write 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.