|
13 | 13 |
|
14 | 14 | use Symfony\Component\Config\Loader\FileLoader; |
15 | 15 | use Symfony\Component\Config\Resource\FileResource; |
| 16 | +use Symfony\Component\Routing\Exception\InvalidArgumentException; |
16 | 17 | use Symfony\Component\Routing\Loader\Configurator\AliasConfigurator; |
17 | 18 | use Symfony\Component\Routing\Loader\Configurator\CollectionConfigurator; |
18 | 19 | use Symfony\Component\Routing\Loader\Configurator\ImportConfigurator; |
19 | 20 | use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator; |
20 | 21 | use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; |
21 | 22 | use Symfony\Component\Routing\RouteCollection; |
| 23 | +use Symfony\Config\RoutesConfig; |
22 | 24 |
|
23 | 25 | /** |
24 | 26 | * PhpFileLoader loads routes from a PHP file. |
@@ -105,38 +107,45 @@ private function loadRoutes(RouteCollection $collection, mixed $routes, string $ |
105 | 107 | return; |
106 | 108 | } |
107 | 109 |
|
108 | | - if (!is_iterable($routes)) { |
109 | | - throw new \InvalidArgumentException(\sprintf('The return value in config file "%s" is invalid: "%s" given.', $path, get_debug_type($routes))); |
| 110 | + if ($routes instanceof RoutesConfig) { |
| 111 | + $routes = [$routes]; |
| 112 | + } elseif (!is_iterable($routes)) { |
| 113 | + throw new InvalidArgumentException(\sprintf('The return value in config file "%s" is invalid: "%s" given.', $path, get_debug_type($routes))); |
110 | 114 | } |
111 | 115 |
|
112 | 116 | $loader = new YamlFileLoader($this->locator, $this->env); |
113 | 117 |
|
114 | 118 | \Closure::bind(function () use ($collection, $routes, $path, $file) { |
115 | 119 | foreach ($routes as $name => $config) { |
| 120 | + $when = $name; |
116 | 121 | if (str_starts_with($name, 'when@')) { |
117 | 122 | if (!$this->env || 'when@'.$this->env !== $name) { |
118 | 123 | continue; |
119 | 124 | } |
| 125 | + $when .= '" when "@'.$this->env; |
| 126 | + } elseif (!$config instanceof RoutesConfig) { |
| 127 | + $config = [$name => $config]; |
| 128 | + } elseif (!\is_int($name)) { |
| 129 | + throw new InvalidArgumentException(\sprintf('Invalid key "%s" returned for the "%s" config builder; none or "when@%%env%%" expected in file "%s".', $name, get_debug_type($config), $path)); |
| 130 | + } |
120 | 131 |
|
121 | | - foreach ($config as $name => $config) { |
122 | | - $this->validate($config, $name.'" when "@'.$this->env, $path); |
123 | | - |
124 | | - if (isset($config['resource'])) { |
125 | | - $this->parseImport($collection, $config, $path, $file); |
126 | | - } else { |
127 | | - $this->parseRoute($collection, $name, $config, $path); |
128 | | - } |
129 | | - } |
130 | | - |
131 | | - continue; |
| 132 | + if ($config instanceof RoutesConfig) { |
| 133 | + $config = $config->routes; |
| 134 | + } elseif (!is_iterable($config)) { |
| 135 | + throw new InvalidArgumentException(\sprintf('The "%s" key should contain an array in "%s".', $name, $path)); |
132 | 136 | } |
133 | 137 |
|
134 | | - $this->validate($config, $name, $path); |
| 138 | + foreach ($config as $name => $config) { |
| 139 | + if (str_starts_with($name, 'when@')) { |
| 140 | + throw new InvalidArgumentException(\sprintf('A route name cannot start with "when@" in "%s".', $path)); |
| 141 | + } |
| 142 | + $this->validate($config, $when, $path); |
135 | 143 |
|
136 | | - if (isset($config['resource'])) { |
137 | | - $this->parseImport($collection, $config, $path, $file); |
138 | | - } else { |
139 | | - $this->parseRoute($collection, $name, $config, $path); |
| 144 | + if (isset($config['resource'])) { |
| 145 | + $this->parseImport($collection, $config, $path, $file); |
| 146 | + } else { |
| 147 | + $this->parseRoute($collection, $name, $config, $path); |
| 148 | + } |
140 | 149 | } |
141 | 150 | } |
142 | 151 | }, $loader, $loader::class)(); |
|
0 commit comments