| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Routing;
|
|
|
13 |
|
|
|
14 |
/**
|
|
|
15 |
* A Route describes a route and its parameters.
|
|
|
16 |
*
|
|
|
17 |
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
18 |
* @author Tobias Schultze <http://tobion.de>
|
|
|
19 |
*/
|
|
|
20 |
class Route implements \Serializable
|
|
|
21 |
{
|
|
|
22 |
private string $path = '/';
|
|
|
23 |
private string $host = '';
|
|
|
24 |
private array $schemes = [];
|
|
|
25 |
private array $methods = [];
|
|
|
26 |
private array $defaults = [];
|
|
|
27 |
private array $requirements = [];
|
|
|
28 |
private array $options = [];
|
|
|
29 |
private string $condition = '';
|
|
|
30 |
private ?CompiledRoute $compiled = null;
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Constructor.
|
|
|
34 |
*
|
|
|
35 |
* Available options:
|
|
|
36 |
*
|
|
|
37 |
* * compiler_class: A class name able to compile this route instance (RouteCompiler by default)
|
|
|
38 |
* * utf8: Whether UTF-8 matching is enforced ot not
|
|
|
39 |
*
|
|
|
40 |
* @param string $path The path pattern to match
|
|
|
41 |
* @param array $defaults An array of default parameter values
|
|
|
42 |
* @param array<string|\Stringable> $requirements An array of requirements for parameters (regexes)
|
|
|
43 |
* @param array $options An array of options
|
|
|
44 |
* @param string|null $host The host pattern to match
|
|
|
45 |
* @param string|string[] $schemes A required URI scheme or an array of restricted schemes
|
|
|
46 |
* @param string|string[] $methods A required HTTP method or an array of restricted methods
|
|
|
47 |
* @param string|null $condition A condition that should evaluate to true for the route to match
|
|
|
48 |
*/
|
|
|
49 |
public function __construct(string $path, array $defaults = [], array $requirements = [], array $options = [], ?string $host = '', string|array $schemes = [], string|array $methods = [], ?string $condition = '')
|
|
|
50 |
{
|
|
|
51 |
$this->setPath($path);
|
|
|
52 |
$this->addDefaults($defaults);
|
|
|
53 |
$this->addRequirements($requirements);
|
|
|
54 |
$this->setOptions($options);
|
|
|
55 |
$this->setHost($host);
|
|
|
56 |
$this->setSchemes($schemes);
|
|
|
57 |
$this->setMethods($methods);
|
|
|
58 |
$this->setCondition($condition);
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
public function __serialize(): array
|
|
|
62 |
{
|
|
|
63 |
return [
|
|
|
64 |
'path' => $this->path,
|
|
|
65 |
'host' => $this->host,
|
|
|
66 |
'defaults' => $this->defaults,
|
|
|
67 |
'requirements' => $this->requirements,
|
|
|
68 |
'options' => $this->options,
|
|
|
69 |
'schemes' => $this->schemes,
|
|
|
70 |
'methods' => $this->methods,
|
|
|
71 |
'condition' => $this->condition,
|
|
|
72 |
'compiled' => $this->compiled,
|
|
|
73 |
];
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* @internal
|
|
|
78 |
*/
|
|
|
79 |
final public function serialize(): string
|
|
|
80 |
{
|
|
|
81 |
throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
public function __unserialize(array $data): void
|
|
|
85 |
{
|
|
|
86 |
$this->path = $data['path'];
|
|
|
87 |
$this->host = $data['host'];
|
|
|
88 |
$this->defaults = $data['defaults'];
|
|
|
89 |
$this->requirements = $data['requirements'];
|
|
|
90 |
$this->options = $data['options'];
|
|
|
91 |
$this->schemes = $data['schemes'];
|
|
|
92 |
$this->methods = $data['methods'];
|
|
|
93 |
|
|
|
94 |
if (isset($data['condition'])) {
|
|
|
95 |
$this->condition = $data['condition'];
|
|
|
96 |
}
|
|
|
97 |
if (isset($data['compiled'])) {
|
|
|
98 |
$this->compiled = $data['compiled'];
|
|
|
99 |
}
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* @internal
|
|
|
104 |
*/
|
|
|
105 |
final public function unserialize(string $serialized)
|
|
|
106 |
{
|
|
|
107 |
$this->__unserialize(unserialize($serialized));
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
public function getPath(): string
|
|
|
111 |
{
|
|
|
112 |
return $this->path;
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* @return $this
|
|
|
117 |
*/
|
|
|
118 |
public function setPath(string $pattern): static
|
|
|
119 |
{
|
|
|
120 |
$pattern = $this->extractInlineDefaultsAndRequirements($pattern);
|
|
|
121 |
|
|
|
122 |
// A pattern must start with a slash and must not have multiple slashes at the beginning because the
|
|
|
123 |
// generated path for this route would be confused with a network path, e.g. '//domain.com/path'.
|
|
|
124 |
$this->path = '/'.ltrim(trim($pattern), '/');
|
|
|
125 |
$this->compiled = null;
|
|
|
126 |
|
|
|
127 |
return $this;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
public function getHost(): string
|
|
|
131 |
{
|
|
|
132 |
return $this->host;
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
/**
|
|
|
136 |
* @return $this
|
|
|
137 |
*/
|
|
|
138 |
public function setHost(?string $pattern): static
|
|
|
139 |
{
|
|
|
140 |
$this->host = $this->extractInlineDefaultsAndRequirements((string) $pattern);
|
|
|
141 |
$this->compiled = null;
|
|
|
142 |
|
|
|
143 |
return $this;
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
/**
|
|
|
147 |
* Returns the lowercased schemes this route is restricted to.
|
|
|
148 |
* So an empty array means that any scheme is allowed.
|
|
|
149 |
*
|
|
|
150 |
* @return string[]
|
|
|
151 |
*/
|
|
|
152 |
public function getSchemes(): array
|
|
|
153 |
{
|
|
|
154 |
return $this->schemes;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
/**
|
|
|
158 |
* Sets the schemes (e.g. 'https') this route is restricted to.
|
|
|
159 |
* So an empty array means that any scheme is allowed.
|
|
|
160 |
*
|
|
|
161 |
* @param string|string[] $schemes The scheme or an array of schemes
|
|
|
162 |
*
|
|
|
163 |
* @return $this
|
|
|
164 |
*/
|
|
|
165 |
public function setSchemes(string|array $schemes): static
|
|
|
166 |
{
|
|
|
167 |
$this->schemes = array_map('strtolower', (array) $schemes);
|
|
|
168 |
$this->compiled = null;
|
|
|
169 |
|
|
|
170 |
return $this;
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
/**
|
|
|
174 |
* Checks if a scheme requirement has been set.
|
|
|
175 |
*/
|
|
|
176 |
public function hasScheme(string $scheme): bool
|
|
|
177 |
{
|
|
|
178 |
return \in_array(strtolower($scheme), $this->schemes, true);
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
/**
|
|
|
182 |
* Returns the uppercased HTTP methods this route is restricted to.
|
|
|
183 |
* So an empty array means that any method is allowed.
|
|
|
184 |
*
|
|
|
185 |
* @return string[]
|
|
|
186 |
*/
|
|
|
187 |
public function getMethods(): array
|
|
|
188 |
{
|
|
|
189 |
return $this->methods;
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
/**
|
|
|
193 |
* Sets the HTTP methods (e.g. 'POST') this route is restricted to.
|
|
|
194 |
* So an empty array means that any method is allowed.
|
|
|
195 |
*
|
|
|
196 |
* @param string|string[] $methods The method or an array of methods
|
|
|
197 |
*
|
|
|
198 |
* @return $this
|
|
|
199 |
*/
|
|
|
200 |
public function setMethods(string|array $methods): static
|
|
|
201 |
{
|
|
|
202 |
$this->methods = array_map('strtoupper', (array) $methods);
|
|
|
203 |
$this->compiled = null;
|
|
|
204 |
|
|
|
205 |
return $this;
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
public function getOptions(): array
|
|
|
209 |
{
|
|
|
210 |
return $this->options;
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
/**
|
|
|
214 |
* @return $this
|
|
|
215 |
*/
|
|
|
216 |
public function setOptions(array $options): static
|
|
|
217 |
{
|
|
|
218 |
$this->options = [
|
|
|
219 |
'compiler_class' => RouteCompiler::class,
|
|
|
220 |
];
|
|
|
221 |
|
|
|
222 |
return $this->addOptions($options);
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
/**
|
|
|
226 |
* @return $this
|
|
|
227 |
*/
|
|
|
228 |
public function addOptions(array $options): static
|
|
|
229 |
{
|
|
|
230 |
foreach ($options as $name => $option) {
|
|
|
231 |
$this->options[$name] = $option;
|
|
|
232 |
}
|
|
|
233 |
$this->compiled = null;
|
|
|
234 |
|
|
|
235 |
return $this;
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
/**
|
|
|
239 |
* Sets an option value.
|
|
|
240 |
*
|
|
|
241 |
* @return $this
|
|
|
242 |
*/
|
|
|
243 |
public function setOption(string $name, mixed $value): static
|
|
|
244 |
{
|
|
|
245 |
$this->options[$name] = $value;
|
|
|
246 |
$this->compiled = null;
|
|
|
247 |
|
|
|
248 |
return $this;
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
/**
|
|
|
252 |
* Returns the option value or null when not found.
|
|
|
253 |
*/
|
|
|
254 |
public function getOption(string $name): mixed
|
|
|
255 |
{
|
|
|
256 |
return $this->options[$name] ?? null;
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
public function hasOption(string $name): bool
|
|
|
260 |
{
|
|
|
261 |
return \array_key_exists($name, $this->options);
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
public function getDefaults(): array
|
|
|
265 |
{
|
|
|
266 |
return $this->defaults;
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
/**
|
|
|
270 |
* @return $this
|
|
|
271 |
*/
|
|
|
272 |
public function setDefaults(array $defaults): static
|
|
|
273 |
{
|
|
|
274 |
$this->defaults = [];
|
|
|
275 |
|
|
|
276 |
return $this->addDefaults($defaults);
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
/**
|
|
|
280 |
* @return $this
|
|
|
281 |
*/
|
|
|
282 |
public function addDefaults(array $defaults): static
|
|
|
283 |
{
|
|
|
284 |
if (isset($defaults['_locale']) && $this->isLocalized()) {
|
|
|
285 |
unset($defaults['_locale']);
|
|
|
286 |
}
|
|
|
287 |
|
|
|
288 |
foreach ($defaults as $name => $default) {
|
|
|
289 |
$this->defaults[$name] = $default;
|
|
|
290 |
}
|
|
|
291 |
$this->compiled = null;
|
|
|
292 |
|
|
|
293 |
return $this;
|
|
|
294 |
}
|
|
|
295 |
|
|
|
296 |
public function getDefault(string $name): mixed
|
|
|
297 |
{
|
|
|
298 |
return $this->defaults[$name] ?? null;
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
public function hasDefault(string $name): bool
|
|
|
302 |
{
|
|
|
303 |
return \array_key_exists($name, $this->defaults);
|
|
|
304 |
}
|
|
|
305 |
|
|
|
306 |
/**
|
|
|
307 |
* @return $this
|
|
|
308 |
*/
|
|
|
309 |
public function setDefault(string $name, mixed $default): static
|
|
|
310 |
{
|
|
|
311 |
if ('_locale' === $name && $this->isLocalized()) {
|
|
|
312 |
return $this;
|
|
|
313 |
}
|
|
|
314 |
|
|
|
315 |
$this->defaults[$name] = $default;
|
|
|
316 |
$this->compiled = null;
|
|
|
317 |
|
|
|
318 |
return $this;
|
|
|
319 |
}
|
|
|
320 |
|
|
|
321 |
public function getRequirements(): array
|
|
|
322 |
{
|
|
|
323 |
return $this->requirements;
|
|
|
324 |
}
|
|
|
325 |
|
|
|
326 |
/**
|
|
|
327 |
* @return $this
|
|
|
328 |
*/
|
|
|
329 |
public function setRequirements(array $requirements): static
|
|
|
330 |
{
|
|
|
331 |
$this->requirements = [];
|
|
|
332 |
|
|
|
333 |
return $this->addRequirements($requirements);
|
|
|
334 |
}
|
|
|
335 |
|
|
|
336 |
/**
|
|
|
337 |
* @return $this
|
|
|
338 |
*/
|
|
|
339 |
public function addRequirements(array $requirements): static
|
|
|
340 |
{
|
|
|
341 |
if (isset($requirements['_locale']) && $this->isLocalized()) {
|
|
|
342 |
unset($requirements['_locale']);
|
|
|
343 |
}
|
|
|
344 |
|
|
|
345 |
foreach ($requirements as $key => $regex) {
|
|
|
346 |
$this->requirements[$key] = $this->sanitizeRequirement($key, $regex);
|
|
|
347 |
}
|
|
|
348 |
$this->compiled = null;
|
|
|
349 |
|
|
|
350 |
return $this;
|
|
|
351 |
}
|
|
|
352 |
|
|
|
353 |
public function getRequirement(string $key): ?string
|
|
|
354 |
{
|
|
|
355 |
return $this->requirements[$key] ?? null;
|
|
|
356 |
}
|
|
|
357 |
|
|
|
358 |
public function hasRequirement(string $key): bool
|
|
|
359 |
{
|
|
|
360 |
return \array_key_exists($key, $this->requirements);
|
|
|
361 |
}
|
|
|
362 |
|
|
|
363 |
/**
|
|
|
364 |
* @return $this
|
|
|
365 |
*/
|
|
|
366 |
public function setRequirement(string $key, string $regex): static
|
|
|
367 |
{
|
|
|
368 |
if ('_locale' === $key && $this->isLocalized()) {
|
|
|
369 |
return $this;
|
|
|
370 |
}
|
|
|
371 |
|
|
|
372 |
$this->requirements[$key] = $this->sanitizeRequirement($key, $regex);
|
|
|
373 |
$this->compiled = null;
|
|
|
374 |
|
|
|
375 |
return $this;
|
|
|
376 |
}
|
|
|
377 |
|
|
|
378 |
public function getCondition(): string
|
|
|
379 |
{
|
|
|
380 |
return $this->condition;
|
|
|
381 |
}
|
|
|
382 |
|
|
|
383 |
/**
|
|
|
384 |
* @return $this
|
|
|
385 |
*/
|
|
|
386 |
public function setCondition(?string $condition): static
|
|
|
387 |
{
|
|
|
388 |
$this->condition = (string) $condition;
|
|
|
389 |
$this->compiled = null;
|
|
|
390 |
|
|
|
391 |
return $this;
|
|
|
392 |
}
|
|
|
393 |
|
|
|
394 |
/**
|
|
|
395 |
* Compiles the route.
|
|
|
396 |
*
|
|
|
397 |
* @throws \LogicException If the Route cannot be compiled because the
|
|
|
398 |
* path or host pattern is invalid
|
|
|
399 |
*
|
|
|
400 |
* @see RouteCompiler which is responsible for the compilation process
|
|
|
401 |
*/
|
|
|
402 |
public function compile(): CompiledRoute
|
|
|
403 |
{
|
|
|
404 |
if (null !== $this->compiled) {
|
|
|
405 |
return $this->compiled;
|
|
|
406 |
}
|
|
|
407 |
|
|
|
408 |
$class = $this->getOption('compiler_class');
|
|
|
409 |
|
|
|
410 |
return $this->compiled = $class::compile($this);
|
|
|
411 |
}
|
|
|
412 |
|
|
|
413 |
private function extractInlineDefaultsAndRequirements(string $pattern): string
|
|
|
414 |
{
|
|
|
415 |
if (false === strpbrk($pattern, '?<')) {
|
|
|
416 |
return $pattern;
|
|
|
417 |
}
|
|
|
418 |
|
|
|
419 |
return preg_replace_callback('#\{(!?)([\w\x80-\xFF]++)(<.*?>)?(\?[^\}]*+)?\}#', function ($m) {
|
|
|
420 |
if (isset($m[4][0])) {
|
|
|
421 |
$this->setDefault($m[2], '?' !== $m[4] ? substr($m[4], 1) : null);
|
|
|
422 |
}
|
|
|
423 |
if (isset($m[3][0])) {
|
|
|
424 |
$this->setRequirement($m[2], substr($m[3], 1, -1));
|
|
|
425 |
}
|
|
|
426 |
|
|
|
427 |
return '{'.$m[1].$m[2].'}';
|
|
|
428 |
}, $pattern);
|
|
|
429 |
}
|
|
|
430 |
|
|
|
431 |
private function sanitizeRequirement(string $key, string $regex)
|
|
|
432 |
{
|
|
|
433 |
if ('' !== $regex) {
|
|
|
434 |
if ('^' === $regex[0]) {
|
|
|
435 |
$regex = substr($regex, 1);
|
|
|
436 |
} elseif (str_starts_with($regex, '\\A')) {
|
|
|
437 |
$regex = substr($regex, 2);
|
|
|
438 |
}
|
|
|
439 |
}
|
|
|
440 |
|
|
|
441 |
if (str_ends_with($regex, '$')) {
|
|
|
442 |
$regex = substr($regex, 0, -1);
|
|
|
443 |
} elseif (\strlen($regex) - 2 === strpos($regex, '\\z')) {
|
|
|
444 |
$regex = substr($regex, 0, -2);
|
|
|
445 |
}
|
|
|
446 |
|
|
|
447 |
if ('' === $regex) {
|
|
|
448 |
throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" cannot be empty.', $key));
|
|
|
449 |
}
|
|
|
450 |
|
|
|
451 |
return $regex;
|
|
|
452 |
}
|
|
|
453 |
|
|
|
454 |
private function isLocalized(): bool
|
|
|
455 |
{
|
|
|
456 |
return isset($this->defaults['_locale']) && isset($this->defaults['_canonical_route']) && ($this->requirements['_locale'] ?? null) === preg_quote($this->defaults['_locale']);
|
|
|
457 |
}
|
|
|
458 |
}
|