Skip to content

Commit 9c041a2

Browse files
[DependencyInjection][Routing] Fix nested config imports when returning PHP arrays
1 parent fac639d commit 9c041a2

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

Loader/PhpFileLoader.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Routing\Loader;
1313

1414
use Symfony\Component\Config\Loader\FileLoader;
15+
use Symfony\Component\Config\Loader\LoaderResolver;
1516
use Symfony\Component\Config\Resource\FileResource;
1617
use Symfony\Component\Routing\Exception\InvalidArgumentException;
1718
use Symfony\Component\Routing\Loader\Configurator\AliasConfigurator;
@@ -114,6 +115,7 @@ private function loadRoutes(RouteCollection $collection, mixed $routes, string $
114115
}
115116

116117
$loader = new YamlFileLoader($this->locator, $this->env);
118+
$loader->setResolver(new LoaderResolver([$this]));
117119

118120
\Closure::bind(function () use ($collection, $routes, $path, $file) {
119121
foreach ($routes as $name => $config) {
@@ -148,7 +150,7 @@ private function loadRoutes(RouteCollection $collection, mixed $routes, string $
148150
}
149151
}
150152
}
151-
}, $loader, $loader::class)();
153+
}, $loader, YamlFileLoader::class)();
152154
}
153155
}
154156

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
routes_from_php:
2+
resource: validpattern.php
3+
type: php
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
return [
4+
'blog_show' => [
5+
'resource' => 'importer-php-returns-array-with-import.yml',
6+
],
7+
'direct' => [
8+
'path' => '/direct',
9+
],
10+
];

Tests/Loader/PhpFileLoaderTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,17 @@ public function testWhenEnvWithArray()
392392
$this->assertSame('/x', $routes->get('x')->getPath());
393393
}
394394

395+
public function testYamlImportsAreResolvedWhenProcessingPhpReturnedArrays()
396+
{
397+
$locator = new FileLocator([__DIR__.'/../Fixtures']);
398+
$loader = new PhpFileLoader($locator);
399+
400+
$routes = $loader->load('importer-php-returns-array.php');
401+
402+
$this->assertSame('/blog/{slug}', $routes->get('blog_show')->getPath());
403+
$this->assertSame('/direct', $routes->get('direct')->getPath());
404+
}
405+
395406
#[DataProvider('providePsr4ConfigFiles')]
396407
public function testImportAttributesWithPsr4Prefix(string $configFile)
397408
{

0 commit comments

Comments
 (0)