|
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
|
24 |
|
public function dispatchRequest(ServerRequestInterface $request) : ResponseInterface |
|
30
|
|
|
{ |
|
31
|
24 |
|
$match = $this->dispatch($request->getMethod(), $request->getUri()->getPath()); |
|
32
|
|
|
|
|
33
|
24 |
|
switch ($match[0]) { |
|
34
|
24 |
|
case FastRoute::NOT_FOUND: |
|
35
|
6 |
|
$this->setNotFoundDecoratorMiddleware(); |
|
36
|
6 |
|
break; |
|
37
|
18 |
|
case FastRoute::METHOD_NOT_ALLOWED: |
|
38
|
6 |
|
$allowed = (array) $match[1]; |
|
39
|
6 |
|
$this->setMethodNotAllowedDecoratorMiddleware($allowed); |
|
40
|
6 |
|
break; |
|
41
|
12 |
|
case FastRoute::FOUND: |
|
42
|
12 |
|
$match[1]->setVars($match[2]); |
|
43
|
12 |
|
$this->setFoundMiddleware($match[1]); |
|
44
|
12 |
|
break; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
24 |
|
return $this->handle($request); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
24 |
|
public function handle(ServerRequestInterface $request) : ResponseInterface |
|
54
|
|
|
{ |
|
55
|
24 |
|
$middleware = $this->shiftMiddleware(); |
|
56
|
|
|
|
|
57
|
24 |
|
if (is_null($middleware)) { |
|
58
|
|
|
throw new OutOfBoundsException('Reached end of middleware stack. Does your controller return a response?'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
24 |
|
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
|
12 |
|
protected function setFoundMiddleware(Route $route) : void |
|
73
|
|
|
{ |
|
74
|
12 |
|
if (! is_null($route->getStrategy())) { |
|
75
|
12 |
|
$route->setStrategy($this->getStrategy()); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// wrap entire dispatch process in exception handler |
|
79
|
12 |
|
$this->prependMiddleware($route->getStrategy()->getExceptionHandler()); |
|
80
|
|
|
|
|
81
|
|
|
// add group and route specific niddlewares |
|
82
|
12 |
|
if ($group = $route->getParentGroup()) { |
|
83
|
|
|
$this->middlewares($group->getMiddlewareStack()); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
12 |
|
$this->middlewares($route->getMiddlewareStack()); |
|
87
|
|
|
|
|
88
|
|
|
// add actual route to end of stack |
|
89
|
12 |
|
$this->middleware($route); |
|
90
|
12 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Handle a not found route. |
|
94
|
|
|
* |
|
95
|
|
|
* @return void |
|
96
|
|
|
*/ |
|
97
|
6 |
|
protected function setNotFoundDecoratorMiddleware() : void |
|
98
|
|
|
{ |
|
99
|
6 |
|
$middleware = $this->getStrategy()->getNotFoundDecorator(new NotFoundException); |
|
100
|
6 |
|
$this->prependMiddleware($middleware); |
|
101
|
6 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Handles a not allowed route. |
|
105
|
|
|
* |
|
106
|
|
|
* @param array $allowed |
|
107
|
|
|
* |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
6 |
|
protected function setMethodNotAllowedDecoratorMiddleware(array $allowed) : void |
|
111
|
|
|
{ |
|
112
|
6 |
|
$middleware = $this->getStrategy()->getMethodNotAllowedDecorator( |
|
113
|
6 |
|
new MethodNotAllowedException($allowed) |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
6 |
|
$this->prependMiddleware($middleware); |
|
117
|
6 |
|
} |
|
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.