1
|
|
|
<?php namespace Comodojo\Dispatcher; |
2
|
|
|
|
3
|
|
|
use \Monolog\Logger; |
4
|
|
|
use \Comodojo\Dispatcher\Components\Configuration; |
5
|
|
|
use \Comodojo\Dispatcher\Request\Model as Request; |
6
|
|
|
use \Comodojo\Dispatcher\Router\Collector as RouteCollector; |
7
|
|
|
use \Comodojo\Dispatcher\Response\Model as Response; |
8
|
|
|
use \Comodojo\Dispatcher\Extra\Model as Extra; |
9
|
|
|
use \Comodojo\Dispatcher\Components\Timestamp as TimestampTrait; |
10
|
|
|
use \Comodojo\Dispatcher\Output\Processor; |
11
|
|
|
use \Comodojo\Dispatcher\Events\DispatcherEvent; |
12
|
|
|
use \Comodojo\Dispatcher\Events\ServiceEvent; |
13
|
|
|
use \Comodojo\Dispatcher\Router\RoutingTableInterface; |
14
|
|
|
use \Comodojo\Dispatcher\Log\DispatcherLogger; |
15
|
|
|
use \Comodojo\Dispatcher\Cache\DispatcherCache; |
16
|
|
|
use \Comodojo\Cache\CacheManager; |
17
|
|
|
use \League\Event\Emitter; |
18
|
|
|
use \Comodojo\Exception\DispatcherException; |
19
|
|
|
use \Exception; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @package Comodojo Dispatcher |
23
|
|
|
* @author Marco Giovinazzi <[email protected]> |
24
|
|
|
* @author Marco Castiello <[email protected]> |
25
|
|
|
* @license GPL-3.0+ |
26
|
|
|
* |
27
|
|
|
* LICENSE: |
28
|
|
|
* |
29
|
|
|
* This program is free software: you can redistribute it and/or modify |
30
|
|
|
* it under the terms of the GNU Affero General Public License as |
31
|
|
|
* published by the Free Software Foundation, either version 3 of the |
32
|
|
|
* License, or (at your option) any later version. |
33
|
|
|
* |
34
|
|
|
* This program is distributed in the hope that it will be useful, |
35
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
36
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
37
|
|
|
* GNU Affero General Public License for more details. |
38
|
|
|
* |
39
|
|
|
* You should have received a copy of the GNU Affero General Public License |
40
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
41
|
|
|
*/ |
42
|
|
|
|
43
|
|
|
class Dispatcher { |
44
|
|
|
|
45
|
|
|
use TimestampTrait; |
46
|
|
|
|
47
|
|
|
private $configuration; |
48
|
|
|
|
49
|
|
|
private $request; |
50
|
|
|
|
51
|
|
|
private $router; |
52
|
|
|
|
53
|
|
|
private $response; |
54
|
|
|
|
55
|
|
|
private $extra; |
56
|
|
|
|
57
|
|
|
private $logger; |
58
|
|
|
|
59
|
|
|
private $cache; |
60
|
|
|
|
61
|
|
|
private $events; |
62
|
|
|
|
63
|
|
|
public function __construct( |
64
|
|
|
$configuration = array(), |
65
|
|
|
Emitter $emitter = null, |
66
|
|
|
CacheManager $cache = null, |
67
|
|
|
Logger $logger = null |
68
|
|
|
) { |
69
|
|
|
|
70
|
|
|
ob_start(); |
71
|
|
|
|
72
|
|
|
$this->setTimestamp(); |
73
|
|
|
|
74
|
|
|
$this->extra = new Extra($this->logger); |
|
|
|
|
75
|
|
|
|
76
|
|
|
$this->configuration = new Configuration($this->getDefaultConfiguration()); |
77
|
|
|
|
78
|
|
|
$this->configuration->merge($configuration); |
79
|
|
|
|
80
|
|
|
$this->events = is_null($emitter) ? new Emitter() : $emitter; |
81
|
|
|
|
82
|
|
|
$this->logger = is_null($logger) ? DispatcherLogger::create($this->configuration) : $logger; |
83
|
|
|
|
84
|
|
|
$this->cache = is_null($cache) ? DispatcherCache::create($this->configuration, $this->logger) : $cache; |
85
|
|
|
|
86
|
|
|
$this->request = new Request($this->configuration, $this->logger); |
87
|
|
|
|
88
|
|
|
$this->router = new RouteCollector($this->configuration, $this->logger, $this->cache, $this->extra); |
89
|
|
|
|
90
|
|
|
$this->response = new Response($this->configuration, $this->logger); |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function configuration() { |
95
|
|
|
|
96
|
|
|
return $this->configuration; |
97
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function events() { |
101
|
|
|
|
102
|
|
|
return $this->events; |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function cache() { |
107
|
|
|
|
108
|
|
|
return $this->cache; |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function request() { |
113
|
|
|
|
114
|
|
|
return $this->request; |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function router() { |
119
|
|
|
|
120
|
|
|
return $this->router; |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function response() { |
125
|
|
|
|
126
|
|
|
return $this->response; |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function extra() { |
131
|
|
|
|
132
|
|
|
return $this->extra; |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function dispatch() { |
137
|
|
|
|
138
|
|
|
$this->events->emit( new DispatcherEvent($this) ); |
139
|
|
|
|
140
|
|
|
if ( $this->configuration()->get('dispatcher-enabled') === false ) { |
141
|
|
|
|
142
|
|
|
$status = $this->configuration()->get('dispatcher-disabled-status'); |
143
|
|
|
|
144
|
|
|
$content = $this->configuration()->get('dispatcher-disabled-message'); |
145
|
|
|
|
146
|
|
|
$this->response()->status()->set($status); |
147
|
|
|
|
148
|
|
|
$this->response()->content()->set($content); |
149
|
|
|
|
150
|
|
|
return $this->shutdown(); |
151
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request') ); |
155
|
|
|
|
156
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.'.$this->request->method()->get()) ); |
157
|
|
|
|
158
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.#') ); |
159
|
|
|
|
160
|
|
|
try { |
161
|
|
|
|
162
|
|
|
$this->router->route($this->request); |
163
|
|
|
|
164
|
|
|
} catch (DispatcherException $de) { |
165
|
|
|
|
166
|
|
|
$this->response()->status()->set( $de->getStatus() ); |
|
|
|
|
167
|
|
|
|
168
|
|
|
$this->response()->content()->set( $de->getMessage() ); |
169
|
|
|
|
170
|
|
|
return $this->shutdown(); |
171
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route') ); |
175
|
|
|
|
176
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$this->router->getType()) ); |
177
|
|
|
|
178
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$this->router->getService()) ); |
179
|
|
|
|
180
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.#') ); |
181
|
|
|
|
182
|
|
|
// translate route to service |
183
|
|
|
|
184
|
|
|
try { |
185
|
|
|
|
186
|
|
|
$this->router->compose($this->response); |
187
|
|
|
|
188
|
|
|
} catch (DispatcherException $de) { |
189
|
|
|
|
190
|
|
|
$this->response()->status()->set( $de->getStatus() ); |
|
|
|
|
191
|
|
|
|
192
|
|
|
$this->response()->content()->set( $de->getMessage() ); |
193
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return $this->shutdown(); |
197
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
private function emitServiceSpecializedEvents($name) { |
201
|
|
|
|
202
|
|
|
return new ServiceEvent( |
203
|
|
|
$name, |
204
|
|
|
$this->logger, |
205
|
|
|
$this->request, |
206
|
|
|
$this->router, |
207
|
|
|
$this->response, |
208
|
|
|
$this->extra |
209
|
|
|
); |
210
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
private function shutdown() { |
214
|
|
|
|
215
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response') ); |
216
|
|
|
|
217
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.'.$this->response->status()->get()) ); |
218
|
|
|
|
219
|
|
|
$this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.#') ); |
220
|
|
|
|
221
|
|
|
$return = Processor::parse($this->configuration, $this->logger, $this->response); |
222
|
|
|
|
223
|
|
|
ob_end_clean(); |
224
|
|
|
|
225
|
|
|
return $return; |
226
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
private function getDefaultConfiguration() { |
230
|
|
|
|
231
|
|
|
return array( |
232
|
|
|
'dispatcher-enabled' => true, |
233
|
|
|
'dispatcher-disabled-status' => 503, |
234
|
|
|
'dispatcher-disabled-message' => 'Dispatcher offline', |
235
|
|
|
'dispatcher-log-name' => 'dispatcher', |
236
|
|
|
'dispatcher-log-enabled' => false, |
237
|
|
|
'dispatcher-log-level' => 'INFO', |
238
|
|
|
'dispatcher-log-target' => '%dispatcher-log-folder%/dispatcher.log', |
239
|
|
|
'dispatcher-log-folder' => '/log', |
240
|
|
|
'dispatcher-supported-methods' => array('GET','PUT','POST','DELETE','OPTIONS','HEAD'), |
241
|
|
|
'dispatcher-default-encoding' => 'UTF-8', |
242
|
|
|
'dispatcher-cache-enabled' => true, |
243
|
|
|
'dispatcher-cache-ttl' => 3600, |
244
|
|
|
'dispatcher-cache-folder' => '/cache', |
245
|
|
|
'dispatcher-cache-algorithm' => 'PICK_FIRST', |
246
|
|
|
// should we implement this? |
247
|
|
|
//'dispatcher-autoroute' => false, |
|
|
|
|
248
|
|
|
'dispatcher-base-url' => self::urlGetAbsolute(), |
249
|
|
|
'dispatcher-real-path' => self::pathGetAbsolute() |
250
|
|
|
); |
251
|
|
|
|
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
private static function urlGetAbsolute() { |
|
|
|
|
255
|
|
|
|
256
|
|
|
$http = 'http' . ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 's' : '') . '://'; |
257
|
|
|
|
258
|
|
|
$uri = preg_replace("/\/index.php(.*?)$/i", "", $_SERVER['PHP_SELF']); |
259
|
|
|
|
260
|
|
|
return ( $http . $_SERVER['HTTP_HOST'] . $uri . "/" ); |
261
|
|
|
|
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
private static function pathGetAbsolute() { |
265
|
|
|
|
266
|
|
|
return realpath(dirname(__FILE__)."/../../../../")."/"; |
267
|
|
|
|
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
} |
271
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.