-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Route.php
317 lines (266 loc) · 8.3 KB
/
Route.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
declare(strict_types=1);
namespace Yiisoft\Router;
use InvalidArgumentException;
use RuntimeException;
use Stringable;
use Yiisoft\Http\Method;
use Yiisoft\Router\Internal\MiddlewareFilter;
use function in_array;
/**
* Route defines a mapping from URL to callback / name and vice versa.
*/
final class Route implements Stringable
{
private ?string $name = null;
/**
* @var string[]
*/
private array $hosts = [];
private bool $override = false;
private bool $actionAdded = false;
/**
* @var array[]|callable[]|string[]
* @psalm-var list<array|callable|string>
*/
private array $middlewares = [];
private array $disabledMiddlewares = [];
/**
* @psalm-var list<array|callable|string>|null
*/
private ?array $enabledMiddlewaresCache = null;
/**
* @var array<string,string>
*/
private array $defaults = [];
/**
* @param string[] $methods
*/
private function __construct(
private array $methods,
private string $pattern,
) {
}
public static function get(string $pattern): self
{
return self::methods([Method::GET], $pattern);
}
public static function post(string $pattern): self
{
return self::methods([Method::POST], $pattern);
}
public static function put(string $pattern): self
{
return self::methods([Method::PUT], $pattern);
}
public static function delete(string $pattern): self
{
return self::methods([Method::DELETE], $pattern);
}
public static function patch(string $pattern): self
{
return self::methods([Method::PATCH], $pattern);
}
public static function head(string $pattern): self
{
return self::methods([Method::HEAD], $pattern);
}
public static function options(string $pattern): self
{
return self::methods([Method::OPTIONS], $pattern);
}
/**
* @param string[] $methods
*/
public static function methods(array $methods, string $pattern): self
{
return new self($methods, $pattern);
}
public function name(string $name): self
{
$route = clone $this;
$route->name = $name;
return $route;
}
public function pattern(string $pattern): self
{
$new = clone $this;
$new->pattern = $pattern;
return $new;
}
public function host(string $host): self
{
return $this->hosts($host);
}
public function hosts(string ...$hosts): self
{
$route = clone $this;
$route->hosts = [];
foreach ($hosts as $host) {
$host = rtrim($host, '/');
if ($host !== '' && !in_array($host, $route->hosts, true)) {
$route->hosts[] = $host;
}
}
return $route;
}
/**
* Marks route as override. When added it will replace existing route with the same name.
*/
public function override(): self
{
$route = clone $this;
$route->override = true;
return $route;
}
/**
* Parameter default values indexed by parameter names.
*
* @psalm-param array<string,null|Stringable|scalar> $defaults
*/
public function defaults(array $defaults): self
{
$route = clone $this;
$route->defaults = array_map('\strval', $defaults);
return $route;
}
/**
* Appends a handler middleware definition that should be invoked for a matched route.
* First added handler will be executed first.
*/
public function middleware(array|callable|string ...$definition): self
{
if ($this->actionAdded) {
throw new RuntimeException('middleware() can not be used after action().');
}
$route = clone $this;
array_push(
$route->middlewares,
...array_values($definition)
);
$route->enabledMiddlewaresCache = null;
return $route;
}
/**
* Prepends a handler middleware definition that should be invoked for a matched route.
* Last added handler will be executed first.
*/
public function prependMiddleware(array|callable|string ...$definition): self
{
if (!$this->actionAdded) {
throw new RuntimeException('prependMiddleware() can not be used before action().');
}
$route = clone $this;
array_unshift(
$route->middlewares,
...array_values($definition)
);
$route->enabledMiddlewaresCache = null;
return $route;
}
/**
* Appends action handler. It is a primary middleware definition that should be invoked last for a matched route.
*/
public function action(array|callable|string $middlewareDefinition): self
{
$route = clone $this;
$route->middlewares[] = $middlewareDefinition;
$route->actionAdded = true;
return $route;
}
/**
* Excludes middleware from being invoked when action is handled.
* It is useful to avoid invoking one of the parent group middleware for
* a certain route.
*/
public function disableMiddleware(mixed ...$definition): self
{
$route = clone $this;
array_push(
$route->disabledMiddlewares,
...array_values($definition)
);
$route->enabledMiddlewaresCache = null;
return $route;
}
/**
* @psalm-template T as string
*
* @psalm-param T $key
*
* @psalm-return (
* T is ('name'|'pattern') ? string :
* (T is 'host' ? string|null :
* (T is 'hosts' ? array<array-key, string> :
* (T is 'methods' ? array<array-key,string> :
* (T is 'defaults' ? array<string,string> :
* (T is ('override'|'hasMiddlewares') ? bool :
* (T is 'enabledMiddlewares' ? array<array-key,array|callable|string> : mixed)
* )
* )
* )
* )
* )
* )
*/
public function getData(string $key): mixed
{
return match ($key) {
'name' => $this->name ??
(implode(', ', $this->methods) . ' ' . implode('|', $this->hosts) . $this->pattern),
'pattern' => $this->pattern,
'host' => $this->hosts[0] ?? null,
'hosts' => $this->hosts,
'methods' => $this->methods,
'defaults' => $this->defaults,
'override' => $this->override,
'hasMiddlewares' => $this->middlewares !== [],
'enabledMiddlewares' => $this->getEnabledMiddlewares(),
default => throw new InvalidArgumentException('Unknown data key: ' . $key),
};
}
public function __toString(): string
{
$result = $this->name === null
? ''
: '[' . $this->name . '] ';
if ($this->methods !== []) {
$result .= implode(',', $this->methods) . ' ';
}
if ($this->hosts) {
$quoted = array_map(static fn ($host) => preg_quote($host, '/'), $this->hosts);
if (!preg_match('/' . implode('|', $quoted) . '/', $this->pattern)) {
$result .= implode('|', $this->hosts);
}
}
$result .= $this->pattern;
return $result;
}
public function __debugInfo()
{
return [
'name' => $this->name,
'methods' => $this->methods,
'pattern' => $this->pattern,
'hosts' => $this->hosts,
'defaults' => $this->defaults,
'override' => $this->override,
'actionAdded' => $this->actionAdded,
'middlewares' => $this->middlewares,
'disabledMiddlewares' => $this->disabledMiddlewares,
'enabledMiddlewares' => $this->getEnabledMiddlewares(),
];
}
/**
* @return array[]|callable[]|string[]
* @psalm-return list<array|callable|string>
*/
private function getEnabledMiddlewares(): array
{
if ($this->enabledMiddlewaresCache !== null) {
return $this->enabledMiddlewaresCache;
}
$this->enabledMiddlewaresCache = MiddlewareFilter::filter($this->middlewares, $this->disabledMiddlewares);
return $this->enabledMiddlewaresCache;
}
}