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::READONLY; |
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->setRaw('events', is_null($events) ? EventsManager::create($this->logger) : $events); |
|
|
|
|
84
|
1 |
|
$this->setRaw('cache', is_null($cache) ? SimpleCacheManager::createFromConfiguration($this->configuration, $this->logger) : $cache); |
|
|
|
|
85
|
|
|
|
86
|
|
|
// init models |
87
|
1 |
|
$this->setRaw('extra', new Extra($this->logger)); |
|
|
|
|
88
|
1 |
|
$this->setRaw('request', new Request($this->configuration, $this->logger)); |
|
|
|
|
89
|
1 |
|
$this->setRaw('router', new Router($this->configuration, $this->logger, $this->cache, $this->extra)); |
|
|
|
|
90
|
1 |
|
$this->setRaw('response', new Response($this->configuration, $this->logger)); |
|
|
|
|
91
|
|
|
|
92
|
|
|
// we're ready! |
93
|
1 |
|
$this->logger->debug("Dispatcher ready"); |
|
|
|
|
94
|
|
|
|
95
|
1 |
|
} |
96
|
|
|
|
97
|
1 |
|
public function dispatch() { |
98
|
|
|
|
99
|
1 |
|
$this->logger->debug("Starting to dispatch."); |
|
|
|
|
100
|
|
|
|
101
|
1 |
|
$this->logger->debug("Emitting global dispatcher event."); |
|
|
|
|
102
|
|
|
|
103
|
1 |
|
$this->events->emit( new DispatcherEvent($this) ); |
|
|
|
|
104
|
|
|
|
105
|
1 |
|
if ( $this->configuration->get('enabled') === false ) { |
|
|
|
|
106
|
|
|
|
107
|
|
|
$this->logger->debug("Dispatcher disabled, shutting down gracefully."); |
|
|
|
|
108
|
|
|
|
109
|
|
|
$status = $this->configuration->get('disabled-status'); |
|
|
|
|
110
|
|
|
|
111
|
|
|
$content = $this->configuration->get('disabled-message'); |
|
|
|
|
112
|
|
|
|
113
|
|
|
$this->response->status->set($status); |
|
|
|
|
114
|
|
|
|
115
|
|
|
$this->response->content->set($content); |
|
|
|
|
116
|
|
|
|
117
|
|
|
return $this->shutdown(); |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
$cache = new ServerCache($this->cache); |
|
|
|
|
122
|
|
|
|
123
|
1 |
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request') ); |
|
|
|
|
124
|
|
|
|
125
|
1 |
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.'.$this->request->method->get()) ); |
|
|
|
|
126
|
|
|
|
127
|
1 |
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.#') ); |
|
|
|
|
128
|
|
|
|
129
|
1 |
|
if ( $cache->read($this->request, $this->response) ) { |
|
|
|
|
130
|
|
|
|
131
|
|
|
return $this->shutdown(); |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
$this->logger->debug("Starting router."); |
|
|
|
|
136
|
|
|
|
137
|
|
|
try { |
138
|
|
|
|
139
|
1 |
|
$this->route = $this->router->route($this->request); |
|
|
|
|
140
|
|
|
|
141
|
1 |
|
} catch (DispatcherException $de) { |
142
|
|
|
|
143
|
1 |
|
$this->logger->debug("Route error (".$de->getStatus()."), shutting down dispatcher."); |
|
|
|
|
144
|
|
|
|
145
|
1 |
|
$this->processDispatcherException($de); |
146
|
|
|
|
147
|
1 |
|
return $this->shutdown(); |
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$route_type = $route->getType(); |
|
|
|
|
152
|
|
|
|
153
|
|
|
$route_service = $route->getServiceName(); |
154
|
|
|
|
155
|
|
|
$this->logger->debug("Route acquired, type $route_type directed to $route_service."); |
|
|
|
|
156
|
|
|
|
157
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route') ); |
|
|
|
|
158
|
|
|
|
159
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$route_type) ); |
|
|
|
|
160
|
|
|
|
161
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$route_service) ); |
|
|
|
|
162
|
|
|
|
163
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.#') ); |
|
|
|
|
164
|
|
|
|
165
|
|
|
// translate route to service |
166
|
|
|
|
167
|
|
|
$this->logger->debug("Running $route_service service."); |
|
|
|
|
168
|
|
|
|
169
|
|
|
try { |
170
|
|
|
|
171
|
|
|
$this->router->compose($this->response); |
|
|
|
|
172
|
|
|
|
173
|
|
|
} catch (DispatcherException $de) { |
174
|
|
|
|
175
|
|
|
$this->logger->debug("Service exception (".$de->getStatus()."), shutting down dispatcher."); |
|
|
|
|
176
|
|
|
|
177
|
|
|
$this->processDispatcherException($de); |
178
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$this->processConfigurationParameters($this->route); |
|
|
|
|
182
|
|
|
|
183
|
|
|
$cache->dump($this->request, $this->response, $this->route); |
|
|
|
|
184
|
|
|
|
185
|
|
|
return $this->shutdown(); |
186
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
private function processConfigurationParameters($route) { |
190
|
|
|
|
191
|
|
|
$params = $route->getParameter('headers'); |
192
|
|
|
|
193
|
|
|
if ( !empty($params) && is_array($params) ) { |
194
|
|
|
|
195
|
|
|
foreach($params as $name=>$value) $this->response->headers->set($name, $value); |
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
1 |
|
private function emitServiceSpecializedEvents($name) { |
201
|
|
|
|
202
|
1 |
|
$this->logger->debug("Emitting $name service-event."); |
|
|
|
|
203
|
|
|
|
204
|
1 |
|
return new ServiceEvent( |
205
|
1 |
|
$name, |
206
|
1 |
|
$this->logger, |
|
|
|
|
207
|
1 |
|
$this->request, |
|
|
|
|
208
|
1 |
|
$this->router, |
|
|
|
|
209
|
1 |
|
$this->response, |
|
|
|
|
210
|
1 |
|
$this->extra |
|
|
|
|
211
|
1 |
|
); |
212
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
1 |
|
private function processDispatcherException(DispatcherException $de) { |
216
|
|
|
|
217
|
1 |
|
$status = $de->getStatus(); |
218
|
|
|
|
219
|
1 |
|
$message = $de->getMessage(); |
220
|
|
|
|
221
|
1 |
|
$headers = $de->getHeaders(); |
222
|
|
|
|
223
|
1 |
|
$this->response->status->set($status); |
|
|
|
|
224
|
|
|
|
225
|
1 |
|
$this->response->content->set($message); |
|
|
|
|
226
|
|
|
|
227
|
1 |
|
$this->response->headers->merge($headers); |
|
|
|
|
228
|
|
|
|
229
|
1 |
|
} |
230
|
|
|
|
231
|
1 |
|
private function shutdown() { |
232
|
|
|
|
233
|
1 |
|
$this->response->consolidate($this->request, $this->route); |
|
|
|
|
234
|
|
|
|
235
|
1 |
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response') ); |
|
|
|
|
236
|
|
|
|
237
|
1 |
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.'.$this->response->status->get()) ); |
|
|
|
|
238
|
|
|
|
239
|
1 |
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.#') ); |
|
|
|
|
240
|
|
|
|
241
|
1 |
|
$this->logger->debug("Composing return value."); |
|
|
|
|
242
|
|
|
|
243
|
1 |
|
$return = Processor::parse($this->configuration, $this->logger, $this->request, $this->response); |
|
|
|
|
244
|
|
|
|
245
|
1 |
|
$this->logger->debug("Dispatcher run-cycle ends."); |
|
|
|
|
246
|
|
|
|
247
|
1 |
|
if ( function_exists('fastcgi_finish_request') ) fastcgi_finish_request(); |
248
|
1 |
|
else ob_end_clean(); |
249
|
|
|
|
250
|
1 |
|
return $return; |
251
|
|
|
|
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
} |
255
|
|
|
|
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.