1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Flight Routing. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.4 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Flight\Routing; |
19
|
|
|
|
20
|
|
|
use Fig\Http\Message\RequestMethodInterface; |
21
|
|
|
use Flight\Routing\Generator\GeneratedUri; |
22
|
|
|
use Flight\Routing\Interfaces\{RouteCompilerInterface, RouteMatcherInterface}; |
23
|
|
|
use Laminas\Stratigility\Next; |
24
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
25
|
|
|
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface, UriInterface}; |
26
|
|
|
use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface}; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Aggregate routes for matching and Dispatching. |
30
|
|
|
* |
31
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class Router implements RouteMatcherInterface, RequestMethodInterface, MiddlewareInterface |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Standard HTTP methods for browser requests. |
37
|
|
|
*/ |
38
|
|
|
public const HTTP_METHODS_STANDARD = [ |
39
|
|
|
self::METHOD_HEAD, |
40
|
|
|
self::METHOD_GET, |
41
|
|
|
self::METHOD_POST, |
42
|
|
|
self::METHOD_PUT, |
43
|
|
|
self::METHOD_PATCH, |
44
|
|
|
self::METHOD_DELETE, |
45
|
|
|
self::METHOD_PURGE, |
46
|
|
|
self::METHOD_OPTIONS, |
47
|
|
|
self::METHOD_TRACE, |
48
|
|
|
self::METHOD_CONNECT, |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
private ?RouteCompilerInterface $compiler; |
52
|
|
|
private ?RouteMatcherInterface $matcher = null; |
53
|
|
|
private ?\SplQueue $pipeline = null; |
54
|
|
|
private string $matcherClass = RouteMatcher::class; |
55
|
|
|
|
56
|
|
|
/** @var array<string,array<int,MiddlewareInterface>> */ |
57
|
|
|
private array $middlewares = []; |
58
|
|
|
|
59
|
|
|
/** @var RouteCollection|(callable(RouteCollection): void)|null */ |
|
|
|
|
60
|
|
|
private $collection; |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** @var CacheItemPoolInterface|string|null */ |
64
|
|
|
private $cacheData; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param CacheItemPoolInterface|string|null $cache use file path or PSR-6 cache |
68
|
|
|
*/ |
69
|
92 |
|
public function __construct(RouteCompilerInterface $compiler = null, $cache = null) |
70
|
|
|
{ |
71
|
92 |
|
$this->compiler = $compiler; |
72
|
92 |
|
$this->cacheData = $cache; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set a route collection instance into Router in order to use addRoute method. |
77
|
|
|
* |
78
|
|
|
* @param CacheItemPoolInterface|string|null $cache use file path or PSR-6 cache |
79
|
|
|
* |
80
|
|
|
* @return static |
81
|
|
|
*/ |
82
|
87 |
|
public static function withCollection(RouteCollection $collection = null, RouteCompilerInterface $compiler = null, $cache = null) |
83
|
|
|
{ |
84
|
87 |
|
$new = new static($compiler, $cache); |
85
|
87 |
|
$new->collection = $collection ?? new RouteCollection(); |
86
|
|
|
|
87
|
87 |
|
return $new; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* This method works only if withCollection method is used. |
92
|
|
|
*/ |
93
|
78 |
|
public function addRoute(Route ...$routes): void |
94
|
|
|
{ |
95
|
78 |
|
if ($this->collection instanceof RouteCollection) { |
96
|
78 |
|
$this->collection->routes($routes); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
2 |
|
public function match(string $method, UriInterface $uri): ?Route |
104
|
|
|
{ |
105
|
2 |
|
return $this->getMatcher()->match($method, $uri); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
7 |
|
public function matchRequest(ServerRequestInterface $request): ?Route |
112
|
|
|
{ |
113
|
7 |
|
return $this->getMatcher()->matchRequest($request); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
7 |
|
public function generateUri(string $routeName, array $parameters = []): GeneratedUri |
120
|
|
|
{ |
121
|
7 |
|
return $this->getMatcher()->generateUri($routeName, $parameters); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Attach middleware to the pipeline. |
126
|
|
|
*/ |
127
|
56 |
|
public function pipe(MiddlewareInterface ...$middlewares): void |
128
|
|
|
{ |
129
|
56 |
|
if (null === $this->pipeline) { |
130
|
56 |
|
$this->pipeline = new \SplQueue(); |
131
|
|
|
} |
132
|
|
|
|
133
|
56 |
|
foreach ($middlewares as $middleware) { |
134
|
56 |
|
$this->pipeline->enqueue($middleware); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Attach a name to a group of middlewares. |
140
|
|
|
*/ |
141
|
4 |
|
public function pipes(string $name, MiddlewareInterface ...$middlewares): void |
142
|
|
|
{ |
143
|
4 |
|
$this->middlewares[$name] = $middlewares; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Sets the RouteCollection instance associated with this Router. |
148
|
|
|
* |
149
|
|
|
* @param (callable(RouteCollection): void) $routeDefinitionCallback takes only one parameter of route collection |
|
|
|
|
150
|
|
|
*/ |
151
|
4 |
|
public function setCollection(callable $routeDefinitionCallback): void |
152
|
|
|
{ |
153
|
4 |
|
$this->collection = $routeDefinitionCallback; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get the RouteCollection instance associated with this Router. |
158
|
|
|
*/ |
159
|
91 |
|
public function getCollection(): RouteCollection |
160
|
|
|
{ |
161
|
91 |
|
if (\is_callable($collection = $this->collection)) { |
162
|
3 |
|
$collection($collection = new RouteCollection()); |
163
|
88 |
|
} elseif (null === $collection) { |
164
|
1 |
|
throw new \RuntimeException(\sprintf('Did you forget to set add the route collection with the "%s".', __CLASS__ . '::setCollection')); |
165
|
|
|
} |
166
|
|
|
|
167
|
90 |
|
return $this->collection = $collection; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Set where cached data will be stored. |
172
|
|
|
* |
173
|
|
|
* @param CacheItemPoolInterface|string $cache use file path or PSR-6 cache |
174
|
|
|
* |
175
|
|
|
* @return void |
176
|
|
|
*/ |
177
|
|
|
public function setCache($cache): void |
178
|
|
|
{ |
179
|
|
|
$this->cacheData = $cache; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* If RouteCollection's data has been cached. |
184
|
|
|
*/ |
185
|
3 |
|
public function isCached(): bool |
186
|
|
|
{ |
187
|
3 |
|
if (null === $cache = $this->cacheData) { |
188
|
1 |
|
return false; |
189
|
|
|
} |
190
|
|
|
|
191
|
2 |
|
return ($cache instanceof CacheItemPoolInterface && $cache->hasItem(__FILE__)) || \file_exists($cache); |
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Set a matcher class associated with this Router. |
196
|
|
|
*/ |
197
|
|
|
public function setMatcher(string $matcherClass): void |
198
|
|
|
{ |
199
|
|
|
if (!\is_subclass_of($matcherClass, RouteMatcherInterface::class)) { |
200
|
|
|
throw new \InvalidArgumentException(\sprintf('"%s" must be a subclass of "%s".', $matcherClass, RouteMatcherInterface::class)); |
201
|
|
|
} |
202
|
|
|
$this->matcherClass = $matcherClass; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Gets the Route matcher instance associated with this Router. |
207
|
|
|
*/ |
208
|
92 |
|
public function getMatcher(): RouteMatcherInterface |
209
|
|
|
{ |
210
|
92 |
|
return $this->matcher ??= $this->cacheData ? $this->getCachedData($this->cacheData) : new $this->matcherClass($this->getCollection(), $this->compiler); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* {@inheritdoc} |
215
|
|
|
*/ |
216
|
77 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
217
|
|
|
{ |
218
|
77 |
|
$route = $this->getMatcher()->matchRequest($request); |
219
|
|
|
|
220
|
73 |
|
if (null !== $route) { |
221
|
50 |
|
foreach ($route->getPiped() as $middleware) { |
222
|
2 |
|
if (isset($this->middlewares[$middleware])) { |
223
|
2 |
|
$this->pipe(...$this->middlewares[$middleware]); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
73 |
|
if (null !== $this->pipeline) { |
229
|
56 |
|
$handler = new Next($this->pipeline, $handler); |
230
|
|
|
} |
231
|
|
|
|
232
|
73 |
|
return $handler->handle($request->withAttribute(Route::class, $route)); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param CacheItemPoolInterface|string $cache |
237
|
|
|
*/ |
238
|
2 |
|
protected function getCachedData($cache): RouteMatcherInterface |
239
|
|
|
{ |
240
|
2 |
|
if ($cache instanceof CacheItemPoolInterface) { |
241
|
|
|
$cachedData = ($cacheItem = $cache->getItem(__FILE__))->get(); |
242
|
|
|
|
243
|
|
|
if (!$cachedData instanceof RouteMatcherInterface) { |
244
|
|
|
$cache->deleteItem(__FILE__); |
245
|
|
|
$cache->save($cacheItem->set($cachedData = new $this->matcherClass($this->getCollection(), $this->compiler))); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return $cacheItem->get(); |
249
|
|
|
} |
250
|
|
|
|
251
|
2 |
|
$cachedData = @include $cache; |
252
|
|
|
|
253
|
2 |
|
if (!$cachedData instanceof RouteMatcherInterface) { |
254
|
1 |
|
$dumpData = "<<<'SERIALIZED'\n" . \serialize(new $this->matcherClass($this->getCollection(), $this->compiler)) . "\nSERIALIZED"; |
255
|
|
|
|
256
|
1 |
|
if (!\is_dir($directory = \dirname($cache))) { |
257
|
|
|
@\mkdir($directory, 0775, true); |
|
|
|
|
258
|
|
|
} |
259
|
|
|
|
260
|
1 |
|
\file_put_contents($cache, "<?php // auto generated: AVOID MODIFYING\n\nreturn \unserialize(" . $dumpData . ");\n"); |
261
|
|
|
|
262
|
1 |
|
if (\function_exists('opcache_invalidate') && \filter_var(\ini_get('opcache.enable'), \FILTER_VALIDATE_BOOLEAN)) { |
263
|
1 |
|
@\opcache_invalidate($cache, true); |
|
|
|
|
264
|
|
|
} |
265
|
1 |
|
$cachedData = require $cache; |
266
|
|
|
} |
267
|
|
|
|
268
|
2 |
|
return $cachedData; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|