From 94df570b502d9019f295ed84663608a4bdefb574 Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Tue, 6 Aug 2024 18:12:57 -0400 Subject: [PATCH] Fix importing PHP config in prepend extension method --- .../DependencyInjection/Loader/FileLoader.php | 1 + .../Loader/PhpFileLoader.php | 7 ++++++- .../Tests/Loader/PhpFileLoaderTest.php | 18 +++++++++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php index 669082179675b..b39a86ee82c44 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php @@ -92,6 +92,7 @@ public function import(mixed $resource, ?string $type = null, bool|string $ignor } } finally { --$this->importing; + $this->loadExtensionConfigs(); } return null; diff --git a/src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php index f6d032a4a0473..d1600809a6872 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php @@ -143,7 +143,12 @@ private function executeCallback(callable $callback, ContainerConfigurator $cont // Force load ContainerConfigurator to make env(), param() etc available. class_exists(ContainerConfigurator::class); - $callback(...$arguments); + ++$this->importing; + try { + $callback(...$arguments); + } finally { + --$this->importing; + } foreach ($configBuilders as $configBuilder) { $this->loadExtensionConfig($configBuilder->getExtensionAlias(), ContainerConfigurator::processValue($configBuilder->toArray())); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php index d7df9b6f11875..8682991c117a4 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php @@ -47,7 +47,7 @@ public function testLoad() $this->assertEquals('foo', $container->getParameter('foo'), '->load() loads a PHP file resource'); } - public function testPrependExtensionConfig() + public function testPrependExtensionConfigWithLoadMethod() { $container = new ContainerBuilder(); $container->registerExtension(new \AcmeExtension()); @@ -63,6 +63,22 @@ public function testPrependExtensionConfig() $this->assertSame($expected, $container->getExtensionConfig('acme')); } + public function testPrependExtensionConfigWithImportMethod() + { + $container = new ContainerBuilder(); + $container->registerExtension(new \AcmeExtension()); + $container->prependExtensionConfig('acme', ['foo' => 'bar']); + $loader = new PhpFileLoader($container, new FileLocator(\dirname(__DIR__).'/Fixtures'), 'prod', new ConfigBuilderGenerator(sys_get_temp_dir()), true); + $loader->import('config/config_builder.php'); + + $expected = [ + ['color' => 'red'], + ['color' => 'blue'], + ['foo' => 'bar'], + ]; + $this->assertSame($expected, $container->getExtensionConfig('acme')); + } + public function testConfigServices() { $fixtures = realpath(__DIR__.'/../Fixtures');