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 a735b0e

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

File tree

Expand file treeCollapse file tree

2 files changed

+50
-2
lines changed
Filter options
Expand file treeCollapse file tree

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\ClassExistenceResource;
2930
use Symfony\Component\Config\Resource\DirectoryResource;
3031
use Symfony\Component\Config\Resource\FileExistenceResource;
@@ -657,16 +658,37 @@ public function prependExtensionConfig($name, array $config)
657658
* * Parameter values are resolved;
658659
* * The parameter bag is frozen;
659660
* * Extension loading is disabled.
661+
*
662+
* @param bool $resolveEnvPlaceholders Whether %env()% parameters should be resolved using the current
663+
* env vars or be replaced by uniquely identifiable placeholders
660664
*/
661-
public function compile()
665+
public function compile(/*$resolveEnvPlaceholders = false*/)
662666
{
667+
if (1 <= func_num_args()) {
668+
$resolveEnvPlaceholders = func_get_arg(0);
669+
} else {
670+
if (__CLASS__ !== static::class) {
671+
$r = new \ReflectionMethod($this, __FUNCTION__);
672+
if (__CLASS__ !== $r->getDeclaringClass()->getName() && (1 > $r->getNumberOfParameters() || 'resolveEnvPlaceholders' !== $r->getParameters()[0]->name)) {
673+
@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);
674+
}
675+
}
676+
$resolveEnvPlaceholders = false;
677+
}
663678
$compiler = $this->getCompiler();
664679

665680
if ($this->trackResources) {
666681
foreach ($compiler->getPassConfig()->getPasses() as $pass) {
667682
$this->addObjectResource($pass);
668683
}
669684
}
685+
$bag = $this->getParameterBag();
686+
687+
if ($resolveEnvPlaceholders && $bag instanceof EnvPlaceholderParameterBag) {
688+
$this->parameterBag = new ParameterBag($this->resolveEnvPlaceholders($bag->all(), true));
689+
$this->envPlaceholders = $bag->getEnvPlaceholders();
690+
$this->parameterBag = $bag = new ParameterBag($this->resolveEnvPlaceholders($this->parameterBag->all()));
691+
}
670692

671693
$compiler->compile($this);
672694

@@ -680,7 +702,6 @@ public function compile()
680702
}
681703

682704
$this->extensionConfigs = array();
683-
$bag = $this->getParameterBag();
684705

685706
parent::compile();
686707

‎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
@@ -550,6 +550,33 @@ public function testResolveEnvValues()
550550
unset($_ENV['DUMMY_ENV_VAR']);
551551
}
552552

553+
public function testCompileWithResolveEnv()
554+
{
555+
$_ENV['DUMMY_ENV_VAR'] = 'du%%y';
556+
557+
$container = new ContainerBuilder();
558+
$container->setParameter('env(FOO)', 'Foo');
559+
$container->setParameter('bar', '%% %env(DUMMY_ENV_VAR)%');
560+
$container->setParameter('foo', '%env(FOO)%');
561+
$container->compile(true);
562+
563+
$this->assertSame('% du%%y', $container->getParameter('bar'));
564+
$this->assertSame('Foo', $container->getParameter('foo'));
565+
566+
unset($_ENV['DUMMY_ENV_VAR']);
567+
}
568+
569+
/**
570+
* @expectedException \Symfony\Component\DependencyInjection\Exception\EnvNotFoundException
571+
* @expectedExceptionMessage Environment variable not found: "FOO".
572+
*/
573+
public function testCompileWithResolveMissingEnv()
574+
{
575+
$container = new ContainerBuilder();
576+
$container->setParameter('foo', '%env(FOO)%');
577+
$container->compile(true);
578+
}
579+
553580
/**
554581
* @expectedException \LogicException
555582
*/

0 commit comments

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