Skip to content

Commit ba77be3

Browse files
[Routing] Allow when@env inside new RoutesConfig() trees
1 parent 9c041a2 commit ba77be3

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
@@ -109,7 +109,7 @@ private function loadRoutes(RouteCollection $collection, mixed $routes, string $
109109
}
110110

111111
if ($routes instanceof RoutesConfig) {
112-
$routes = [$routes];
112+
$routes = $routes->routes;
113113
} elseif (!is_iterable($routes)) {
114114
throw new InvalidArgumentException(\sprintf('The return value in config file "%s" is invalid: "%s" given.', $path, get_debug_type($routes)));
115115
}
@@ -119,8 +119,7 @@ private function loadRoutes(RouteCollection $collection, mixed $routes, string $
119119

120120
\Closure::bind(function () use ($collection, $routes, $path, $file) {
121121
foreach ($routes as $name => $config) {
122-
$when = $name;
123-
if (str_starts_with($name, 'when@')) {
122+
if (str_starts_with($when = $name, 'when@')) {
124123
if (!$this->env || 'when@'.$this->env !== $name) {
125124
continue;
126125
}
@@ -133,22 +132,11 @@ private function loadRoutes(RouteCollection $collection, mixed $routes, string $
133132

134133
if ($config instanceof RoutesConfig) {
135134
$config = $config->routes;
136-
} elseif (!is_iterable($config)) {
135+
} elseif (!\is_array($config)) {
137136
throw new InvalidArgumentException(\sprintf('The "%s" key should contain an array in "%s".', $name, $path));
138137
}
139138

140-
foreach ($config as $name => $config) {
141-
if (str_starts_with($name, 'when@')) {
142-
throw new InvalidArgumentException(\sprintf('A route name cannot start with "when@" in "%s".', $path));
143-
}
144-
$this->validate($config, $when, $path);
145-
146-
if (isset($config['resource'])) {
147-
$this->parseImport($collection, $config, $path, $file);
148-
} else {
149-
$this->parseRoute($collection, $name, $config, $path);
150-
}
151-
}
139+
$this->loadContent($collection, $config, $path, $file);
152140
}
153141
}, $loader, YamlFileLoader::class)();
154142
}

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
@@ -381,6 +381,20 @@ public function testLoadsObjectRoutes()
381381
$this->assertSame('/a', $routes->get('a')->getPath());
382382
$this->assertSame('/b', $routes->get('b')->getPath());
383383
$this->assertSame(['GET'], $routes->get('b')->getMethods());
384+
$this->assertNull($routes->get('c'));
385+
$this->assertNull($routes->get('d'));
386+
387+
$loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures']), 'dev');
388+
$routes = $loader->load('routes_object.php');
389+
$this->assertSame('/a', $routes->get('a')->getPath());
390+
$this->assertSame('/b', $routes->get('b')->getPath());
391+
$this->assertSame('/c', $routes->get('c')->getPath());
392+
$this->assertNull($routes->get('d'));
393+
394+
$loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures']), 'test');
395+
$routes = $loader->load('routes_object.php');
396+
$this->assertNull($routes->get('c'));
397+
$this->assertSame('/d', $routes->get('d')->getPath());
384398
}
385399

386400
public function testWhenEnvWithArray()

0 commit comments

Comments
 (0)