Skip to content

Commit cd6f496

Browse files
committed
feature #19681 [DI] Allow injecting ENV parameters at runtime using %env(MY_ENV_VAR)% (nicolas-grekas)
This PR was merged into the 3.2-dev branch. Discussion ---------- [DI] Allow injecting ENV parameters at runtime using %env(MY_ENV_VAR)% | Q | A | ------------- | --- | Branch? | master | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #10138, #7555, #16403, #18155 | License | MIT | Doc PR | symfony/symfony-docs#6918 This is an alternative approach to #18155 for injecting env vars into container configurations. With this PR, anywhere parameters are allowed, one can use `%env(ENV_VAR)%` to inject a dynamic env var. Additionally, if one sets a value to such parameters in e.g. the `parameter.yml` file (`env(ENV_VAR): foo`), this value will be used as a default value when the env var is not defined. If no default value is specified, an `EnvVarNotFoundException` will be thrown at runtime. Unlike previous attempts that also used parameters (#16403), the implementation is compatible with DI extensions: before being dumped, env vars are resolved to uniquely identifiable string placeholders that can get through DI extensions manipulations. When dumped, these unique placeholders are replaced by dynamic calls to a getEnv method.. ping @magnusnordlander @dzuelke @fabpot Commits ------- bac2132 [DI] Allow injecting ENV parameters at runtime using %env(MY_ENV_VAR)% syntax
2 parents d9374de + c891923 commit cd6f496

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

Kernel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\Component\DependencyInjection\ContainerInterface;
1717
use Symfony\Component\DependencyInjection\ContainerBuilder;
1818
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
19-
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
2019
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2120
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
2221
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
@@ -611,7 +610,8 @@ protected function prepareContainer(ContainerBuilder $container)
611610
*/
612611
protected function getContainerBuilder()
613612
{
614-
$container = new ContainerBuilder(new ParameterBag($this->getKernelParameters()));
613+
$container = new ContainerBuilder();
614+
$container->getParameterBag()->add($this->getKernelParameters());
615615

616616
if (class_exists('ProxyManager\Configuration') && class_exists('Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator')) {
617617
$container->setProxyInstantiator(new RuntimeInstantiator());

0 commit comments

Comments
 (0)