Permalink
Browse files

feature #21460 [DI] ContainerBuilder::compile() can optionally resolv…

…e 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
  • Loading branch information...
2 parents c43b85e + a3fd512 commit 10c3fc2081b4e859e379c7fae7d1a1d0a10356d0 @fabpot fabpot committed Feb 9, 2017
@@ -25,6 +25,7 @@
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
+use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\Config\Resource\ClassExistenceResource;
use Symfony\Component\Config\Resource\DirectoryResource;
use Symfony\Component\Config\Resource\FileExistenceResource;
@@ -658,16 +659,39 @@ public function prependExtensionConfig($name, array $config)
* * Parameter values are resolved;
* * The parameter bag is frozen;
* * Extension loading is disabled.
+ *
+ * @param bool $resolveEnvPlaceholders Whether %env()% parameters should be resolved using the current
+ * env vars or be replaced by uniquely identifiable placeholders.
+ * Set to "true" when you want to use the current ContainerBuilder
+ * directly, keep to "false" when the container is dumped instead.
*/
- public function compile()
+ public function compile(/*$resolveEnvPlaceholders = false*/)
{
+ if (1 <= func_num_args()) {
+ $resolveEnvPlaceholders = func_get_arg(0);
+ } else {
+ if (__CLASS__ !== static::class) {
+ $r = new \ReflectionMethod($this, __FUNCTION__);
+ if (__CLASS__ !== $r->getDeclaringClass()->getName() && (1 > $r->getNumberOfParameters() || 'resolveEnvPlaceholders' !== $r->getParameters()[0]->name)) {
+ @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);
+ }
+ }
+ $resolveEnvPlaceholders = false;
+ }
$compiler = $this->getCompiler();
if ($this->trackResources) {
foreach ($compiler->getPassConfig()->getPasses() as $pass) {
$this->addObjectResource($pass);
}
}
+ $bag = $this->getParameterBag();
+
+ if ($resolveEnvPlaceholders && $bag instanceof EnvPlaceholderParameterBag) {
+ $this->parameterBag = new ParameterBag($this->resolveEnvPlaceholders($bag->all(), true));
+ $this->envPlaceholders = $bag->getEnvPlaceholders();
+ $this->parameterBag = $bag = new ParameterBag($this->resolveEnvPlaceholders($this->parameterBag->all()));
+ }
$compiler->compile($this);
@@ -681,7 +705,6 @@ public function compile()
}
$this->extensionConfigs = array();
- $bag = $this->getParameterBag();
parent::compile();
@@ -550,6 +550,33 @@ public function testResolveEnvValues()
unset($_ENV['DUMMY_ENV_VAR']);
}
+ public function testCompileWithResolveEnv()
+ {
+ $_ENV['DUMMY_ENV_VAR'] = 'du%%y';
+
+ $container = new ContainerBuilder();
+ $container->setParameter('env(FOO)', 'Foo');
+ $container->setParameter('bar', '%% %env(DUMMY_ENV_VAR)%');
+ $container->setParameter('foo', '%env(FOO)%');
+ $container->compile(true);
+
+ $this->assertSame('% du%%y', $container->getParameter('bar'));
+ $this->assertSame('Foo', $container->getParameter('foo'));
+
+ unset($_ENV['DUMMY_ENV_VAR']);
+ }
+
+ /**
+ * @expectedException \Symfony\Component\DependencyInjection\Exception\EnvNotFoundException
+ * @expectedExceptionMessage Environment variable not found: "FOO".
+ */
+ public function testCompileWithResolveMissingEnv()
+ {
+ $container = new ContainerBuilder();
+ $container->setParameter('foo', '%env(FOO)%');
+ $container->compile(true);
+ }
+
/**
* @expectedException \LogicException
*/

0 comments on commit 10c3fc2

Please sign in to comment.