Skip to content

Commit 287e74a

Browse files
[DependencyInjection][Routing] Fix nested config imports when returning PHP arrays
1 parent ac4e4da commit 287e74a

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

Loader/PhpFileLoader.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Config\Builder\ConfigBuilderGeneratorInterface;
1616
use Symfony\Component\Config\Builder\ConfigBuilderInterface;
1717
use Symfony\Component\Config\FileLocatorInterface;
18+
use Symfony\Component\Config\Loader\LoaderResolver;
1819
use Symfony\Component\DependencyInjection\Attribute\When;
1920
use Symfony\Component\DependencyInjection\Attribute\WhenNot;
2021
use Symfony\Component\DependencyInjection\Container;
@@ -121,8 +122,14 @@ public function load(mixed $resource, ?string $type = null): mixed
121122
throw new InvalidArgumentException(\sprintf('Invalid key "%s" returned for the "%s" config builder; none or "services" expected in file "%s".', $key, get_debug_type($config), $path));
122123
}
123124
$yamlLoader = new YamlFileLoader($this->container, $this->locator, $this->env, $this->prepend);
125+
$yamlLoader->setResolver(new LoaderResolver([$this]));
124126
$loadContent = new \ReflectionMethod(YamlFileLoader::class, 'loadContent');
125-
$loadContent->invoke($yamlLoader, ContainerConfigurator::processValue((array) $config), $path);
127+
++$this->importing;
128+
try {
129+
$loadContent->invoke($yamlLoader, ContainerConfigurator::processValue((array) $config), $path);
130+
} finally {
131+
--$this->importing;
132+
}
126133
} elseif ($config instanceof ConfigBuilderInterface) {
127134
if (\is_string($key) && $config->getExtensionAlias() !== $key) {
128135
throw new InvalidArgumentException(\sprintf('The extension alias "%s" of the "%s" config builder does not match the key "%s" in file "%s".', $config->getExtensionAlias(), get_debug_type($config), $key, $path));
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
22

3-
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
43
use Symfony\Component\DependencyInjection\Tests\Fixtures\AcmeConfig;
54

65
if ('prod' !== $env) {
76
return;
87
}
98

10-
return function (AcmeConfig $config, ContainerConfigurator $c) {
11-
$c->import('nested_config_builder.php');
12-
13-
$config->color('blue');
14-
};
9+
return [
10+
'imports' => [
11+
'nested_config_builder.php',
12+
],
13+
new AcmeConfig(['color' => 'blue']),
14+
];

Tests/Loader/PhpFileLoaderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public function testPrependExtensionConfigWithLoadMethod()
6868
$container = new ContainerBuilder();
6969
$container->registerExtension(new \AcmeExtension());
7070
$container->prependExtensionConfig('acme', ['foo' => 'bar']);
71-
$loader = new PhpFileLoader($container, new FileLocator(\dirname(__DIR__).'/Fixtures'), 'prod', new ConfigBuilderGenerator(sys_get_temp_dir()), true);
72-
$loader->load('config/config_builder.php');
71+
$loader = new PhpFileLoader($container, new FileLocator(\dirname(__DIR__).'/Fixtures/config'), 'prod', new ConfigBuilderGenerator(sys_get_temp_dir()), true);
72+
$loader->load('config_builder.php');
7373

7474
$expected = [
7575
['color' => 'red'],
@@ -84,8 +84,8 @@ public function testPrependExtensionConfigWithImportMethod()
8484
$container = new ContainerBuilder();
8585
$container->registerExtension(new \AcmeExtension());
8686
$container->prependExtensionConfig('acme', ['foo' => 'bar']);
87-
$loader = new PhpFileLoader($container, new FileLocator(\dirname(__DIR__).'/Fixtures'), 'prod', new ConfigBuilderGenerator(sys_get_temp_dir()), true);
88-
$loader->import('config/config_builder.php');
87+
$loader = new PhpFileLoader($container, new FileLocator(\dirname(__DIR__).'/Fixtures/config'), 'prod', new ConfigBuilderGenerator(sys_get_temp_dir()), true);
88+
$loader->import('config_builder.php');
8989

9090
$expected = [
9191
['color' => 'red'],

0 commit comments

Comments
 (0)