Skip to content

[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

Merged
merged 1 commit into from
Dec 14, 2016

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Aug 25, 2016

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

Choose a reason for hiding this comment

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

turned

@nicolas-grekas nicolas-grekas force-pushed the env-params branch 2 times, most recently from d816802 to 9f7017e Compare August 25, 2016 12:39
be reconfigured dynamically even after being compiled.

.. versionadded:: 3.2
``env(...)`` parameters were introduced in Symfony 3.2.
Copy link
Member

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

@wouterj
Copy link
Member

wouterj commented Aug 28, 2016

status: needs work

@javiereguiluz javiereguiluz changed the title [DI] Document %env(...)% dynamic parameters [WCM][DependencyInjection] Document %env(...)% dynamic parameters Aug 29, 2016
fabpot added a commit to symfony/symfony 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
symfony-splitter pushed a commit to symfony/http-kernel 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
symfony-splitter pushed a commit to symfony/dependency-injection 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
@weaverryan weaverryan changed the title [WCM][DependencyInjection] Document %env(...)% dynamic parameters [DependencyInjection] Document %env(...)% dynamic parameters Sep 18, 2016
Copy link
Member

@weaverryan weaverryan left a 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:
Copy link
Member

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:
Copy link
Member

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.
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

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

environment

@weaverryan
Copy link
Member

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``
Copy link
Member

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 thedatabase.user` parameter).

@nicolas-grekas
Copy link
Member Author

Comments addressed, thanks for the review.

@wouterj
Copy link
Member

wouterj commented Oct 1, 2016

👍

#app/config/parameters.yml

parameters:
env(DATABASE_HOST): localhost
Copy link
Contributor

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"

Copy link
Member

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

Copy link
Contributor

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

Copy link
Member

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!

Copy link
Contributor

@mickadoo mickadoo Oct 13, 2016

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!

Copy link
Member

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.


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
Copy link
Member

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
Copy link
Member

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

Copy link
Member

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" ?>
Copy link
Member

Choose a reason for hiding this comment

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

missing filename comment

Copy link
Member

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

Copy link
Member Author

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?

Copy link
Member

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?

Copy link
Member

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.

Copy link
Member Author

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
Copy link
Member

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>
Copy link
Member

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.

@wouterj
Copy link
Member

wouterj commented Oct 16, 2016

status: needs work

ping @nicolas-grekas

@nicolas-grekas
Copy link
Member Author

comments addressed
Status: needs review

.. 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
Copy link
Member

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.

Copy link
Member

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?

Copy link
Member

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.

Copy link
Member Author

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?


.. configuration-block::

.. code-block:: apache
Copy link
Member Author

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?

Copy link
Member

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)

Copy link
Member

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:

apache-sphinx

Copy link
Member

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'

Copy link
Member

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.

@wouterj wouterj added this to the 3.2 milestone Nov 9, 2016
@wouterj
Copy link
Member

wouterj commented Nov 9, 2016

👍
status: reviewed

Note to merger: Please fix the conf.py configuration to suppor the new configuration block formats.

@wouterj
Copy link
Member

wouterj commented Nov 19, 2016

ping @javiereguiluz @weaverryan @xabbuh please review & vote

)
));

You can also give the ``env()`` parameters a default value: the default value
Copy link
Member

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 ...

Copy link
Member

@javiereguiluz javiereguiluz left a comment

Choose a reason for hiding this comment

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

👍

@xabbuh xabbuh changed the base branch from master to 3.2 December 8, 2016 20:26
@xabbuh
Copy link
Member

xabbuh commented Dec 8, 2016

👍

@xabbuh
Copy link
Member

xabbuh commented Dec 14, 2016

Thank you Nicolas.

@xabbuh xabbuh merged commit 083da46 into symfony:3.2 Dec 14, 2016
xabbuh added a commit that referenced this pull request Dec 14, 2016
…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
@nicolas-grekas nicolas-grekas deleted the env-params branch July 1, 2018 20:44
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.

10 participants