Skip to content
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
Closed

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

wants to merge 1 commit into from

Conversation

lavoiesl
Copy link
Contributor

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
Copy link
Contributor Author

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

@mvrhov
Copy link

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
Copy link
Contributor Author

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

@@ -25,6 +25,7 @@
class ParameterBag implements ParameterBagInterface
{
protected $parameters = array();
protected $environment_map = array();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Property names should use camel case

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@fabpot
Copy link
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
…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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants