Route.php
12.2 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing;
/**
* A Route describes a route and its parameters.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Tobias Schultze <http://tobion.de>
*/
class Route implements \Serializable
{
private $path = '/';
private $host = '';
private $schemes = [];
private $methods = [];
private $defaults = [];
private $requirements = [];
private $options = [];
private $condition = '';
/**
* @var CompiledRoute|null
*/
private $compiled;
/**
* Constructor.
*
* Available options:
*
* * compiler_class: A class name able to compile this route instance (RouteCompiler by default)
* * utf8: Whether UTF-8 matching is enforced ot not
*
* @param string $path The path pattern to match
* @param array $defaults An array of default parameter values
* @param array $requirements An array of requirements for parameters (regexes)
* @param array $options An array of options
* @param string|null $host The host pattern to match
* @param string|string[] $schemes A required URI scheme or an array of restricted schemes
* @param string|string[] $methods A required HTTP method or an array of restricted methods
* @param string|null $condition A condition that should evaluate to true for the route to match
*/
public function __construct(string $path, array $defaults = [], array $requirements = [], array $options = [], ?string $host = '', $schemes = [], $methods = [], ?string $condition = '')
{
$this->setPath($path);
$this->addDefaults($defaults);
$this->addRequirements($requirements);
$this->setOptions($options);
$this->setHost($host);
$this->setSchemes($schemes);
$this->setMethods($methods);
$this->setCondition($condition);
}
public function __serialize(): array
{
return [
'path' => $this->path,
'host' => $this->host,
'defaults' => $this->defaults,
'requirements' => $this->requirements,
'options' => $this->options,
'schemes' => $this->schemes,
'methods' => $this->methods,
'condition' => $this->condition,
'compiled' => $this->compiled,
];
}
/**
* @internal
*/
final public function serialize(): string
{
return serialize($this->__serialize());
}
public function __unserialize(array $data): void
{
$this->path = $data['path'];
$this->host = $data['host'];
$this->defaults = $data['defaults'];
$this->requirements = $data['requirements'];
$this->options = $data['options'];
$this->schemes = $data['schemes'];
$this->methods = $data['methods'];
if (isset($data['condition'])) {
$this->condition = $data['condition'];
}
if (isset($data['compiled'])) {
$this->compiled = $data['compiled'];
}
}
/**
* @internal
*/
final public function unserialize($serialized)
{
$this->__unserialize(unserialize($serialized));
}
/**
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* @return $this
*/
public function setPath(string $pattern)
{
$pattern = $this->extractInlineDefaultsAndRequirements($pattern);
// A pattern must start with a slash and must not have multiple slashes at the beginning because the
// generated path for this route would be confused with a network path, e.g. '//domain.com/path'.
$this->path = '/'.ltrim(trim($pattern), '/');
$this->compiled = null;
return $this;
}
/**
* @return string
*/
public function getHost()
{
return $this->host;
}
/**
* @return $this
*/
public function setHost(?string $pattern)
{
$this->host = $this->extractInlineDefaultsAndRequirements((string) $pattern);
$this->compiled = null;
return $this;
}
/**
* Returns the lowercased schemes this route is restricted to.
* So an empty array means that any scheme is allowed.
*
* @return string[]
*/
public function getSchemes()
{
return $this->schemes;
}
/**
* Sets the schemes (e.g. 'https') this route is restricted to.
* So an empty array means that any scheme is allowed.
*
* @param string|string[] $schemes The scheme or an array of schemes
*
* @return $this
*/
public function setSchemes($schemes)
{
$this->schemes = array_map('strtolower', (array) $schemes);
$this->compiled = null;
return $this;
}
/**
* Checks if a scheme requirement has been set.
*
* @return bool
*/
public function hasScheme(string $scheme)
{
return \in_array(strtolower($scheme), $this->schemes, true);
}
/**
* Returns the uppercased HTTP methods this route is restricted to.
* So an empty array means that any method is allowed.
*
* @return string[]
*/
public function getMethods()
{
return $this->methods;
}
/**
* Sets the HTTP methods (e.g. 'POST') this route is restricted to.
* So an empty array means that any method is allowed.
*
* @param string|string[] $methods The method or an array of methods
*
* @return $this
*/
public function setMethods($methods)
{
$this->methods = array_map('strtoupper', (array) $methods);
$this->compiled = null;
return $this;
}
/**
* @return array
*/
public function getOptions()
{
return $this->options;
}
/**
* @return $this
*/
public function setOptions(array $options)
{
$this->options = [
'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
];
return $this->addOptions($options);
}
/**
* @return $this
*/
public function addOptions(array $options)
{
foreach ($options as $name => $option) {
$this->options[$name] = $option;
}
$this->compiled = null;
return $this;
}
/**
* Sets an option value.
*
* @param mixed $value The option value
*
* @return $this
*/
public function setOption(string $name, $value)
{
$this->options[$name] = $value;
$this->compiled = null;
return $this;
}
/**
* Returns the option value or null when not found.
*
* @return mixed
*/
public function getOption(string $name)
{
return $this->options[$name] ?? null;
}
/**
* @return bool
*/
public function hasOption(string $name)
{
return \array_key_exists($name, $this->options);
}
/**
* @return array
*/
public function getDefaults()
{
return $this->defaults;
}
/**
* @return $this
*/
public function setDefaults(array $defaults)
{
$this->defaults = [];
return $this->addDefaults($defaults);
}
/**
* @return $this
*/
public function addDefaults(array $defaults)
{
if (isset($defaults['_locale']) && $this->isLocalized()) {
unset($defaults['_locale']);
}
foreach ($defaults as $name => $default) {
$this->defaults[$name] = $default;
}
$this->compiled = null;
return $this;
}
/**
* @return mixed
*/
public function getDefault(string $name)
{
return $this->defaults[$name] ?? null;
}
/**
* @return bool
*/
public function hasDefault(string $name)
{
return \array_key_exists($name, $this->defaults);
}
/**
* Sets a default value.
*
* @param mixed $default The default value
*
* @return $this
*/
public function setDefault(string $name, $default)
{
if ('_locale' === $name && $this->isLocalized()) {
return $this;
}
$this->defaults[$name] = $default;
$this->compiled = null;
return $this;
}
/**
* @return array
*/
public function getRequirements()
{
return $this->requirements;
}
/**
* @return $this
*/
public function setRequirements(array $requirements)
{
$this->requirements = [];
return $this->addRequirements($requirements);
}
/**
* @return $this
*/
public function addRequirements(array $requirements)
{
if (isset($requirements['_locale']) && $this->isLocalized()) {
unset($requirements['_locale']);
}
foreach ($requirements as $key => $regex) {
$this->requirements[$key] = $this->sanitizeRequirement($key, $regex);
}
$this->compiled = null;
return $this;
}
/**
* @return string|null
*/
public function getRequirement(string $key)
{
return $this->requirements[$key] ?? null;
}
/**
* @return bool
*/
public function hasRequirement(string $key)
{
return \array_key_exists($key, $this->requirements);
}
/**
* @return $this
*/
public function setRequirement(string $key, string $regex)
{
if ('_locale' === $key && $this->isLocalized()) {
return $this;
}
$this->requirements[$key] = $this->sanitizeRequirement($key, $regex);
$this->compiled = null;
return $this;
}
/**
* @return string
*/
public function getCondition()
{
return $this->condition;
}
/**
* @return $this
*/
public function setCondition(?string $condition)
{
$this->condition = (string) $condition;
$this->compiled = null;
return $this;
}
/**
* Compiles the route.
*
* @return CompiledRoute
*
* @throws \LogicException If the Route cannot be compiled because the
* path or host pattern is invalid
*
* @see RouteCompiler which is responsible for the compilation process
*/
public function compile()
{
if (null !== $this->compiled) {
return $this->compiled;
}
$class = $this->getOption('compiler_class');
return $this->compiled = $class::compile($this);
}
private function extractInlineDefaultsAndRequirements(string $pattern): string
{
if (false === strpbrk($pattern, '?<')) {
return $pattern;
}
return preg_replace_callback('#\{(!?)(\w++)(<.*?>)?(\?[^\}]*+)?\}#', function ($m) {
if (isset($m[4][0])) {
$this->setDefault($m[2], '?' !== $m[4] ? substr($m[4], 1) : null);
}
if (isset($m[3][0])) {
$this->setRequirement($m[2], substr($m[3], 1, -1));
}
return '{'.$m[1].$m[2].'}';
}, $pattern);
}
private function sanitizeRequirement(string $key, string $regex)
{
if ('' !== $regex) {
if ('^' === $regex[0]) {
$regex = substr($regex, 1);
} elseif (0 === strpos($regex, '\\A')) {
$regex = substr($regex, 2);
}
}
if (str_ends_with($regex, '$')) {
$regex = substr($regex, 0, -1);
} elseif (\strlen($regex) - 2 === strpos($regex, '\\z')) {
$regex = substr($regex, 0, -2);
}
if ('' === $regex) {
throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" cannot be empty.', $key));
}
return $regex;
}
private function isLocalized(): bool
{
return isset($this->defaults['_locale']) && isset($this->defaults['_canonical_route']) && ($this->requirements['_locale'] ?? null) === preg_quote($this->defaults['_locale']);
}
}