8000 [DI] ContainerBuilder::compile() can optionally resolve env vars in p… · symfony/symfony@8b278b2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8b278b2

Browse files
[DI] ContainerBuilder::compile() can optionally resolve env vars in parameter bag
1 parent b1098d9 commit 8b278b2

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
2626
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
2727
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
28+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
2829
use Symfony\Component\Config\Resource\FileResource;
2930
use Symfony\Component\Config\Resource\ResourceInterface;
3031
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\InstantiatorInterface;
@@ -555,15 +556,27 @@ public function prependExtensionConfig($name, array $config)
555556
* * The parameter bag is frozen;
556557
* * Extension loading is disabled.
557558
*/
558-
public function compile()
559+
public function compile(/* $resolveEnvPlaceholders = false */)
559560
{
561+
if (__CLASS__ !== static::class) {
562+
$r = new \ReflectionMethod($this, __FUNCTION__);
563+
if (__CLASS__ !== $r->getDeclaringClass()->getName() && (1 > $r->getNumberOfParameters() || 'resolveEnvPlaceholders' !== $r->getParameters()[0]->name)) {
564+
@trigger_error(sprintf('The %s::compile() method expects a first "$resolveEnvPlaceholders" argument since version 3.3. It will be made mandatory in 4.0.', static::class), E_USER_DEPRECATED);
565+
}
566+
}
567+
$resolveEnvPlaceholders = 0 < func_num_args() ? func_get_arg(0) : false;
560568
$compiler = $this->getCompiler();
561569

562570
if ($this->trackResources) {
563571
foreach ($compiler->getPassConfig()->getPasses() as $pass) {
564572
$this->addObjectResource($pass);
565573
}
566574
}
575+
$bag = $this->getParameterBag();
576+
577+
if ($resolveEnvPlaceholders && $bag instanceof EnvPlaceholderParameterBag) {
578+
$this->parameterBag = $bag = new ParameterBag($this->resolveEnvPlaceholders($bag->all(), true));
579+
}
567580

568581
$compiler->compile($this);
569582

@@ -577,7 +590,6 @@ public function compile()
577590
}
578591

579592
$this->extensionConfigs = array();
580-
$bag = $this->getParameterBag();
581593

582594
parent::compile();
583595

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,19 @@ public function testResolveEnvValues()
548548
unset($_ENV['DUMMY_ENV_VAR']);
549549
}
550550

551+
public function testCompileWithResolveEnv()
552+
{
553+
$_ENV['DUMMY_ENV_VAR'] = 'du%%y';
554+
555+
$container = new ContainerBuilder();
556+
$container->setParameter('bar', '%% %env(DUMMY_ENV_VAR)%');
557+
$container->compile(true);
558+
559+
$this->assertSame('% du%%y', $container->getParameter('bar'));
560+
561+
unset($_ENV['DUMMY_ENV_VAR']);
562+
}
563+
551564
/**
552565
* @expectedException \LogicException
553566
*/

0 commit comments

Comments
 (0)
0