1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Burzum\FastRouteMiddleware; |
5
|
|
|
|
6
|
|
|
use Burzum\FastRouteMiddleware\Handler\FoundHandlerInterface; |
7
|
|
|
use Burzum\FastRouteMiddleware\Handler\NotAllowedHandlerInterface; |
8
|
|
|
use Burzum\FastRouteMiddleware\Handler\NotFoundHandlerInterface; |
9
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
11
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
13
|
|
|
use FastRoute\Dispatcher as DispatcherInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* FastRoute Dispatcher Middleware |
17
|
|
|
* |
18
|
|
|
* @link https://github.com/nikic/FastRoute |
19
|
|
|
*/ |
20
|
|
|
class FastRouteMiddleware implements MiddlewareInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Fast Route Dispatcher |
24
|
|
|
* |
25
|
|
|
* @var \FastRoute\Dispatcher |
26
|
|
|
*/ |
27
|
|
|
protected $dispatcher; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Route not allowed Handler |
31
|
|
|
* |
32
|
|
|
* @var null|\Burzum\FastRouteMiddleware\Handler\NotAllowedHandlerInterface |
33
|
|
|
*/ |
34
|
|
|
protected $notAllowedHandler; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Route not found Handler |
38
|
|
|
* |
39
|
|
|
* @var null|\Burzum\FastRouteMiddleware\Handler\NotFoundHandlerInterface |
40
|
|
|
*/ |
41
|
|
|
protected $notFoundHandler; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Route Found Handler |
45
|
|
|
* |
46
|
|
|
* @var null|\Burzum\FastRouteMiddleware\Handler\FoundHandlerInterface |
47
|
|
|
*/ |
48
|
|
|
protected $foundHandler; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Route Attribute for the Request |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $routeAttribute = 'route'; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Constructor |
59
|
|
|
* |
60
|
|
|
* @param \FastRoute\Dispatcher $dispatcher Fastroute Dispatcher |
61
|
|
|
*/ |
62
|
|
|
public function __construct( |
63
|
|
|
DispatcherInterface $dispatcher, |
64
|
|
|
?FoundHandlerInterface $foundHandler = null, |
65
|
|
|
?NotFoundHandlerInterface $notFoundHandler = null, |
66
|
|
|
?NotAllowedHandlerInterface $notAllowedHandler = null |
|
|
|
|
67
|
|
|
) { |
68
|
|
|
$this->dispatcher = $dispatcher; |
69
|
|
|
$this->foundHandler = $foundHandler; |
70
|
|
|
$this->notFoundHandler = $notFoundHandler; |
71
|
|
|
$this->notAllowedHandler = $notFoundHandler; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Sets the route attribute name |
76
|
|
|
* |
77
|
|
|
* @param string $attributeName Attribute Name |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
|
|
public function setRouteAttribute(string $attributeName): self |
81
|
|
|
{ |
82
|
|
|
$this->routeAttribute = $attributeName; |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Process an incoming server request and return a response, optionally |
89
|
|
|
* delegating response creation to a handler. |
90
|
|
|
* |
91
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request |
92
|
|
|
* @param \Psr\Http\Server\RequestHandlerInterface $requestHandler Request Handler |
93
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
94
|
|
|
*/ |
95
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $requestHandler): ResponseInterface |
96
|
|
|
{ |
97
|
|
|
$routeInfo = $this->dispatch(); |
98
|
|
|
$result = null; |
99
|
|
|
|
100
|
|
|
switch ($routeInfo[0]) { |
101
|
|
|
case DispatcherInterface::NOT_FOUND: |
102
|
|
|
if ($this->notFoundHandler !== null) { |
103
|
|
|
$result = $this->notFoundHandler->handle($request); |
104
|
|
|
} |
105
|
|
|
break; |
106
|
|
|
case DispatcherInterface::METHOD_NOT_ALLOWED: |
107
|
|
|
if ($this->notAllowedHandler !== null) { |
108
|
|
|
$result = $this->notAllowedHandler->handle($request, $routeInfo[1]); |
109
|
|
|
} |
110
|
|
|
break; |
111
|
|
|
case DispatcherInterface::FOUND: |
112
|
|
|
$request = $request->withAttribute( |
113
|
|
|
$this->routeAttribute, |
114
|
|
|
new RouteInfo($routeInfo[1], $routeInfo[2]) |
115
|
|
|
); |
116
|
|
|
if ($this->foundHandler !== null) { |
117
|
|
|
$result = $this->foundHandler->handle($request, $routeInfo[1], $routeInfo[2]); |
118
|
|
|
} |
119
|
|
|
break; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ($result instanceof ResponseInterface) { |
123
|
|
|
return $result; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $requestHandler->handle($request); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Dispatches the Request URI |
131
|
|
|
* |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
protected function dispatch(): array |
135
|
|
|
{ |
136
|
|
|
$httpMethod = $_SERVER['REQUEST_METHOD']; |
137
|
|
|
$uri = $_SERVER['REQUEST_URI']; |
138
|
|
|
|
139
|
|
|
// Strip query string (?foo=bar) and decode URI |
140
|
|
|
if (false !== $pos = strpos($uri, '?')) { |
141
|
|
|
$uri = substr($uri, 0, $pos); |
142
|
|
|
} |
143
|
|
|
$uri = rawurldecode($uri); |
144
|
|
|
|
145
|
|
|
return $this->dispatcher->dispatch($httpMethod, $uri); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.