forked from Respect/Rest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouter.php
More file actions
352 lines (280 loc) · 10.1 KB
/
Router.php
File metadata and controls
352 lines (280 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
declare(strict_types=1);
namespace Respect\Rest;
use InvalidArgumentException;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use ReflectionClass;
use Respect\Rest\Routes\AbstractRoute;
use Throwable;
use function array_pop;
use function class_exists;
use function count;
use function interface_exists;
use function is_array;
use function is_callable;
use function is_string;
use function preg_match;
use function rtrim;
use function substr_count;
use function trigger_error;
use function usort;
use const E_USER_ERROR;
/**
* A router that contains many instances of routes.
*
* @method AbstractRoute get(string $path, mixed $routeTarget)
* @method AbstractRoute post(string $path, mixed $routeTarget)
* @method AbstractRoute put(string $path, mixed $routeTarget)
* @method AbstractRoute delete(string $path, mixed $routeTarget)
* @method AbstractRoute head(string $path, mixed $routeTarget)
* @method AbstractRoute options(string $path, mixed $routeTarget)
* @method AbstractRoute patch(string $path, mixed $routeTarget)
* @method AbstractRoute any(string $path, mixed $routeTarget)
*/
final class Router implements MiddlewareInterface, RequestHandlerInterface, RouteProvider
{
public DispatchContext|null $context = null;
/** @var array<int, Routines\Routinable> */
protected array $globalRoutines = [];
/** @var array<int, AbstractRoute> */
protected array $routes = [];
/** @var array<int, AbstractRoute> */
protected array $sideRoutes = [];
private DispatchEngine|null $dispatchEngine = null;
public function __construct(
protected string $basePath,
private ResponseFactoryInterface&StreamFactoryInterface $factory,
) {
$this->basePath = rtrim($basePath, '/');
}
public function always(string $routineName, mixed ...$params): static
{
/** @var class-string<Routines\Routinable> $routineClassName */
$routineClassName = 'Respect\\Rest\\Routines\\' . $routineName;
$routineClass = new ReflectionClass($routineClassName);
$routineInstance = $routineClass->newInstanceArgs($params);
$this->globalRoutines[] = $routineInstance;
foreach ($this->routes as $route) {
$route->appendRoutine($routineInstance);
}
return $this;
}
public function appendRoute(AbstractRoute $route): static
{
$this->routes[] = $route;
$route->sideRoutes = &$this->sideRoutes;
$route->basePath = $this->basePath;
foreach ($this->globalRoutines as $routine) {
$route->appendRoutine($routine);
}
$this->sortRoutesByComplexity();
return $this;
}
public function appendSideRoute(AbstractRoute $route): static
{
$this->sideRoutes[] = $route;
foreach ($this->globalRoutines as $routine) {
$route->appendRoutine($routine);
}
return $this;
}
/** @param array<int, mixed> $arguments */
public function callbackRoute(
string $method,
string $path,
callable $callback,
array $arguments = [],
): Routes\Callback {
$route = new Routes\Callback($method, $path, $callback, $arguments);
$this->appendRoute($route);
return $route;
}
/** @param array<int, mixed> $arguments */
public function classRoute(string $method, string $path, string $class, array $arguments = []): Routes\ClassName
{
$route = new Routes\ClassName($method, $path, $class, $arguments);
$this->appendRoute($route);
return $route;
}
public function dispatch(ServerRequestInterface $serverRequest): DispatchContext
{
return $this->dispatchEngine()->dispatch($serverRequest);
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
return $this->dispatchEngine()->handle($request);
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$context = $this->dispatch($request);
if ($context->route === null && !$context->hasPreparedResponse()) {
return $handler->handle($request);
}
if ($context->route === null) {
$response = $context->response();
if ($response !== null && $response->getStatusCode() === 404) {
return $handler->handle($request);
}
return $response ?? $handler->handle($request);
}
return $context->response() ?? $handler->handle($request);
}
public function createDispatchContext(ServerRequestInterface $serverRequest): DispatchContext
{
return new DispatchContext(
$serverRequest,
$this->factory,
);
}
public function dispatchContext(DispatchContext $context): DispatchContext
{
return $this->dispatchEngine()->dispatchContext($context);
}
public function exceptionRoute(string $className, callable $callback): Routes\Exception
{
$route = new Routes\Exception($className, $callback);
$this->appendSideRoute($route);
return $route;
}
public function errorRoute(callable $callback): Routes\Error
{
$route = new Routes\Error($callback);
$this->appendSideRoute($route);
return $route;
}
public function statusRoute(int|null $statusCode, callable $callback): Routes\Status
{
$route = new Routes\Status($statusCode, $callback);
$this->appendSideRoute($route);
return $route;
}
/** @return array<int, Routes\AbstractRoute> */
public function getSideRoutes(): array
{
return $this->sideRoutes;
}
public function factoryRoute(string $method, string $path, string $className, callable $factory): Routes\Factory
{
$route = new Routes\Factory($method, $path, $className, $factory);
$this->appendRoute($route);
return $route;
}
public function instanceRoute(string $method, string $path, object $instance): Routes\Instance
{
$route = new Routes\Instance($method, $path, $instance);
$this->appendRoute($route);
return $route;
}
public function staticRoute(string $method, string $path, mixed $staticValue): Routes\StaticValue
{
$route = new Routes\StaticValue($method, $path, $staticValue);
$this->appendRoute($route);
return $route;
}
/** @return array<int, AbstractRoute> */
public function getRoutes(): array
{
return $this->routes;
}
public function getBasePath(): string
{
return $this->basePath;
}
public function dispatchEngine(): DispatchEngine
{
return $this->dispatchEngine ??= new DispatchEngine(
$this,
$this->factory,
function (DispatchContext $ctx): void {
$this->context = $ctx;
},
);
}
public static function compareOcurrences(string $patternA, string $patternB, string $sub): bool
{
return substr_count($patternA, $sub) < substr_count($patternB, $sub);
}
protected function sortRoutesByComplexity(): void
{
usort(
$this->routes,
static function (AbstractRoute $a, AbstractRoute $b): int {
$pa = $a->pattern;
$pb = $b->pattern;
if ($pa === $pb) {
return 0;
}
$slashCount = Router::compareOcurrences($pa, $pb, '/');
$aCatchall = preg_match('#/\*\*$#', $pa);
$bCatchall = preg_match('#/\*\*$#', $pb);
if ($aCatchall != $bCatchall) {
return $aCatchall ? 1 : -1;
}
if ($aCatchall && $bCatchall) {
return $slashCount ? 1 : -1;
}
if (Router::compareOcurrences($pa, $pb, AbstractRoute::PARAM_IDENTIFIER)) {
return -1;
}
return $slashCount ? -1 : 1;
},
);
}
public function __toString(): string
{
$string = '';
try {
$response = $this->context?->response();
if ($response !== null) {
$string = (string) $response->getBody();
}
} catch (Throwable $exception) {
trigger_error($exception->getMessage(), E_USER_ERROR);
}
return $string;
}
/** @param array<int, mixed> $args */
public function __call(string $method, array $args): AbstractRoute
{
if (count($args) < 2) {
throw new InvalidArgumentException(
'Any route binding must have at least 2 arguments',
);
}
[$path, $routeTarget] = $args;
if (is_array($path)) {
$lastPath = array_pop($path);
foreach ($path as $p) {
$this->$method($p, $routeTarget);
}
return $this->$method($lastPath, $routeTarget);
}
if (is_callable($routeTarget)) {
if (!isset($args[2])) {
return $this->callbackRoute($method, $path, $routeTarget);
}
return $this->callbackRoute($method, $path, $routeTarget, $args[2]);
}
if ($routeTarget instanceof Routable) {
return $this->instanceRoute($method, $path, $routeTarget);
}
if (!is_string($routeTarget)) {
return $this->staticRoute($method, $path, $routeTarget);
}
if (!class_exists($routeTarget) && !interface_exists($routeTarget)) {
return $this->staticRoute($method, $path, $routeTarget);
}
if (!isset($args[2])) {
return $this->classRoute($method, $path, $routeTarget);
}
if (is_callable($args[2])) {
return $this->factoryRoute($method, $path, $routeTarget, $args[2]);
}
return $this->classRoute($method, $path, $routeTarget, $args[2]);
}
}