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 1a2b1cb

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

File tree

2 files changed

+50
-2
lines changed
Filter options

2 files changed

+50
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/ContainerBuilder.php
+23-2Lines changed: 23 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,16 +556,37 @@ public function prependExtensionConfig($name, array $config)
555556
* * Parameter values are resolved;
556557
* * The parameter bag is frozen;
557558
* * Extension loading is disabled.
559+
*
560+
* @param bool $resolveEnvPlaceholders Whether %env()% parameters should be resolved using the current
561+
* env vars or be replaced by uniquely identifiable placeholders
558562
*/
559-
public function compile()
563+
public function compile(/*$resolveEnvPlaceholders = false*/)
560564
{
565+
if (1 <= func_num_args()) {
566+
$resolveEnvPlaceholders = func_get_arg(0);
567+
} else {
568+
if (__CLASS__ !== static::class) {
569+
$r = new \ReflectionMethod($this, __FUNCTION__);
570+
if (__CLASS__ !== $r->getDeclaringClass()->getName() && (1 > $r->getNumberOfParameters() || 'resolveEnvPlaceholders' !== $r->getParameters()[0]->name)) {
571+
@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);
572+
}
573+
}
574+
$resolveEnvPlaceholders = false;
575+
}
561576
$compiler = $this->getCompiler();
562577

563578
if ($this->trackResources) {
564579
foreach ($compiler->getPassConfig()->getPasses() as $pass) {
565580
$this->addObjectResource($pass);
566581
}
567582
}
583+
$bag = $this->getParameterBag();
584+
585+
if ($resolveEnvPlaceholders && $bag instanceof EnvPlaceholderParameterBag) {
586+
$this->parameterBag = new ParameterBag($this->resolveEnvPlaceholders($bag->all(), true));
587+
$this->envPlaceholders = $bag->getEnvPlaceholders();
588+
$this->parameterBag = $bag = new ParameterBag($this->resolveEnvPlaceholders($this->parameterBag->all()));
589+
}
568590

569591
$compiler->compile($this);
570592

@@ -578,7 +600,6 @@ public function compile()
578600
}
579601

580602
$this->extensionConfigs = array();
581-
$bag = $this->getParameterBag();
582603

583604
parent::compile();
584605

‎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.