|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Flight Routing. |
|
7
|
|
|
* |
|
8
|
|
|
* PHP version 7.1 and above required |
|
9
|
|
|
* |
|
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
|
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
|
13
|
|
|
* |
|
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
15
|
|
|
* file that was distributed with this source code. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace Flight\Routing\Traits; |
|
19
|
|
|
|
|
20
|
|
|
use Closure; |
|
21
|
|
|
use Flight\Routing\Interfaces\RouteInterface; |
|
22
|
|
|
use Flight\Routing\Router; |
|
23
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
24
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
25
|
|
|
|
|
26
|
|
|
trait ValidationTrait |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @param callable|object|string|string[] $controller |
|
30
|
|
|
* |
|
31
|
|
|
* @return mixed |
|
32
|
|
|
*/ |
|
33
|
35 |
|
protected function resolveNamespace($controller) |
|
34
|
|
|
{ |
|
35
|
35 |
|
$namespace = $this->namespace; |
|
36
|
|
|
|
|
37
|
35 |
|
if (null !== $namespace && (\is_string($controller) || !$controller instanceof Closure)) { |
|
38
|
|
|
if ( |
|
39
|
|
|
( |
|
40
|
4 |
|
\is_string($controller) && |
|
41
|
4 |
|
!\class_exists($controller) |
|
42
|
|
|
) && |
|
43
|
4 |
|
!str_starts_with($controller, $namespace) |
|
44
|
|
|
) { |
|
45
|
3 |
|
$controller = \is_callable($controller) ? $controller : $this->namespace . \ltrim($controller, '\\/'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
4 |
|
if (\is_array($controller) && (!\is_object($controller[0]) && !\class_exists($controller[0]))) { |
|
49
|
1 |
|
$controller[0] = $this->namespace . \ltrim($controller[0], '\\/'); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
35 |
|
return $controller; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param ServerRequestInterface $request |
|
58
|
|
|
* @param RouteInterface $route |
|
59
|
|
|
* |
|
60
|
|
|
* @return mixed |
|
61
|
|
|
*/ |
|
62
|
35 |
|
protected function resolveController(ServerRequestInterface $request, RouteInterface &$route) |
|
63
|
|
|
{ |
|
64
|
35 |
|
$controller = $route->getController(); |
|
65
|
|
|
|
|
66
|
|
|
// Disable or enable HTTP request method prefix for action. |
|
67
|
35 |
|
if (str_ends_with($route->getName(), '__restful')) { |
|
68
|
|
|
switch (true) { |
|
69
|
3 |
|
case \is_array($controller): |
|
70
|
2 |
|
$controller[1] = $this->getResourceMethod($request, $controller[1]); |
|
71
|
|
|
|
|
72
|
2 |
|
break; |
|
73
|
|
|
|
|
74
|
1 |
|
case \is_string($controller) && \class_exists($controller): |
|
75
|
|
|
$controller = [ |
|
76
|
1 |
|
$controller, |
|
77
|
1 |
|
$this->getResourceMethod($request, \substr($route->getName(), -0, -9)), |
|
78
|
|
|
]; |
|
79
|
|
|
|
|
80
|
1 |
|
break; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
35 |
|
$handler = $this->resolveNamespace($controller); |
|
85
|
|
|
|
|
86
|
|
|
// For a class that implements RequestHandlerInterface, we will call handle() |
|
87
|
|
|
// if no method has been specified explicitly |
|
88
|
35 |
|
if (\is_string($handler) && \is_a($handler, RequestHandlerInterface::class, true)) { |
|
89
|
11 |
|
$handler = [$handler, 'handle']; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// If controller is instance of RequestHandlerInterface |
|
93
|
35 |
|
if ($handler instanceof RequestHandlerInterface) { |
|
94
|
11 |
|
return $handler->handle($request); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
24 |
|
return $handler; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Check if given request method matches given route method. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string[] $routeMethod |
|
104
|
|
|
* @param string $requestMethod |
|
105
|
|
|
* |
|
106
|
|
|
* @return bool |
|
107
|
|
|
*/ |
|
108
|
46 |
|
private function compareMethod(array $routeMethod, string $requestMethod): bool |
|
109
|
|
|
{ |
|
110
|
46 |
|
return \in_array($requestMethod, $routeMethod, true); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Check if given request domain matches given route domain. |
|
115
|
|
|
* |
|
116
|
|
|
* @param null|string $routeDomain |
|
117
|
|
|
* @param string $requestDomain |
|
118
|
|
|
* @param array<int|string,string> $parameters |
|
119
|
|
|
* |
|
120
|
|
|
* @return bool |
|
121
|
|
|
*/ |
|
122
|
44 |
|
private function compareDomain(?string $routeDomain, string $requestDomain, array &$parameters): bool |
|
123
|
|
|
{ |
|
124
|
44 |
|
return ($routeDomain === null || empty($routeDomain)) || |
|
125
|
44 |
|
(bool) \preg_match($routeDomain, $requestDomain, $parameters); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Check if given request uri matches given uri method. |
|
130
|
|
|
* |
|
131
|
|
|
* @param string $routeUri |
|
132
|
|
|
* @param string $requestUri |
|
133
|
|
|
* @param array<int|string,string> $parameters |
|
134
|
|
|
* |
|
135
|
|
|
* @return bool |
|
136
|
|
|
*/ |
|
137
|
48 |
|
private function compareUri(string $routeUri, string $requestUri, array &$parameters): bool |
|
138
|
|
|
{ |
|
139
|
48 |
|
return (bool) \preg_match($routeUri, $requestUri, $parameters); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Check if given request uri scheme matches given route scheme. |
|
144
|
|
|
* |
|
145
|
|
|
* @param string[] $routeScheme |
|
146
|
|
|
* @param string $requestScheme |
|
147
|
|
|
* |
|
148
|
|
|
* @return bool |
|
149
|
|
|
*/ |
|
150
|
43 |
|
private function compareScheme(array $routeScheme, string $requestScheme): bool |
|
151
|
|
|
{ |
|
152
|
43 |
|
return empty($routeScheme) || \in_array($requestScheme, $routeScheme, true); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Get merged default parameters. |
|
157
|
|
|
* |
|
158
|
|
|
* @param array<int|string,mixed> $params |
|
159
|
|
|
* @param array<string,string> $defaults |
|
160
|
|
|
* |
|
161
|
|
|
* @return array<string,string> Merged default parameters |
|
162
|
|
|
*/ |
|
163
|
42 |
|
private function mergeDefaults(array $params, array $defaults): array |
|
164
|
|
|
{ |
|
165
|
42 |
|
foreach ($params as $key => $value) { |
|
166
|
42 |
|
if (!\is_int($key) && (!isset($defaults[$key]) || null !== $value)) { |
|
167
|
6 |
|
$defaults[$key] = $value; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
42 |
|
return $defaults; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Merge Router attributes in route default and patterns. |
|
176
|
|
|
* |
|
177
|
|
|
* @param RouteInterface $route |
|
178
|
|
|
* |
|
179
|
|
|
* @return RouteInterface |
|
180
|
|
|
*/ |
|
181
|
48 |
|
private function mergeAttributes(RouteInterface $route): RouteInterface |
|
182
|
|
|
{ |
|
183
|
48 |
|
foreach ($this->attributes as $type => $attributes) { |
|
184
|
1 |
|
if (Router::TYPE_DEFAULT === $type) { |
|
185
|
1 |
|
$route->setDefaults($attributes); |
|
186
|
|
|
|
|
187
|
1 |
|
continue; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
1 |
|
$route->setPatterns($attributes); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
48 |
|
return $route; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @param ServerRequestInterface $request |
|
198
|
|
|
* @param string $name |
|
199
|
|
|
*/ |
|
200
|
3 |
|
private function getResourceMethod(ServerRequestInterface $request, string $name): string |
|
201
|
|
|
{ |
|
202
|
3 |
|
return \strtolower($request->getMethod()) . \ucfirst($name); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|