-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Add docs for the Dotenv component #7350
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
|
||
.. code-block:: php | ||
|
||
$dbUser = getenv('DB_USER); |
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 single quote 'DB_USER'
|
||
Symfony Env never overwrites existing environment variables. | ||
|
||
You should never store a ``.env`` file is your code repository as it might |
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.
Typo. Should be "in your code"
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.
fixed
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 for providing the docs for this component. Everything is well explained and easy to understand, but I miss two things ... not for this component itself, but for when using it inside Symfony:
- It's not clear enough for me how and when to use this. You mention that this is not for production. So should I create this only for
dev
? But then, why define it indev
if I already haveapp/config/config_dev.yml
which works nicely without env vars? In other words, I don't know why I should "complicate" things indev
with env vars ... which probably are only needed inprod
. - Is there a seamless integration with Symfony? If I define a
.env
file, could I use those vars with%env()%
in my files automatically or should I do something?
environment variables (as recommended for `twelve-factor applications | ||
<http://www.12factor.net/>`_. Using an `.env` file to store those environment | ||
variables eases development and CI management by keeping them in one "standard" | ||
place and agnostic of the technology stack you are using (Nginx vs PHP build-in |
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.
build-in
-> built-in
?
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.
fixed
|
||
.. note:: | ||
|
||
PHP has a lot of different implementation of this "pattern". This |
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.
implementation
-> implementations
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.
done
|
||
Load an ``.env`` file in your PHP application via ``Dotenv::load()``: | ||
|
||
.. code-block:: 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.
In docs we are not allowed to use this php
code block and we must the ::
trick.
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.
fixed
Symfony Env never overwrites existing environment variables. | ||
|
||
You should never store a ``.env`` file is your code repository as it might | ||
contains sensitive information; creates a ``.env.dist`` file with sensible |
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.
creates
-> create
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.
fixed
contains sensitive information; creates a ``.env.dist`` file with sensible | ||
defaults instead. | ||
|
||
Symfony Env should only be used in development/testing/staging environments. |
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.
This recommendation contradicts a bit the previous one. If this file should only be used for non-production environments, then there's no problem storing this file in the repo because the credentials will be different. If this is true, then we don't need .env.dist
and we can simplify things a lot.
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 db connection settings might still be sensitive information for testing/staging environments.
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.
.env
contains data that should be different for each dev, so having it in Git would not make sense.
.. note:: | ||
|
||
PHP has a lot of different implementation of this "pattern". This | ||
implementation goal is to replicate what ``source .env`` would do. So, it |
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.
This implementation's goal
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.
fixed
|
||
PHP has a lot of different implementation of this "pattern". This | ||
implementation goal is to replicate what ``source .env`` would do. So, it | ||
tries to be as similar as possible with the default shells behavior, and |
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.
shell's behavior
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.
although plural is probably correct in this case too.
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.
ok
Symfony Env never overwrites existing environment variables. | ||
|
||
You should never store a ``.env`` file is your code repository as it might | ||
contains sensitive information; creates a ``.env.dist`` file with sensible |
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.
it might contain
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.
fixed
@javiereguiluz Using a Regarding the integration with Symfony, it's going to be part of the whole new "bootstrap" experience I'm working on. More on that later. |
38a171f
to
bb181d5
Compare
|
||
You can install the component in 2 different ways: | ||
|
||
* :doc:`Install it via Composer </components/using_components>` (``symfony/env`` on `Packagist`_); |
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 .. _Packagist: https://packagist.org/packages/symfony/env
at the end of this document
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.
fixed
================= | ||
|
||
The Env Component parses ``.env`` files to make environment variables | ||
stored in them accessible via ``getenv()``, ``$_ENV``, or ``$_SERVER``. |
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.
the Oxford comma should be removed
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.
arf, I really don't like this but ok, done.
|
||
Sensitive information and environment-dependent settings should be defined as | ||
environment variables (as recommended for `twelve-factor applications | ||
<http://www.12factor.net/>`_. Using an `.env` file to store those environment |
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 do not inline the referenced URLs, but add them add the end of the document.
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.
double backticks needed to enclose the filename
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.
done x 2
|
||
Given the following ``.env`` file content: | ||
|
||
.. code-block:: bash |
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.
Don't we have a better code block type we could use here?
Given the following ``.env`` file content: | ||
|
||
.. code-block:: bash | ||
|
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 add a file comment too:
# .env
DB_USER=root
DB_PASS=pass
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.
done
Comments addressed |
👍 Status: Reviewed |
|
||
First, require Symfony Dotenv via Composer: | ||
|
||
.. code-block:: bash |
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.
Don't we use terminal
now?
|
||
Symfony Env never overwrites existing environment variables. | ||
|
||
You should never store a ``.env`` file in your code repository as it might |
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.
store an .env
file
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, it's store a dot env file :)
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.
Right, thanks!
Symfony Env never overwrites existing environment variables. | ||
|
||
You should never store a ``.env`` file in your code repository as it might | ||
contain sensitive information; create a ``.env.dist`` file with sensible |
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.
an
here too
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 here
This PR was merged into the 3.3-dev branch. Discussion ---------- Add a new Dotenv component | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | symfony/symfony-docs#7350 This introduces a new Dotnv Component that manages `.env` files. Read the referenced doc PR above for more information about usage: But here, I want to explain the rationale behind creating such a component instead of reusing an existing one. * First, this version only implements what you can do in a "real" bash shell script (which is what a `.env` really is): so **no value validation** for instance (and anyway, an env var value is always a string). That's important as in production, we should use real env variables, and we don't have validation for them there; * It allows to only parse a file without populating the env variables (we have 3 stages: `load` `parse` and `populate`); * Strict implementation of what you can do in a `.env` file, same behavior as bash ($VAR and ${VAR} are supported for instance, executing commands as well); * Great error messages: I spent a lot of time being sure that error reporting is top notch; * Clean, simple, and straightforward code (small public API); * It only does `.env` management, there is no uneeded abstractions like being able to add an env variable directly (just use `putenv`); There are some unimplemented features as I don't think they are needed and would increase the complexity of the code: several concatenated strings `FOO='foo'"bar"` for instance. Commits ------- 5a6be8a [Dotenv] added the component
This one is ready and the related PR on Symfony has been merged now. |
|
||
Sensitive information and environment-dependent settings should be defined as | ||
environment variables (as recommended for `twelve-factor applications`_. Using | ||
an ``.env`` file to store those environment variables eases development and CI |
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.
Then it's a
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.
touché
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.
fixed
👍 |
PHP has a lot of different implementations of this "pattern". This | ||
implementation's goal is to replicate what ``source .env`` would do. So, it | ||
tries to be as similar as possible with the default shell's behavior, and | ||
does not do anything not possible via ``source`` (like value validation). |
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.
Double negation is hard to read
----- | ||
|
||
Sensitive information and environment-dependent settings should be defined as | ||
environment variables (as recommended for `twelve-factor applications`_. 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.
A closing parenthesis is missing on this line.
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.
fixed
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'm missing a brief explanation about the 3 stages and their purposes: load, parse and populate. Thanks!
DB_USER=root | ||
DB_PASS=${DB_USER}pass # Include the user as a password prefix | ||
|
||
Embed commands via ``$()`` (not supported on Windows): |
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.
Windows 10 added support for a Bash shell, so is this is still valid?
Note that I'm not using Windows atm, nor Windows 10 😅
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.
Windows is not able to do it out of the box. But you can use it through an emulator like cmder or ConEmu.
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.
Wait ConEmu support this? Cool :)
Windows 10 added bash support, but using an Ubuntu subsystem 😵 so it will work as expected then.
environment variables (as recommended for `twelve-factor applications`_. Using | ||
a ``.env`` file to store those environment variables eases development and CI | ||
management by keeping them in one "standard" place and agnostic of the | ||
technology stack you are using (Nginx vs PHP built-in server for instance). |
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.
nginx
instead of 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.
I think we use Nginx everywhere else.
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.
indeed, Nginx is used elsewhere
|
||
The Dotenv Component parses ``.env`` files to make environment variables | ||
stored in them accessible via ``getenv()``, ``$_ENV`` or ``$_SERVER``. | ||
|
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.
Shouldn't you add a versionadded
section (like in cache and workflow?)
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.
done
@javiereguiluz The only entry-point should be |
defaults instead. | ||
|
||
Symfony Env should only be used in development/testing/staging environments. | ||
For production environments, use "real" environment variables. |
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 do Symfony Env stands for? Does it refer to this component?
Does it mean we should not use this component in production environment?
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.
Fixed, should have been Symfony Dotenv
.
Indeed, in production, you should define "real" env vars.
Anything blocking the merge (besides time :))? |
I hope it'll bring Symfony closer to The Twelve-Factor App (esp. https://12factor.net/config), which will be good news for Docker users. Currently the parameters.yml situation is a bit of a pain (probably improved since dynamic |
Thank you @fabpot. |
This PR was merged into the master branch. Discussion ---------- Add docs for the Dotenv component See symfony/symfony#21234 Commits ------- 03fda49 added docs for the env component
See symfony/symfony#21234