-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[DependencyInjection] Document %env(...)% dynamic parameters #6918
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
Conversation
8f1d491
to
0eae3eb
Compare
$container->setParameter('env(DATABASE_HOST)', 'localhost'); | ||
|
||
Unlike ``SYMFONY__`` prefixed environment variables, ``%env(...)%`` parameters are | ||
truned into run-time environment variable reads, so that dumped containers can |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
turned
d816802
to
9f7017e
Compare
be reconfigured dynamically even after being compiled. | ||
|
||
.. versionadded:: 3.2 | ||
``env(...)`` parameters were introduced in Symfony 3.2. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please put the versionadded directive after the section header, instead of at the end
status: needs work |
9f7017e
to
3021c4f
Compare
…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
…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
…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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Status: Reviewed
Setting this as reviewed because all of my comments are very minor!
|
||
.. code-block:: yaml | ||
|
||
parameters: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add #app/config/parameters.yml
above this - it's probably the file where they will define the default value
) | ||
)); | ||
|
||
``env(...)`` parameters can have default values be set and used whenever their | ||
corresponding environment variables are not found: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also give the ``env(...)`` parameters a default value: the default value will be used whenever
the corresponding environment variable is *not* found:
.. note:: | ||
|
||
All popular web servers support setting environment variables. Read their | ||
documentation to know how. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... to find out how.
.. note:: | ||
|
||
You can also define the default value of any existing parameters using | ||
special environement variables named after their corresponding parameter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
environment
Status: Reviewed Just needs a few minor changes. |
You can also define the default value of any existing parameters using | ||
special environement variables named after their corresponding parameter | ||
prefixed with ``SYMFONY__`` after replacing dots by double underscores | ||
(e.g. ``SYMFONY__FOO__BAR`` to set the default value of the ``foo.bar`` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's keep using a real example here: (e.g.
SYMFONY__DATABASE__USERto set the default value of the
database.user` parameter).
3021c4f
to
68e9b49
Compare
Comments addressed, thanks for the review. |
👍 |
#app/config/parameters.yml | ||
|
||
parameters: | ||
env(DATABASE_HOST): localhost |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this functionality part of the symfony/symfony#19681 PR? I've tried something similar with:
database_driver: 'env(DATABASE_DRIVER): pdo_mysql'
but it doesn't use the default and just complains that:
"The given 'driver' env(DATABASE_DRIVER): pdo_mysql is unknown"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mickadoo your code is not what is documented here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stof I just thought I'd try it out, didn't work though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's documented here is:
parameters:
database_driver: '%env(DATABASE_DRIVER)%'
env(DATABASE_DRIVER): pdo_mysql
Which, as you can see, is quite different from the format you tested. If there is something we can do to improve the description of the feature, please tell us!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wouterj Ah, I get it now. Sorry about that!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem @mickadoo thanks for test-casing this documentation proposal.
I think we should include the database_driver: '%env(DATABASE_DRIVER)%'
line in this example to avoid confusing for other readers.
68e9b49
to
27399c0
Compare
|
||
You can now reference these parameters wherever you need them. | ||
For example, if you want to use the value of the ``DATABASE_HOST`` environment | ||
variable into you service container configuration, you can reference it using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[...] variable in your service container configuration [...]
|
||
.. code-block:: yaml | ||
|
||
#app/config/parameters.yml |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing space between comment and "app"
.. code-block:: yaml | ||
|
||
#app/config/parameters.yml | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blank line should be removed
|
||
.. code-block:: xml | ||
|
||
<?xml version="1.0" encoding="UTF-8" ?> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing filename comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same for the PHP config example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have an example of such filenames for xml & php?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually simply replace the file extension. So here we would have app/config/parameters.yml
, app/config/parameters.xml
, and app/config/parameters.php
. By the way, can you also add similar comments in the example above to, but using config.yml
, config.xml
, and config.php
there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah well, the previous example is not new. We should add the comments there in older branches too and can probably better do that in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it, fixed
|
||
$container->setParameter('env(DATABASE_HOST)', 'localhost'); | ||
|
||
If you're using Apache, environment variables can be set e.g. using the following |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everywhere else we give examples for Nginx too IIRC. I think we should do the same here.
<Directory "/path/to/symfony_2_app/web"> | ||
AllowOverride All | ||
Allow from All | ||
</Directory> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also add a placeholder comment to indicate that this config is not a complete working examples, but only shows how to configure environment variables for a virtual host.
status: needs work ping @nicolas-grekas |
27399c0
to
21d6a75
Compare
comments addressed |
.. code-block:: terminal | ||
You can reference environment variables by using special parameters named after | ||
the variables you want to use enclosed between ``env()``. Their actual values | ||
will be resolved at runtime, so that dumped containers can be reconfigured |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A quick question about this:
Their actual values will be resolved at runtime [...]
Are they resolved at runtime every time they are used ... or are they resolved just the first time they are used and then Symfony reuses the cached value? I say this because of the code of the getEnv() method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per request they are resolved once. But if you change the value, during the next request you will see the new value (again resolved only once during this request). Should we make that more clear somehow?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @xabbuh. I wouldn't add any notice about this because that behavior is exactly what we should expect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added (once per request)
, good enough?
21d6a75
to
083da46
Compare
|
||
.. configuration-block:: | ||
|
||
.. code-block:: apache |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looking at the tests, apache
here makes sphinx fail, any clue why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apache has no label configured. Please edit the _build/conf.py
file and add: 'apache': 'Apache'
(same for the other formats in this block). (and also ping javier that the symfony.com config has to be updated)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wouterj thanks for the heads up. It will be merged in symfony.com soon:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@javiereguiluz please note that you also have to add 'nginx': 'NGINX'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wouterj thanks! It's fixed now. I opted for Nginx
as the human-readable name instead of NGINX
. The official website mixes nginx
, Nginx
and NGINX
... but the Wikipedia always uses Nginx
.
👍 Note to merger: Please fix the |
ping @javiereguiluz @weaverryan @xabbuh please review & vote |
) | ||
)); | ||
|
||
You can also give the ``env()`` parameters a default value: the default value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... a default value: the default value will be used whenever ...
-->
... a default value used whenever ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
👍 |
Thank you Nicolas. |
…eters (nicolas-grekas) This PR was merged into the 3.2 branch. Discussion ---------- [DependencyInjection] Document %env(...)% dynamic parameters Ref. symfony/symfony#19681 Commits ------- 083da46 [DI] Document %env()% dynamic parameters
Ref. symfony/symfony#19681