Skip to content

Commit d2007bf

Browse files
committed
bug symfony#13912 [DependencyInjection] Highest precedence for user parameters (lyrixx)
This PR was merged into the 2.3 branch. Discussion ---------- [DependencyInjection] Highest precedence for user parameters | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | ~ | License | MIT | Doc PR | ~ 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 generally 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. Commits ------- ce2764d [DependencyInjection] Highest precedence for user parameters
2 parents 4b7e5fa + ce2764d commit d2007bf

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

src/Symfony/Component/DependencyInjection/Compiler/MergeExtensionConfigurationPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ public function process(ContainerBuilder $container)
5050
$extension->load($config, $tmpContainer);
5151

5252
$container->merge($tmpContainer);
53+
$container->getParameterBag()->add($parameters);
5354
}
5455

5556
$container->addDefinitions($definitions);
5657
$container->addAliases($aliases);
57-
$container->getParameterBag()->add($parameters);
5858
}
5959
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\MergeExtensionConfigurationPass;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
8+
9+
class MergeExtensionConfigurationPassTest extends \PHPUnit_Framework_TestCase
10+
{
11+
public function testUserParametersAreMostImportantThanDefaultOne()
12+
{
13+
$container = new ContainerBuilder();
14+
$container->getParameterBag()->set('key', 'user_value');
15+
$container->registerExtension(new ExtensionA());
16+
$container->loadFromExtension('a');
17+
$container->registerExtension($b = new ExtensionB());
18+
$container->loadFromExtension('b');
19+
20+
$pass = new MergeExtensionConfigurationPass();
21+
$pass->process($container);
22+
23+
$this->assertSame('user_value', $container->getParameter('key'));
24+
$this->assertSame('user_value', $b->parameterKey);
25+
}
26+
}
27+
28+
abstract class Extension implements ExtensionInterface
29+
{
30+
public function getNamespace()
31+
{
32+
return 'http://example.org/schema/dic/'.$this->getAlias();
33+
}
34+
35+
public function getXsdValidationBasePath()
36+
{
37+
return false;
38+
}
39+
}
40+
41+
class ExtensionA extends Extension
42+
{
43+
public function load(array $config, ContainerBuilder $container)
44+
{
45+
$container->getParameterBag()->set('key', 'default_value');
46+
}
47+
48+
public function getAlias()
49+
{
50+
return 'a';
51+
}
52+
}
53+
54+
class ExtensionB extends Extension
55+
{
56+
public $parameterKey;
57+
58+
public function load(array $config, ContainerBuilder $container)
59+
{
60+
$this->parameterKey = $container->getParameter('key');
61+
}
62+
63+
public function getAlias()
64+
{
65+
return 'b';
66+
}
67+
}

0 commit comments

Comments
 (0)