From ce2764d416c7287d2126e3d1fb9338f329c1d80f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Thu, 12 Mar 2015 15:14:24 +0100 Subject: [PATCH] [DependencyInjection] Highest precedence for user parameters If `router.request_context.host` is defined in `parameters.yml` and if this key is used in `globals` section of twig bundle then the value registered in the DIC/parameters is the default value (`localhost`). It occurs because generall the framework bundle is defined before the twig bundle. So after the first loop, the (user) value registered in the `ContainerBuilder` is overridden by the default value of the framework bundle. And so, when it comes the time to process twig bundle, the last one gets the default value, and not the user value. This patch force the merge of default value, but give an highest precedence of user parameters for each extensions. --- .../MergeExtensionConfigurationPass.php | 2 +- .../MergeExtensionConfigurationPassTest.php | 67 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Compiler/MergeExtensionConfigurationPassTest.php diff --git a/src/Symfony/Component/DependencyInjection/Compiler/MergeExtensionConfigurationPass.php b/src/Symfony/Component/DependencyInjection/Compiler/MergeExtensionConfigurationPass.php index eb38911c35..2596b4e19a 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/MergeExtensionConfigurationPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/MergeExtensionConfigurationPass.php @@ -50,10 +50,10 @@ class MergeExtensionConfigurationPass implements CompilerPassInterface $extension->load($config, $tmpContainer); $container->merge($tmpContainer); + $container->getParameterBag()->add($parameters); } $container->addDefinitions($definitions); $container->addAliases($aliases); - $container->getParameterBag()->add($parameters); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/MergeExtensionConfigurationPassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/MergeExtensionConfigurationPassTest.php new file mode 100644 index 0000000000..c16c4c5570 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/MergeExtensionConfigurationPassTest.php @@ -0,0 +1,67 @@ +getParameterBag()->set('key', 'user_value'); + $container->registerExtension(new ExtensionA()); + $container->loadFromExtension('a'); + $container->registerExtension($b = new ExtensionB()); + $container->loadFromExtension('b'); + + $pass = new MergeExtensionConfigurationPass(); + $pass->process($container); + + $this->assertSame('user_value', $container->getParameter('key')); + $this->assertSame('user_value', $b->parameterKey); + } +} + +abstract class Extension implements ExtensionInterface +{ + public function getNamespace() + { + return 'http://example.org/schema/dic/'.$this->getAlias(); + } + + public function getXsdValidationBasePath() + { + return false; + } +} + +class ExtensionA extends Extension +{ + public function load(array $config, ContainerBuilder $container) + { + $container->getParameterBag()->set('key', 'default_value'); + } + + public function getAlias() + { + return 'a'; + } +} + +class ExtensionB extends Extension +{ + public $parameterKey; + + public function load(array $config, ContainerBuilder $container) + { + $this->parameterKey = $container->getParameter('key'); + } + + public function getAlias() + { + return 'b'; + } +}