New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP][DependencyInjection] Issue #7555 - Resolve environment variables at runtime #10138

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
4 participants
@lavoiesl
Contributor

lavoiesl commented Jan 25, 2014

Q A
Bug fix? no
New feature? yes
BC breaks? minimal
Deprecations? no
Tests pass? yes
Fixed tickets #7555
License MIT
Doc PR TODO
  • basic tests
  • performance tests
  • Deep tests
  • documentation

This allows to set a parameter environment_map that contains a map [$parameter => $env_var]. This allows to call ParameterBag::resolveEnvironmentMap even after the bag was initially resolved and frozen. As mentioned by @stof, dependent parameter can not be resolved anymore, so it is forbidden to reference a parameter in the environment map elsewhere. However, resolveEnvironmentMap will use resolveValue, so it is possible to reference another parameter in the enviromnent variable.

HttpKernel now calls ParameterBag::resolveEnvironmentMap after the Container was fully loaded.

I am not entirely sure of the implication of this method with the caching mechanism, the cache warmers, etc. I could use some help from someone with a better understanding of the whole thing.

The only BC break is that environment_map is not a valid parameter name anymore, it is reserved.

@lavoiesl

This comment has been minimized.

Show comment
Hide comment
@lavoiesl

lavoiesl Jan 25, 2014

Contributor

So far, the issues in the build do not seem to be linked to this PR.

Contributor

lavoiesl commented Jan 25, 2014

So far, the issues in the build do not seem to be linked to this PR.

@mvrhov

This comment has been minimized.

Show comment
Hide comment
@mvrhov

mvrhov Jan 26, 2014

Contributor

Can I suggest an improvement.. This run-time resolver could also resolve the parameters set via the newly introduced expression engine.

Contributor

mvrhov commented Jan 26, 2014

Can I suggest an improvement.. This run-time resolver could also resolve the parameters set via the newly introduced expression engine.

@lavoiesl

This comment has been minimized.

Show comment
Hide comment
@lavoiesl

lavoiesl Jan 26, 2014

Contributor

This could indeed be considered, but this would be a separate PR isn’t it ?

Contributor

lavoiesl commented Jan 26, 2014

This could indeed be considered, but this would be a separate PR isn’t it ?

Show outdated Hide outdated src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php
@@ -25,6 +25,7 @@
class ParameterBag implements ParameterBagInterface
{
protected $parameters = array();
protected $environment_map = array();

This comment has been minimized.

@stof

stof Jan 26, 2014

Member

Property names should use camel case

@stof

stof Jan 26, 2014

Member

Property names should use camel case

This comment has been minimized.

@lavoiesl

lavoiesl Jan 26, 2014

Contributor

Thanks

@lavoiesl

lavoiesl Jan 26, 2014

Contributor

Thanks

@fabpot

This comment has been minimized.

Show comment
Hide comment
@fabpot

fabpot Nov 12, 2015

Member

Closing as it does not fix the problem in all cases (see the referenced other PR I created which also failed).

Member

fabpot commented Nov 12, 2015

Closing as it does not fix the problem in all cases (see the referenced other PR I created which also failed).

@fabpot fabpot closed this Nov 12, 2015

fabpot added a commit that referenced this pull request Sep 15, 2016

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment