| 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\Loader\Configurator;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\Routing\Route;
|
|
|
15 |
use Symfony\Component\Routing\RouteCollection;
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* @author Nicolas Grekas <p@tchwork.com>
|
|
|
19 |
*/
|
|
|
20 |
class CollectionConfigurator
|
|
|
21 |
{
|
|
|
22 |
use Traits\AddTrait;
|
|
|
23 |
use Traits\HostTrait;
|
|
|
24 |
use Traits\RouteTrait;
|
|
|
25 |
|
|
|
26 |
private RouteCollection $parent;
|
|
|
27 |
private ?CollectionConfigurator $parentConfigurator;
|
|
|
28 |
private ?array $parentPrefixes;
|
|
|
29 |
private string|array|null $host = null;
|
|
|
30 |
|
|
|
31 |
public function __construct(RouteCollection $parent, string $name, self $parentConfigurator = null, array $parentPrefixes = null)
|
|
|
32 |
{
|
|
|
33 |
$this->parent = $parent;
|
|
|
34 |
$this->name = $name;
|
|
|
35 |
$this->collection = new RouteCollection();
|
|
|
36 |
$this->route = new Route('');
|
|
|
37 |
$this->parentConfigurator = $parentConfigurator; // for GC control
|
|
|
38 |
$this->parentPrefixes = $parentPrefixes;
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
public function __sleep(): array
|
|
|
42 |
{
|
|
|
43 |
throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
public function __wakeup()
|
|
|
47 |
{
|
|
|
48 |
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
public function __destruct()
|
|
|
52 |
{
|
|
|
53 |
if (null === $this->prefixes) {
|
|
|
54 |
$this->collection->addPrefix($this->route->getPath());
|
|
|
55 |
}
|
|
|
56 |
if (null !== $this->host) {
|
|
|
57 |
$this->addHost($this->collection, $this->host);
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
$this->parent->addCollection($this->collection);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Creates a sub-collection.
|
|
|
65 |
*/
|
|
|
66 |
final public function collection(string $name = ''): self
|
|
|
67 |
{
|
|
|
68 |
return new self($this->collection, $this->name.$name, $this, $this->prefixes);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Sets the prefix to add to the path of all child routes.
|
|
|
73 |
*
|
|
|
74 |
* @param string|array $prefix the prefix, or the localized prefixes
|
|
|
75 |
*
|
|
|
76 |
* @return $this
|
|
|
77 |
*/
|
|
|
78 |
final public function prefix(string|array $prefix): static
|
|
|
79 |
{
|
|
|
80 |
if (\is_array($prefix)) {
|
|
|
81 |
if (null === $this->parentPrefixes) {
|
|
|
82 |
// no-op
|
|
|
83 |
} elseif ($missing = array_diff_key($this->parentPrefixes, $prefix)) {
|
|
|
84 |
throw new \LogicException(sprintf('Collection "%s" is missing prefixes for locale(s) "%s".', $this->name, implode('", "', array_keys($missing))));
|
|
|
85 |
} else {
|
|
|
86 |
foreach ($prefix as $locale => $localePrefix) {
|
|
|
87 |
if (!isset($this->parentPrefixes[$locale])) {
|
|
|
88 |
throw new \LogicException(sprintf('Collection "%s" with locale "%s" is missing a corresponding prefix in its parent collection.', $this->name, $locale));
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
$prefix[$locale] = $this->parentPrefixes[$locale].$localePrefix;
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
$this->prefixes = $prefix;
|
|
|
95 |
$this->route->setPath('/');
|
|
|
96 |
} else {
|
|
|
97 |
$this->prefixes = null;
|
|
|
98 |
$this->route->setPath($prefix);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
return $this;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Sets the host to use for all child routes.
|
|
|
106 |
*
|
|
|
107 |
* @param string|array $host the host, or the localized hosts
|
|
|
108 |
*
|
|
|
109 |
* @return $this
|
|
|
110 |
*/
|
|
|
111 |
final public function host(string|array $host): static
|
|
|
112 |
{
|
|
|
113 |
$this->host = $host;
|
|
|
114 |
|
|
|
115 |
return $this;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
private function createRoute(string $path): Route
|
|
|
119 |
{
|
|
|
120 |
return (clone $this->route)->setPath($path);
|
|
|
121 |
}
|
|
|
122 |
}
|