1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Antidot\React\Router; |
4
|
|
|
|
5
|
|
|
use Antidot\Application\Http\Middleware\CallableMiddleware; |
6
|
|
|
use Antidot\Application\Http\Middleware\PipedRouteMiddleware; |
7
|
|
|
use Antidot\Application\Http\Route; |
8
|
|
|
use Antidot\Application\Http\Router; |
9
|
|
|
use Antidot\Container\MiddlewareFactory; |
10
|
|
|
use Antidot\Container\RequestHandlerFactory; |
11
|
|
|
use Antidot\React\MiddlewarePipeline; |
12
|
|
|
use FastRoute\DataGenerator\GroupCountBased; |
|
|
|
|
13
|
|
|
use FastRoute\Dispatcher; |
|
|
|
|
14
|
|
|
use FastRoute\RouteCollector; |
|
|
|
|
15
|
|
|
use FastRoute\RouteParser\Std; |
|
|
|
|
16
|
|
|
use LogicException; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
19
|
|
|
use React\EventLoop\LoopInterface; |
20
|
|
|
|
21
|
|
|
class ReactFastRouter implements Router |
22
|
|
|
{ |
23
|
|
|
private $routeCollector; |
24
|
|
|
private $middlewareFactory; |
25
|
|
|
private $requestHandlerFactory; |
26
|
|
|
private $dispatcher; |
27
|
|
|
private $loadedMiddleware; |
28
|
|
|
private $loop; |
29
|
|
|
|
30
|
|
|
public function __construct( |
31
|
|
|
MiddlewareFactory $middlewareFactory, |
32
|
|
|
RequestHandlerFactory $requestHandlerFactory, |
33
|
|
|
LoopInterface $loop |
34
|
|
|
) { |
35
|
|
|
$this->routeCollector = new RouteCollector(new Std(), new GroupCountBased()); |
36
|
|
|
$this->middlewareFactory = $middlewareFactory; |
37
|
|
|
$this->requestHandlerFactory = $requestHandlerFactory; |
38
|
|
|
$this->loop = $loop; |
39
|
|
|
$this->loadedMiddleware = []; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function append(Route $route): void |
43
|
|
|
{ |
44
|
|
|
$this->routeCollector->addRoute($route->method(), $route->path(), $route->pipeline()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function match(ServerRequestInterface $request): PipedRouteMiddleware |
48
|
|
|
{ |
49
|
|
|
if (null === $this->dispatcher) { |
50
|
|
|
$this->dispatcher = new Dispatcher\GroupCountBased($this->routeCollector->getData()); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
$routeInfo = $this->dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath()); |
53
|
|
|
$handler = array_key_last($routeInfo[1]); |
54
|
|
|
switch ($routeInfo[0]) { |
55
|
|
|
case Dispatcher::NOT_FOUND: |
56
|
|
|
case Dispatcher::METHOD_NOT_ALLOWED: |
57
|
|
|
if (array_key_exists($handler, $this->loadedMiddleware)) { |
58
|
|
|
return $this->loadedMiddleware[$handler]; |
59
|
|
|
} |
60
|
|
|
$this->loadedMiddleware[$handler] = new PipedRouteMiddleware( |
61
|
|
|
new MiddlewarePipeline($this->loop), |
62
|
|
|
true, [] |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
return $this->loadedMiddleware[$routeInfo[1]]; |
66
|
|
|
case Dispatcher::FOUND: |
67
|
|
|
if (array_key_exists($handler, $this->loadedMiddleware)) { |
68
|
|
|
return $this->loadedMiddleware[$handler]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->loadedMiddleware[$handler] = new PipedRouteMiddleware( |
72
|
|
|
$this->getPipeline($routeInfo[1]), |
73
|
|
|
false, |
74
|
|
|
$routeInfo[2] |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
return $this->loadedMiddleware[$handler]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
throw new LogicException('Something went wrong in routing.'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
private function getPipeline(array $middlewarePipeline): MiddlewarePipeline |
84
|
|
|
{ |
85
|
|
|
$pipeline = new MiddlewarePipeline($this->loop); |
86
|
|
|
$handler = array_pop($middlewarePipeline); |
87
|
|
|
|
88
|
|
|
foreach ($middlewarePipeline as $middleware) { |
89
|
|
|
$pipeline->pipe($this->middlewareFactory->create($middleware)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$requestHandlerFactory = $this->requestHandlerFactory; |
93
|
|
|
$handler = $requestHandlerFactory->create($handler); |
94
|
|
|
$callableHandler = static function (ServerRequestInterface $request) use ($handler): ResponseInterface { |
95
|
|
|
return $handler->handle($request); |
96
|
|
|
}; |
97
|
|
|
$pipeline->pipe(new CallableMiddleware($callableHandler)); |
98
|
|
|
|
99
|
|
|
return $pipeline; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths