|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Lead\Router\Middleware; |
|
5
|
|
|
|
|
6
|
|
|
use Lead\Router\Exception\RouteNotFoundException; |
|
7
|
|
|
use Lead\Router\RouterInterface; |
|
8
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
10
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* RouterMiddleware |
|
15
|
|
|
*/ |
|
16
|
|
|
class RouterMiddleware implements MiddlewareInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Router |
|
20
|
|
|
* |
|
21
|
|
|
* @var \Lead\Router\Router |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $router; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Not found callback |
|
27
|
|
|
* |
|
28
|
|
|
* @var callable |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $notFoundCallback = null; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Ignore the exception |
|
34
|
|
|
* |
|
35
|
|
|
* @var bool |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $ignoreNotFoundException = false; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The request attribute name for the route |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $routeAttribute = 'route'; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Constructor |
|
48
|
|
|
* |
|
49
|
|
|
* @param \Lead\Router\RouterInterface $router Router |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct( |
|
52
|
|
|
RouterInterface $router |
|
53
|
|
|
) { |
|
54
|
|
|
$this->router = $router; |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Sets the request attribute name for the route object |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $name Name |
|
61
|
|
|
* @return $this |
|
62
|
|
|
*/ |
|
63
|
|
|
public function setRouteAttribute(string $name): self |
|
64
|
|
|
{ |
|
65
|
|
|
$this->routeAttribute = $name; |
|
66
|
|
|
|
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Sets the flag to ignore the route not found exception |
|
72
|
|
|
* |
|
73
|
|
|
* @param bool $ignore Ignore |
|
74
|
|
|
* @return $this |
|
75
|
|
|
*/ |
|
76
|
|
|
public function setIgnoreException(bool $ignore): self |
|
77
|
|
|
{ |
|
78
|
|
|
$this->ignoreNotFoundException = $ignore; |
|
79
|
|
|
|
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Process an incoming server request and return a response, optionally |
|
85
|
|
|
* delegating response creation to a handler. |
|
86
|
|
|
* |
|
87
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request |
|
88
|
|
|
* @param \Psr\Http\Server\RequestHandlerInterface $requestHandler Request Handler |
|
89
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
90
|
|
|
*/ |
|
91
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $requestHandler): ResponseInterface |
|
92
|
|
|
{ |
|
93
|
|
|
try { |
|
94
|
|
|
$route = $this->router->route($request); |
|
95
|
|
|
$request = $request->withAttribute($this->routeAttribute, $route); |
|
96
|
|
|
} catch (RouteNotFoundException $exception) { |
|
97
|
|
|
if (is_callable($this->notFoundCallback)) { |
|
98
|
|
|
$callable = $this->notFoundCallback; |
|
99
|
|
|
$result = $callable($request, $this->router); |
|
100
|
|
|
if ($result instanceof ResponseInterface) { |
|
101
|
|
|
return $result; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
if (!$this->ignoreNotFoundException) { |
|
106
|
|
|
throw $exception; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $requestHandler->handle($request); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.