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
|
30 |
|
public function dispatchRequest(ServerRequestInterface $request) : ResponseInterface |
30
|
|
|
{ |
31
|
30 |
|
$match = $this->dispatch($request->getMethod(), $request->getUri()->getPath()); |
32
|
|
|
|
33
|
30 |
|
switch ($match[0]) { |
34
|
30 |
|
case FastRoute::NOT_FOUND: |
35
|
12 |
|
$this->setNotFoundDecoratorMiddleware(); |
36
|
12 |
|
break; |
37
|
18 |
|
case FastRoute::METHOD_NOT_ALLOWED: |
38
|
4 |
|
$allowed = (array) $match[1]; |
39
|
4 |
|
$this->setMethodNotAllowedDecoratorMiddleware($allowed); |
40
|
4 |
|
break; |
41
|
14 |
|
case FastRoute::FOUND: |
42
|
14 |
|
$match[1]->setVars($match[2]); |
43
|
14 |
|
$this->setFoundMiddleware($match[1]); |
44
|
14 |
|
break; |
45
|
|
|
} |
46
|
|
|
|
47
|
30 |
|
return $this->handle($request); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
30 |
|
public function handle(ServerRequestInterface $request) : ResponseInterface |
54
|
|
|
{ |
55
|
30 |
|
$middleware = $this->shiftMiddleware(); |
56
|
|
|
|
57
|
30 |
|
if (is_null($middleware)) { |
58
|
|
|
throw new OutOfBoundsException('Reached end of middleware stack. Does your controller return a response?'); |
59
|
|
|
} |
60
|
|
|
|
61
|
30 |
|
return $middleware->process($request, $this); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set up middleware for a found route |
66
|
|
|
* |
67
|
|
|
* @param \League\Route\Route $route |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
14 |
|
protected function setFoundMiddleware(Route $route) : void |
72
|
|
|
{ |
73
|
14 |
|
if (is_null($route->getStrategy())) { |
74
|
|
|
$route->setStrategy($this->getStrategy()); |
75
|
|
|
} |
76
|
|
|
|
77
|
14 |
|
$container = $route->getStrategy()->getContainer(); |
|
|
|
|
78
|
|
|
|
79
|
14 |
|
foreach ($this->getMiddlewareStack() as $key => $middleware) { |
80
|
2 |
|
$this->middleware[$key] = $this->resolveMiddleware($middleware); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// wrap entire dispatch process in exception handler |
84
|
14 |
|
$this->prependMiddleware($route->getStrategy()->getExceptionHandler()); |
85
|
|
|
|
86
|
|
|
// add group and route specific middleware |
87
|
14 |
|
if ($group = $route->getParentGroup()) { |
88
|
4 |
|
foreach ($group->getMiddlewareStack() as $middleware) { |
89
|
2 |
|
$this->middleware($this->resolveMiddleware($middleware)); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
14 |
|
foreach ($route->getMiddlewareStack() as $middleware) { |
94
|
2 |
|
$this->middleware($this->resolveMiddleware($middleware)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// add actual route to end of stack |
98
|
14 |
|
$this->middleware($route); |
99
|
14 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Set up middleware for a not found route |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
12 |
|
protected function setNotFoundDecoratorMiddleware() : void |
107
|
|
|
{ |
108
|
12 |
|
$middleware = $this->getStrategy()->getNotFoundDecorator(new NotFoundException); |
109
|
12 |
|
$this->prependMiddleware($middleware); |
110
|
12 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Set up middleware for a not allowed route |
114
|
|
|
* |
115
|
|
|
* @param array $allowed |
116
|
|
|
* |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
4 |
|
protected function setMethodNotAllowedDecoratorMiddleware(array $allowed) : void |
120
|
|
|
{ |
121
|
4 |
|
$middleware = $this->getStrategy()->getMethodNotAllowedDecorator( |
122
|
4 |
|
new MethodNotAllowedException($allowed) |
123
|
|
|
); |
124
|
|
|
|
125
|
4 |
|
$this->prependMiddleware($middleware); |
126
|
4 |
|
} |
127
|
|
|
} |
128
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.