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 \SplQueue $pipeline; |
52
|
|
|
private ?RouteCompilerInterface $compiler; |
53
|
|
|
private ?RouteMatcherInterface $matcher = null; |
54
|
|
|
|
55
|
|
|
/** @var array<string,array<int,MiddlewareInterface>> */ |
56
|
|
|
private array $middlewares = []; |
57
|
|
|
|
58
|
|
|
/** @var RouteCollection|(callable(RouteCollection): void)|null */ |
|
|
|
|
59
|
|
|
private $collection; |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** @var CacheItemPoolInterface|string|null */ |
63
|
|
|
private $cacheData; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param CacheItemPoolInterface|string|null $cache use file path or PSR-6 cache |
67
|
|
|
*/ |
68
|
92 |
|
public function __construct(RouteCompilerInterface $compiler = null, $cache = null) |
69
|
|
|
{ |
70
|
92 |
|
$this->compiler = $compiler; |
71
|
92 |
|
$this->pipeline = new \SplQueue(); |
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
|
55 |
|
public function pipe(MiddlewareInterface ...$middlewares): void |
128
|
|
|
{ |
129
|
55 |
|
foreach ($middlewares as $middleware) { |
130
|
55 |
|
$this->pipeline->enqueue($middleware); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Attach a name to a group of middlewares. |
136
|
|
|
*/ |
137
|
4 |
|
public function pipes(string $name, MiddlewareInterface ...$middlewares): void |
138
|
|
|
{ |
139
|
4 |
|
$this->middlewares[$name] = $middlewares; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Sets the RouteCollection instance associated with this Router. |
144
|
|
|
* |
145
|
|
|
* @param (callable(RouteCollection): void) $routeDefinitionCallback takes only one parameter of route collection |
|
|
|
|
146
|
|
|
*/ |
147
|
4 |
|
public function setCollection(callable $routeDefinitionCallback): void |
148
|
|
|
{ |
149
|
4 |
|
$this->collection = $routeDefinitionCallback; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get the RouteCollection instance associated with this Router. |
154
|
|
|
*/ |
155
|
91 |
|
public function getCollection(): RouteCollection |
156
|
|
|
{ |
157
|
91 |
|
if (\is_callable($collection = $this->collection)) { |
158
|
3 |
|
$collection($collection = new RouteCollection()); |
159
|
88 |
|
} elseif (null === $collection) { |
160
|
1 |
|
throw new \RuntimeException(\sprintf('Did you forget to set add the route collection with the "%s".', __CLASS__ . '::setCollection')); |
161
|
|
|
} |
162
|
|
|
|
163
|
90 |
|
return $this->collection = $collection; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Set where cached data will be stored. |
168
|
|
|
* |
169
|
|
|
* @param CacheItemPoolInterface|string $cache use file path or PSR-6 cache |
170
|
|
|
* |
171
|
|
|
* @return void |
172
|
|
|
*/ |
173
|
|
|
public function setCache($cache): void |
174
|
|
|
{ |
175
|
|
|
$this->cacheData = $cache; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* If RouteCollection's data has been cached. |
180
|
|
|
*/ |
181
|
3 |
|
public function isCached(): bool |
182
|
|
|
{ |
183
|
3 |
|
if (null === $cache = $this->cacheData) { |
184
|
1 |
|
return false; |
185
|
|
|
} |
186
|
|
|
|
187
|
2 |
|
return ($cache instanceof CacheItemPoolInterface && $cache->hasItem(__FILE__)) || \file_exists($cache); |
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Gets the Route matcher instance associated with this Router. |
192
|
|
|
*/ |
193
|
92 |
|
public function getMatcher(): RouteMatcherInterface |
194
|
|
|
{ |
195
|
92 |
|
return $this->matcher ??= $this->cacheData ? $this->getCachedData($this->cacheData) : new RouteMatcher($this->getCollection(), $this->compiler); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* {@inheritdoc} |
200
|
|
|
*/ |
201
|
77 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
202
|
|
|
{ |
203
|
77 |
|
$route = $this->getMatcher()->matchRequest($request); |
204
|
|
|
|
205
|
73 |
|
if (null !== $route) { |
206
|
50 |
|
foreach ($route->getPiped() as $middleware) { |
207
|
2 |
|
foreach ($this->middlewares[$middleware] ?? [] as $pipedMiddleware) { |
208
|
2 |
|
$this->pipeline->enqueue($pipedMiddleware); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
73 |
|
return (new Next($this->pipeline, $handler))->handle($request->withAttribute(Route::class, $route)); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param CacheItemPoolInterface|string $cache |
218
|
|
|
*/ |
219
|
2 |
|
protected function getCachedData($cache): RouteMatcherInterface |
220
|
|
|
{ |
221
|
2 |
|
if ($cache instanceof CacheItemPoolInterface) { |
222
|
|
|
$cachedData = ($cacheItem = $cache->getItem(__FILE__))->get(); |
223
|
|
|
|
224
|
|
|
if (!$cachedData instanceof RouteMatcherInterface) { |
225
|
|
|
$cache->deleteItem(__FILE__); |
226
|
|
|
$cache->save($cacheItem->set($cachedData = new RouteMatcher($this->getCollection(), $this->compiler))); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $cacheItem->get(); |
230
|
|
|
} |
231
|
|
|
|
232
|
2 |
|
$cachedData = @include $cache; |
233
|
|
|
|
234
|
2 |
|
if (!$cachedData instanceof RouteMatcherInterface) { |
235
|
1 |
|
$dumpData = "<<<'SERIALIZED'\n" . \serialize(new RouteMatcher($this->getCollection(), $this->compiler)) . "\nSERIALIZED"; |
236
|
|
|
|
237
|
1 |
|
if (!\is_dir($directory = \dirname($cache))) { |
238
|
|
|
@\mkdir($directory, 0775, true); |
|
|
|
|
239
|
|
|
} |
240
|
|
|
|
241
|
1 |
|
\file_put_contents($cache, "<?php // auto generated: AVOID MODIFYING\n\nreturn \unserialize(" . $dumpData . ");\n"); |
242
|
|
|
|
243
|
1 |
|
if (\function_exists('opcache_invalidate') && \filter_var(\ini_get('opcache.enable'), \FILTER_VALIDATE_BOOLEAN)) { |
244
|
1 |
|
@\opcache_invalidate($cache, true); |
|
|
|
|
245
|
|
|
} |
246
|
1 |
|
$cachedData = require $cache; |
247
|
|
|
} |
248
|
|
|
|
249
|
2 |
|
return $cachedData; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|