1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace League\Route; |
4
|
|
|
|
5
|
|
|
use FastRoute\Dispatcher as FastRoute; |
6
|
|
|
use FastRoute\Dispatcher\GroupCountBased as GroupCountBasedDispatcher; |
7
|
|
|
use League\Route\Http\Exception\{MethodNotAllowedException, NotFoundException}; |
8
|
|
|
use League\Route\Middleware\{MiddlewareAwareInterface, MiddlewareAwareTrait}; |
9
|
|
|
use League\Route\Strategy\{StrategyAwareInterface, StrategyAwareTrait}; |
10
|
|
|
use OutOfBoundsException; |
11
|
|
|
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface}; |
12
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
13
|
|
|
|
14
|
|
|
class Dispatcher extends GroupCountBasedDispatcher implements |
15
|
|
|
MiddlewareAwareInterface, |
16
|
|
|
RequestHandlerInterface, |
17
|
|
|
StrategyAwareInterface |
18
|
|
|
{ |
19
|
|
|
use MiddlewareAwareTrait; |
20
|
|
|
use StrategyAwareTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Dispatch the current route. |
24
|
|
|
* |
25
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request |
26
|
|
|
* |
27
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
28
|
|
|
*/ |
29
|
26 |
|
public function dispatchRequest(ServerRequestInterface $request) : ResponseInterface |
30
|
|
|
{ |
31
|
26 |
|
$match = $this->dispatch($request->getMethod(), $request->getUri()->getPath()); |
32
|
|
|
|
33
|
26 |
|
switch ($match[0]) { |
34
|
26 |
|
case FastRoute::NOT_FOUND: |
35
|
12 |
|
$this->setNotFoundDecoratorMiddleware(); |
36
|
12 |
|
break; |
37
|
14 |
|
case FastRoute::METHOD_NOT_ALLOWED: |
38
|
4 |
|
$allowed = (array) $match[1]; |
39
|
4 |
|
$this->setMethodNotAllowedDecoratorMiddleware($allowed); |
40
|
4 |
|
break; |
41
|
10 |
|
case FastRoute::FOUND: |
42
|
10 |
|
$match[1]->setVars($match[2]); |
43
|
10 |
|
$this->setFoundMiddleware($match[1]); |
44
|
10 |
|
break; |
45
|
|
|
} |
46
|
|
|
|
47
|
26 |
|
return $this->handle($request); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
26 |
|
public function handle(ServerRequestInterface $request) : ResponseInterface |
54
|
|
|
{ |
55
|
26 |
|
$middleware = $this->shiftMiddleware(); |
56
|
|
|
|
57
|
26 |
|
if (is_null($middleware)) { |
58
|
|
|
throw new OutOfBoundsException('Reached end of middleware stack. Does your controller return a response?'); |
59
|
|
|
} |
60
|
|
|
|
61
|
26 |
|
return $middleware->process($request, $this); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Handle dispatching of a found route. |
66
|
|
|
* |
67
|
|
|
* @param \League\Route\Route $route |
68
|
|
|
* @param array $vars |
|
|
|
|
69
|
|
|
* |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
10 |
|
protected function setFoundMiddleware(Route $route) : void |
73
|
|
|
{ |
74
|
10 |
|
if (! is_null($route->getStrategy())) { |
75
|
10 |
|
$route->setStrategy($this->getStrategy()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// wrap entire dispatch process in exception handler |
79
|
10 |
|
$this->prependMiddleware($route->getStrategy()->getExceptionHandler()); |
80
|
|
|
|
81
|
|
|
// add group and route specific niddlewares |
82
|
10 |
|
if ($group = $route->getParentGroup()) { |
83
|
2 |
|
$this->middlewares($group->getMiddlewareStack()); |
84
|
|
|
} |
85
|
|
|
|
86
|
10 |
|
$this->middlewares($route->getMiddlewareStack()); |
87
|
|
|
|
88
|
|
|
// add actual route to end of stack |
89
|
10 |
|
$this->middleware($route); |
90
|
10 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Handle a not found route. |
94
|
|
|
* |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
12 |
|
protected function setNotFoundDecoratorMiddleware() : void |
98
|
|
|
{ |
99
|
12 |
|
$middleware = $this->getStrategy()->getNotFoundDecorator(new NotFoundException); |
100
|
12 |
|
$this->prependMiddleware($middleware); |
101
|
12 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Handles a not allowed route. |
105
|
|
|
* |
106
|
|
|
* @param array $allowed |
107
|
|
|
* |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
4 |
|
protected function setMethodNotAllowedDecoratorMiddleware(array $allowed) : void |
111
|
|
|
{ |
112
|
4 |
|
$middleware = $this->getStrategy()->getMethodNotAllowedDecorator( |
113
|
4 |
|
new MethodNotAllowedException($allowed) |
114
|
|
|
); |
115
|
|
|
|
116
|
4 |
|
$this->prependMiddleware($middleware); |
117
|
4 |
|
} |
118
|
|
|
} |
119
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.