|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gacela\Router; |
|
6
|
|
|
|
|
7
|
|
|
use ReflectionClass; |
|
8
|
|
|
use ReflectionNamedType; |
|
9
|
|
|
|
|
10
|
|
|
use function count; |
|
11
|
|
|
use function is_object; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @method static get(string $path, object|string $controller, string $action = '__invoke') |
|
15
|
|
|
* @method static head(string $path, object|string $controller, string $action = '__invoke') |
|
16
|
|
|
* @method static connect(string $path, object|string $controller, string $action = '__invoke') |
|
17
|
|
|
* @method static post(string $path, object|string $controller, string $action = '__invoke') |
|
18
|
|
|
* @method static delete(string $path, object|string $controller, string $action = '__invoke') |
|
19
|
|
|
* @method static options(string $path, object|string $controller, string $action = '__invoke') |
|
20
|
|
|
* @method static patch(string $path, object|string $controller, string $action = '__invoke') |
|
21
|
|
|
* @method static put(string $path, object|string $controller, string $action = '__invoke') |
|
22
|
|
|
* @method static trace(string $path, object|string $controller, string $action = '__invoke') |
|
23
|
|
|
*/ |
|
24
|
|
|
final class Route |
|
25
|
|
|
{ |
|
26
|
|
|
private static ?Request $request = null; |
|
27
|
|
|
|
|
28
|
|
|
private static bool $isResponded = false; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param object|class-string $controller |
|
|
|
|
|
|
32
|
|
|
*/ |
|
33
|
42 |
|
private function __construct( |
|
34
|
|
|
private string $method, |
|
35
|
|
|
private string $path, |
|
36
|
|
|
private object|string $controller, |
|
37
|
|
|
private string $action = '__invoke', |
|
38
|
|
|
) { |
|
39
|
42 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @psalm-suppress MixedArgument |
|
43
|
|
|
*/ |
|
44
|
42 |
|
public static function __callStatic(string $name, array $arguments): void |
|
45
|
|
|
{ |
|
46
|
42 |
|
match ($name) { |
|
47
|
42 |
|
'head' => self::route(Request::METHOD_HEAD, ...$arguments), |
|
|
|
|
|
|
48
|
42 |
|
'connect' => self::route(Request::METHOD_CONNECT, ...$arguments), |
|
|
|
|
|
|
49
|
42 |
|
'post' => self::route(Request::METHOD_POST, ...$arguments), |
|
|
|
|
|
|
50
|
42 |
|
'delete' => self::route(Request::METHOD_DELETE, ...$arguments), |
|
|
|
|
|
|
51
|
42 |
|
'options' => self::route(Request::METHOD_OPTIONS, ...$arguments), |
|
|
|
|
|
|
52
|
42 |
|
'patch' => self::route(Request::METHOD_PATCH, ...$arguments), |
|
|
|
|
|
|
53
|
42 |
|
'put' => self::route(Request::METHOD_PUT, ...$arguments), |
|
|
|
|
|
|
54
|
42 |
|
'trace' => self::route(Request::METHOD_TRACE, ...$arguments), |
|
|
|
|
|
|
55
|
42 |
|
default => self::route(Request::METHOD_GET, ...$arguments), |
|
|
|
|
|
|
56
|
42 |
|
}; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @internal for testing |
|
61
|
|
|
*/ |
|
62
|
42 |
|
public static function reset(): void |
|
63
|
|
|
{ |
|
64
|
42 |
|
self::$isResponded = false; |
|
65
|
42 |
|
self::$request = null; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
42 |
|
public function requestMatches(): bool |
|
69
|
|
|
{ |
|
70
|
42 |
|
if (self::$isResponded) { |
|
71
|
1 |
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
42 |
|
if (!$this->methodMatches()) { |
|
75
|
1 |
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
41 |
|
if (!$this->pathMatches()) { |
|
79
|
1 |
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
40 |
|
return true; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @psalm-suppress MixedMethodCall |
|
87
|
|
|
*/ |
|
88
|
40 |
|
public function run(): string |
|
89
|
|
|
{ |
|
90
|
40 |
|
self::$isResponded = true; |
|
91
|
|
|
|
|
92
|
40 |
|
if (is_object($this->controller)) { |
|
93
|
|
|
return (string)$this->controller |
|
94
|
|
|
->{$this->action}(...$this->getParams()); |
|
95
|
|
|
} |
|
96
|
40 |
|
return (string)(new $this->controller()) |
|
97
|
40 |
|
->{$this->action}(...$this->getParams()); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param object|class-string $controller |
|
|
|
|
|
|
102
|
|
|
*/ |
|
103
|
42 |
|
private static function route( |
|
104
|
|
|
string $method, |
|
105
|
|
|
string $path, |
|
106
|
|
|
object|string $controller, |
|
107
|
|
|
string $action = '__invoke', |
|
108
|
|
|
): void { |
|
109
|
42 |
|
$path = ($path === '/') ? '' : $path; |
|
110
|
|
|
|
|
111
|
42 |
|
$route = new self($method, $path, $controller, $action); |
|
112
|
|
|
|
|
113
|
42 |
|
if ($route->requestMatches()) { |
|
114
|
40 |
|
echo $route->run(); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
42 |
|
private function methodMatches(): bool |
|
119
|
|
|
{ |
|
120
|
42 |
|
return $this->request()->isMethod($this->method); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
41 |
|
private function pathMatches(): bool |
|
124
|
|
|
{ |
|
125
|
41 |
|
return (bool)preg_match($this->getPathPattern(), $this->request()->path()) |
|
126
|
41 |
|
|| (bool)preg_match($this->getPathPatternWithoutOptionals(), $this->request()->path()); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
41 |
|
private function getPathPattern(): string |
|
130
|
|
|
{ |
|
131
|
41 |
|
$pattern = preg_replace('#({.*})#U', '(.*)', $this->path); |
|
132
|
|
|
|
|
133
|
41 |
|
return '#^/' . $pattern . '$#'; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
3 |
|
private function getPathPatternWithoutOptionals(): string |
|
137
|
|
|
{ |
|
138
|
3 |
|
$pattern = preg_replace('#/({.*\?})#U', '(/(.*))?', $this->path); |
|
139
|
|
|
|
|
140
|
3 |
|
return '#^/' . $pattern . '$#'; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
40 |
|
private function getParams(): array |
|
144
|
|
|
{ |
|
145
|
40 |
|
$params = []; |
|
146
|
40 |
|
$pathParamKeys = []; |
|
147
|
40 |
|
$pathParamValues = []; |
|
148
|
|
|
|
|
149
|
40 |
|
preg_match($this->getPathPattern(), '/' . $this->path, $pathParamKeys); |
|
150
|
40 |
|
preg_match($this->getPathPattern(), $this->request()->path(), $pathParamValues); |
|
151
|
|
|
|
|
152
|
40 |
|
unset($pathParamValues[0], $pathParamKeys[0]); |
|
153
|
40 |
|
$pathParamKeys = array_map(static fn ($key) => trim($key, '{}'), $pathParamKeys); |
|
154
|
|
|
|
|
155
|
40 |
|
while (count($pathParamValues) !== count($pathParamKeys)) { |
|
156
|
2 |
|
array_shift($pathParamKeys); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
40 |
|
$pathParams = array_combine($pathParamKeys, $pathParamValues); |
|
160
|
40 |
|
$actionParams = (new ReflectionClass($this->controller)) |
|
161
|
40 |
|
->getMethod($this->action) |
|
162
|
40 |
|
->getParameters(); |
|
163
|
|
|
|
|
164
|
40 |
|
foreach ($actionParams as $actionParam) { |
|
165
|
36 |
|
$paramName = $actionParam->getName(); |
|
166
|
|
|
/** @var string|null $paramType */ |
|
167
|
36 |
|
$paramType = null; |
|
168
|
|
|
|
|
169
|
36 |
|
if ($actionParam->getType() && is_a($actionParam->getType(), ReflectionNamedType::class)) { |
|
170
|
36 |
|
$paramType = $actionParam->getType()->getName(); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
36 |
|
$value = match ($paramType) { |
|
174
|
36 |
|
'string' => $pathParams[$paramName] ?? '', |
|
175
|
36 |
|
'int' => (int)($pathParams[$paramName] ?? 0), |
|
176
|
36 |
|
'float' => (float)($pathParams[$paramName] ?? 0.0), |
|
177
|
36 |
|
'bool' => (bool)json_decode($pathParams[$paramName] ?? '0'), |
|
178
|
36 |
|
null => null, |
|
179
|
36 |
|
}; |
|
180
|
|
|
|
|
181
|
36 |
|
$params[$paramName] = $value; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
40 |
|
return $params; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
42 |
|
private function request(): Request |
|
188
|
|
|
{ |
|
189
|
42 |
|
return self::$request ??= Request::fromGlobals(); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|