Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 13ea687

Browse filesBrowse files
[DI] ContainerBuilder::compile() can optionally resolve env vars in parameter bag
1 parent b1098d9 commit 13ea687
Copy full SHA for 13ea687

File tree

2 files changed

+43
-2
lines changed
Filter options

2 files changed

+43
-2
lines changed

‎src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/ContainerBuilder.php
+16-2Lines changed: 16 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,29 @@ 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 = new ParameterBag($this->resolveEnvPlaceholders($bag->all(), true));
579+
$this->envPlaceholders = $bag->getEnvPlaceholders();
580+
$this->parameterBag = $bag = new ParameterBag($this->resolveEnvPlaceholders($this->parameterBag->all()));
581+
}
567582

568583
$compiler->compile($this);
569584

@@ -577,7 +592,6 @@ public function compile()
577592
}
578593

579594
$this->extensionConfigs = array();
580-
$bag = $this->getParameterBag();
581595

582596
parent::compile();
583597

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,33 @@ 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('env(FOO)', 'Foo');
557+
$container->setParameter('bar', '%% %env(DUMMY_ENV_VAR)%');
558+
$container->setParameter('foo', '%env(FOO)%');
559+
$container->compile(true);
560+
561+
$this->assertSame('% du%%y', $container->getParameter('bar'));
562+
$this->assertSame('Foo', $container->getParameter('foo'));
563+
564+
unset($_ENV['DUMMY_ENV_VAR']);
565+
}
566+
567+
/**
568+
* @expectedException \Symfony\Component\DependencyInjection\Exception\EnvNotFoundException
569+
* @expectedExceptionMessage Environment variable not found: "FOO".
570+
*/
571+
public function testCompileWithResolveMissingEnv()
572+
{
573+
$container = new ContainerBuilder();
574+
$container->setParameter('foo', '%env(FOO)%');
575+
$container->compile(true);
576+
}
577+
551578
/**
552579
* @expectedException \LogicException
553580
*/

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.