Skip to content

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

Merged
merged 1 commit into from
Jan 24, 2017
Merged

Conversation

fabpot
Copy link
Member

@fabpot fabpot commented Jan 11, 2017


.. code-block:: php

$dbUser = getenv('DB_USER);
Copy link
Member

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

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"

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

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.

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:

  1. 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 in dev if I already have app/config/config_dev.yml which works nicely without env vars? In other words, I don't know why I should "complicate" things in dev with env vars ... which probably are only needed in prod.
  2. 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
Copy link
Member

Choose a reason for hiding this comment

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

build-in -> built-in ?

Copy link
Member Author

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

Choose a reason for hiding this comment

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

implementation -> implementations

Copy link
Member Author

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

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.

Copy link
Member Author

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

Choose a reason for hiding this comment

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

creates -> create

Copy link
Member Author

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

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.

Copy link
Contributor

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.

Copy link
Member Author

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

@jakzal jakzal Jan 11, 2017

Choose a reason for hiding this comment

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

This implementation's goal

Copy link
Member Author

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

Choose a reason for hiding this comment

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

shell's behavior

Copy link
Contributor

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.

Copy link
Member Author

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

Choose a reason for hiding this comment

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

it might contain

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

@fabpot
Copy link
Member Author

fabpot commented Jan 11, 2017

@javiereguiluz Using a .env file to define env vars in projects is a "standard" used by many frameworks. You don't need to use it of course as we have other mechanisms in Symfony, but this component allows Symfony projects to be more similar with projects written with other frameworks and languages.

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.

@fabpot fabpot force-pushed the env-component branch 2 times, most recently from 38a171f to bb181d5 Compare January 11, 2017 23:18
@xabbuh xabbuh added the On hold label Jan 12, 2017

You can install the component in 2 different ways:

* :doc:`Install it via Composer </components/using_components>` (``symfony/env`` on `Packagist`_);
Copy link
Member

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

Copy link
Member Author

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

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

Copy link
Member Author

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

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.

Copy link
Member

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

Copy link
Member Author

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

@xabbuh xabbuh Jan 12, 2017

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

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 add a file comment too:

# .env
DB_USER=root
DB_PASS=pass

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@fabpot
Copy link
Member Author

fabpot commented Jan 12, 2017

Comments addressed

@xabbuh
Copy link
Member

xabbuh commented Jan 12, 2017

👍

Status: Reviewed


First, require Symfony Dotenv via Composer:

.. code-block:: bash
Copy link
Contributor

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

Choose a reason for hiding this comment

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

store an .env file

Copy link
Member Author

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 :)

Copy link
Contributor

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

Choose a reason for hiding this comment

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

an here too

Copy link
Member Author

Choose a reason for hiding this comment

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

same here

fabpot added a commit to symfony/symfony that referenced this pull request Jan 12, 2017
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
@fabpot fabpot removed the On hold label Jan 12, 2017
@fabpot
Copy link
Member Author

fabpot commented Jan 12, 2017

This one is ready and the related PR on Symfony has been merged now.

@fabpot fabpot changed the title added docs for the env component Add docs for the env component Jan 12, 2017
@fabpot fabpot changed the title Add docs for the env component Add docs for the Dotenv component Jan 12, 2017

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

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 :)

Copy link
Member Author

Choose a reason for hiding this comment

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

touché

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

@HeahDude
Copy link
Contributor

👍

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

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

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

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.

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):
Copy link
Contributor

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 😅

Copy link
Contributor

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.

Copy link
Contributor

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).
Copy link
Contributor

Choose a reason for hiding this comment

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

nginx instead of Nginx?

Copy link
Member

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.

Copy link
Member Author

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

Copy link
Member

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?)

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@fabpot
Copy link
Member Author

fabpot commented Jan 14, 2017

@javiereguiluz The only entry-point should be load for most developers. load is just a shortcut to parse then populate, but using those last 2 directly should be very rare. So, I don't think we need to "document" them here (phpdocs are enough IMHO).

defaults instead.

Symfony Env should only be used in development/testing/staging environments.
For production environments, use "real" environment variables.

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?

Copy link
Member Author

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.

@fabpot
Copy link
Member Author

fabpot commented Jan 21, 2017

Anything blocking the merge (besides time :))?

@teohhanhui
Copy link
Contributor

teohhanhui commented Jan 23, 2017

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.

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 env parameters)...

@xabbuh
Copy link
Member

xabbuh commented Jan 24, 2017

Thank you @fabpot.

@xabbuh xabbuh merged commit 03fda49 into symfony:master Jan 24, 2017
xabbuh added a commit that referenced this pull request Jan 24, 2017
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
@javiereguiluz javiereguiluz modified the milestone: 3.3 Feb 1, 2017
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.