|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace League\Route; |
|
6
|
|
|
|
|
7
|
|
|
use FastRoute\Dispatcher as FastRoute; |
|
8
|
|
|
use FastRoute\Dispatcher\GroupCountBased as GroupCountBasedDispatcher; |
|
9
|
|
|
use League\Route\Http\Exception\{MethodNotAllowedException, NotFoundException}; |
|
10
|
|
|
use League\Route\Middleware\{MiddlewareAwareInterface, MiddlewareAwareTrait}; |
|
11
|
|
|
use League\Route\Strategy\{StrategyAwareInterface, StrategyAwareTrait, StrategyInterface}; |
|
12
|
|
|
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface}; |
|
13
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
14
|
|
|
use RuntimeException; |
|
15
|
|
|
|
|
16
|
|
|
class Dispatcher extends GroupCountBasedDispatcher implements |
|
17
|
|
|
MiddlewareAwareInterface, |
|
18
|
|
|
RequestHandlerInterface, |
|
19
|
|
|
RouteConditionHandlerInterface, |
|
20
|
|
|
StrategyAwareInterface |
|
21
|
|
|
{ |
|
22
|
|
|
use MiddlewareAwareTrait; |
|
23
|
|
|
use RouteConditionHandlerTrait; |
|
24
|
|
|
use StrategyAwareTrait; |
|
25
|
|
|
|
|
26
|
69 |
|
public function dispatchRequest(ServerRequestInterface $request): ResponseInterface |
|
27
|
|
|
{ |
|
28
|
69 |
|
$method = $request->getMethod(); |
|
29
|
69 |
|
$uri = $request->getUri()->getPath(); |
|
30
|
69 |
|
$match = $this->dispatch($method, $uri); |
|
31
|
|
|
|
|
32
|
69 |
|
switch ($match[0]) { |
|
33
|
69 |
|
case FastRoute::NOT_FOUND: |
|
34
|
18 |
|
$this->setNotFoundDecoratorMiddleware(); |
|
35
|
18 |
|
break; |
|
36
|
51 |
|
case FastRoute::METHOD_NOT_ALLOWED: |
|
37
|
6 |
|
$allowed = (array) $match[1]; |
|
38
|
6 |
|
$this->setMethodNotAllowedDecoratorMiddleware($allowed); |
|
39
|
6 |
|
break; |
|
40
|
45 |
|
case FastRoute::FOUND: |
|
41
|
45 |
|
$route = $this->ensureHandlerIsRoute($match[1], $method, $uri)->setVars($match[2]); |
|
42
|
|
|
|
|
43
|
45 |
|
if ($this->isExtraConditionMatch($route, $request)) { |
|
44
|
45 |
|
$this->setFoundMiddleware($route); |
|
45
|
45 |
|
$request = $this->requestWithRouteAttributes($request, $route); |
|
46
|
45 |
|
break; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$this->setNotFoundDecoratorMiddleware(); |
|
50
|
|
|
break; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
69 |
|
return $this->handle($request); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
69 |
|
public function handle(ServerRequestInterface $request): ResponseInterface |
|
57
|
|
|
{ |
|
58
|
69 |
|
$middleware = $this->shiftMiddleware(); |
|
59
|
69 |
|
return $middleware->process($request, $this); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
45 |
|
protected function ensureHandlerIsRoute($matchingHandler, $httpMethod, $uri): Route |
|
63
|
|
|
{ |
|
64
|
45 |
|
if ($matchingHandler instanceof Route) { |
|
65
|
45 |
|
return $matchingHandler; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return new Route($httpMethod, $uri, $matchingHandler); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
45 |
|
protected function requestWithRouteAttributes(ServerRequestInterface $request, Route $route): ServerRequestInterface |
|
72
|
|
|
{ |
|
73
|
45 |
|
$routerParams = $route->getVars(); |
|
74
|
|
|
|
|
75
|
45 |
|
foreach ($routerParams as $key => $value) { |
|
76
|
18 |
|
$request = $request->withAttribute($key, $value); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
45 |
|
return $request; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
45 |
|
protected function setFoundMiddleware(Route $route): void |
|
83
|
|
|
{ |
|
84
|
45 |
|
if ($route->getStrategy() === null) { |
|
85
|
3 |
|
$strategy = $this->getStrategy(); |
|
86
|
|
|
|
|
87
|
3 |
|
if (!($strategy instanceof StrategyInterface)) { |
|
88
|
|
|
throw new RuntimeException('Cannot determine strategy to use for dispatch of found route'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
3 |
|
$route->setStrategy($strategy); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
45 |
|
$strategy = $route->getStrategy(); |
|
95
|
45 |
|
$container = $strategy instanceof ContainerAwareInterface ? $strategy->getContainer() : null; |
|
96
|
|
|
|
|
97
|
45 |
|
foreach ($this->getMiddlewareStack() as $key => $middleware) { |
|
98
|
3 |
|
$this->middleware[$key] = $this->resolveMiddleware($middleware, $container); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
// wrap entire dispatch process in exception handler |
|
102
|
45 |
|
$this->prependMiddleware($strategy->getThrowableHandler()); |
|
103
|
|
|
|
|
104
|
|
|
// add group and route specific middleware |
|
105
|
45 |
|
if ($group = $route->getParentGroup()) { |
|
106
|
6 |
|
foreach ($group->getMiddlewareStack() as $middleware) { |
|
107
|
3 |
|
$this->middleware($this->resolveMiddleware($middleware, $container)); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
45 |
|
foreach ($route->getMiddlewareStack() as $middleware) { |
|
112
|
3 |
|
$this->middleware($this->resolveMiddleware($middleware, $container)); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// add actual route to end of stack |
|
116
|
45 |
|
$this->middleware($route); |
|
117
|
45 |
|
} |
|
118
|
|
|
|
|
119
|
6 |
|
protected function setMethodNotAllowedDecoratorMiddleware(array $allowed): void |
|
120
|
|
|
{ |
|
121
|
6 |
|
$strategy = $this->getStrategy(); |
|
122
|
|
|
|
|
123
|
6 |
|
if (!($strategy instanceof StrategyInterface)) { |
|
124
|
|
|
throw new RuntimeException('Cannot determine strategy to use for dispatch of method not allowed route'); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
6 |
|
$middleware = $strategy->getMethodNotAllowedDecorator(new MethodNotAllowedException($allowed)); |
|
128
|
6 |
|
$this->prependMiddleware($middleware); |
|
129
|
6 |
|
} |
|
130
|
|
|
|
|
131
|
18 |
|
protected function setNotFoundDecoratorMiddleware(): void |
|
132
|
|
|
{ |
|
133
|
18 |
|
$strategy = $this->getStrategy(); |
|
134
|
|
|
|
|
135
|
18 |
|
if (!($strategy instanceof StrategyInterface)) { |
|
136
|
|
|
throw new RuntimeException('Cannot determine strategy to use for dispatch of not found route'); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
18 |
|
$middleware = $strategy->getNotFoundDecorator(new NotFoundException()); |
|
140
|
18 |
|
$this->prependMiddleware($middleware); |
|
141
|
18 |
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|