Skip to content

Commit 645e093

Browse files
Merge branch '7.4' into 8.0
* 7.4: [Config][DependencyInjection] Deprecate the fluent PHP format for semantic configuration [Routing] Allow when@env inside `new RoutesConfig()` trees [Messenger] Simplify code [DependencyInjection] Split ImportsConfig and ParametersConfig out of ServicesConfig
2 parents 7b07c24 + ba77be3 commit 645e093

File tree

5 files changed

+53
-44
lines changed

5 files changed

+53
-44
lines changed

Loader/Config/RoutesConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
* alias: string,
5252
* deprecated?: array{package:string, version:string, message?:string},
5353
* }
54-
* @psalm-type Routes = array<string, Route|Import|Alias>
54+
* @psalm-type Routes = array<string, Route|Import|Alias|RoutesConfig|array<string, Route|Import|Alias>>
5555
*/
5656
class RoutesConfig
5757
{

Loader/PhpFileLoader.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private function loadRoutes(RouteCollection $collection, mixed $routes, string $
9797
}
9898

9999
if ($routes instanceof RoutesConfig) {
100-
$routes = [$routes];
100+
$routes = $routes->routes;
101101
} elseif (!is_iterable($routes)) {
102102
throw new InvalidArgumentException(\sprintf('The return value in config file "%s" is invalid: "%s" given.', $path, get_debug_type($routes)));
103103
}
@@ -107,8 +107,7 @@ private function loadRoutes(RouteCollection $collection, mixed $routes, string $
107107

108108
\Closure::bind(function () use ($collection, $routes, $path, $file) {
109109
foreach ($routes as $name => $config) {
110-
$when = $name;
111-
if (str_starts_with($name, 'when@')) {
110+
if (str_starts_with($when = $name, 'when@')) {
112111
if (!$this->env || 'when@'.$this->env !== $name) {
113112
continue;
114113
}
@@ -121,22 +120,11 @@ private function loadRoutes(RouteCollection $collection, mixed $routes, string $
121120

122121
if ($config instanceof RoutesConfig) {
123122
$config = $config->routes;
124-
} elseif (!is_iterable($config)) {
123+
} elseif (!\is_array($config)) {
125124
throw new InvalidArgumentException(\sprintf('The "%s" key should contain an array in "%s".', $name, $path));
126125
}
127126

128-
foreach ($config as $name => $config) {
129-
if (str_starts_with($name, 'when@')) {
130-
throw new InvalidArgumentException(\sprintf('A route name cannot start with "when@" in "%s".', $path));
131-
}
132-
$this->validate($config, $when, $path);
133-
134-
if (isset($config['resource'])) {
135-
$this->parseImport($collection, $config, $path, $file);
136-
} else {
137-
$this->parseRoute($collection, $name, $config, $path);
138-
}
139-
}
127+
$this->loadContent($collection, $config, $path, $file);
140128
}
141129
}, $loader, YamlFileLoader::class)();
142130
}

Loader/YamlFileLoader.php

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -74,33 +74,7 @@ public function load(mixed $file, ?string $type = null): RouteCollection
7474
throw new \InvalidArgumentException(\sprintf('The file "%s" must contain a YAML array.', $path));
7575
}
7676

77-
foreach ($parsedConfig as $name => $config) {
78-
if (str_starts_with($name, 'when@')) {
79-
if (!$this->env || 'when@'.$this->env !== $name) {
80-
continue;
81-
}
82-
83-
foreach ($config as $name => $config) {
84-
$this->validate($config, $name.'" when "@'.$this->env, $path);
85-
86-
if (isset($config['resource'])) {
87-
$this->parseImport($collection, $config, $path, $file);
88-
} else {
89-
$this->parseRoute($collection, $name, $config, $path);
90-
}
91-
}
92-
93-
continue;
94-
}
95-
96-
$this->validate($config, $name, $path);
97-
98-
if (isset($config['resource'])) {
99-
$this->parseImport($collection, $config, $path, $file);
100-
} else {
101-
$this->parseRoute($collection, $name, $config, $path);
102-
}
103-
}
77+
$this->loadContent($collection, $parsedConfig, $path, $file);
10478

10579
return $collection;
10680
}
@@ -273,6 +247,29 @@ protected function validate(mixed $config, string $name, string $path): void
273247
}
274248
}
275249

250+
private function loadContent(RouteCollection $collection, array $config, string $path, string $file): void
251+
{
252+
foreach ($config as $name => $config) {
253+
if (!str_starts_with($when = $name, 'when@')) {
254+
$config = [$name => $config];
255+
} elseif (!$this->env || 'when@'.$this->env !== $name) {
256+
continue;
257+
} else {
258+
$when .= '" when "@'.$this->env;
259+
}
260+
261+
foreach ($config as $name => $config) {
262+
$this->validate($config, $when, $path);
263+
264+
if (isset($config['resource'])) {
265+
$this->parseImport($collection, $config, $path, $file);
266+
} else {
267+
$this->parseRoute($collection, $name, $config, $path);
268+
}
269+
}
270+
}
271+
}
272+
276273
/**
277274
* @throws \InvalidArgumentException If one of the provided config keys is not supported,
278275
* something is missing or the combination is nonsense

Tests/Fixtures/routes_object.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,14 @@
1010
'path' => '/b',
1111
'methods' => ['GET'],
1212
],
13+
'when@dev' => new RoutesConfig([
14+
'c' => [
15+
'path' => '/c',
16+
],
17+
]),
18+
'when@test' => [
19+
'd' => [
20+
'path' => '/d',
21+
],
22+
],
1323
]);

Tests/Loader/PhpFileLoaderTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,20 @@ public function testLoadsObjectRoutes()
365365
$this->assertSame('/a', $routes->get('a')->getPath());
366366
$this->assertSame('/b', $routes->get('b')->getPath());
367367
$this->assertSame(['GET'], $routes->get('b')->getMethods());
368+
$this->assertNull($routes->get('c'));
369+
$this->assertNull($routes->get('d'));
370+
371+
$loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures']), 'dev');
372+
$routes = $loader->load('routes_object.php');
373+
$this->assertSame('/a', $routes->get('a')->getPath());
374+
$this->assertSame('/b', $routes->get('b')->getPath());
375+
$this->assertSame('/c', $routes->get('c')->getPath());
376+
$this->assertNull($routes->get('d'));
377+
378+
$loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures']), 'test');
379+
$routes = $loader->load('routes_object.php');
380+
$this->assertNull($routes->get('c'));
381+
$this->assertSame('/d', $routes->get('d')->getPath());
368382
}
369383

370384
public function testWhenEnvWithArray()

0 commit comments

Comments
 (0)