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 10c3fc2

Browse filesBrowse files
committed
feature #21460 [DI] ContainerBuilder::compile() can optionally resolve env vars in parameter bag (nicolas-grekas)
This PR was merged into the 3.3-dev branch. Discussion ---------- [DI] ContainerBuilder::compile() can optionally resolve env vars in parameter bag | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #21420 | License | MIT | Doc PR | - Here is a new feature allowing one to do `$container->compile(true)` and get the env vars resolved using the current env. Commits ------- a3fd512 [DI] ContainerBuilder::compile() can optionally resolve env vars in parameter bag
2 parents c43b85e + a3fd512 commit 10c3fc2
Copy full SHA for 10c3fc2

File tree

2 files changed

+52
-2
lines changed
Filter options

2 files changed

+52
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/ContainerBuilder.php
+25-2Lines changed: 25 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;
@@ -658,16 +659,39 @@ public function prependExtensionConfig($name, array $config)
658659
* * Parameter values are resolved;
659660
* * The parameter bag is frozen;
660661
* * Extension loading is disabled.
662+
*
663+
* @param bool $resolveEnvPlaceholders Whether %env()% parameters should be resolved using the current
664+
* env vars or be replaced by uniquely identifiable placeholders.
665+
* Set to "true" when you want to use the current ContainerBuilder
666+
* directly, keep to "false" when the container is dumped instead.
661667
*/
662-
public function compile()
668+
public function compile(/*$resolveEnvPlaceholders = false*/)
663669
{
670+
if (1 <= func_num_args()) {
671+
$resolveEnvPlaceholders = func_get_arg(0);
672+
} else {
673+
if (__CLASS__ !== static::class) {
674+
$r = new \ReflectionMethod($this, __FUNCTION__);
675+
if (__CLASS__ !== $r->getDeclaringClass()->getName() && (1 > $r->getNumberOfParameters() || 'resolveEnvPlaceholders' !== $r->getParameters()[0]->name)) {
676+
@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);
677+
}
678+
}
679+
$resolveEnvPlaceholders = false;
680+
}
664681
$compiler = $this->getCompiler();
665682

666683
if ($this->trackResources) {
667684
foreach ($compiler->getPassConfig()->getPasses() as $pass) {
668685
$this->addObjectResource($pass);
669686
}
670687
}
688+
$bag = $this->getParameterBag();
689+
690+
if ($resolveEnvPlaceholders && $bag instanceof EnvPlaceholderParameterBag) {
691+
$this->parameterBag = new ParameterBag($this->resolveEnvPlaceholders($bag->all(), true));
692+
$this->envPlaceholders = $bag->getEnvPlaceholders();
693+
$this->parameterBag = $bag = new ParameterBag($this->resolveEnvPlaceholders($this->parameterBag->all()));
694+
}
671695

672696
$compiler->compile($this);
673697

@@ -681,7 +705,6 @@ public function compile()
681705
}
682706

683707
$this->extensionConfigs = array();
684-
$bag = $this->getParameterBag();
685708

686709
parent::compile();
687710

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