Download the PHP package maatwebsite/yamlenv without Composer
On this page you can find all versions of the php package maatwebsite/yamlenv. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download maatwebsite/yamlenv
More information about maatwebsite/yamlenv
Files in maatwebsite/yamlenv
Package yamlenv
Short Description Reads `env.yml` and makes a validated list of variables available as environment variables
License LGPL
Informations about the package yamlenv
Maatwebsite\Yamlenv
Reads env.yml and makes a validated list of variables available as environment variables.
This is package is based on vlucas/phpdotenv.
Why choose Yaml over .env?
The benefits of using a env.yml file or similar has been proven a long time ago. The popularity of vlucas/phpdotenv and similar packages is a testament of this. There is no reason why you shouldn't use a package like that in your projects, especially if the amount of variables you need to manage is relatively small.
In case of larger, enterprise scale, applications, the amount of environment settings might very quickly become unmanageable. If it's a multi tenant system even more so. And if you're supporting different versions of the application with its own list of required environment settings, it soon becomes a necessity to automate this process.
These are largely the reasons we decided to move towards Yaml. It provides a few simple advantages over env.yml files:
- It allows nesting, to allow grouping of certain settings ( like database connection credentials ).
- It is easily readable by human as well as multiple automated deployment tools ( Ansible e.g. ).
- There are at least as many ( probably more ) trustworthy packages that work with Yaml ( Symfony being the most important one )
In our initial use case it was the combination of nesting and Ansible compatibility that made us decide to build this package.
Installation with Composer
Usage
The documentation below is, for a large part, the same as Vlucas/Dotenv. The filenames are changed of course and classnames. We tried to keep the package as close in usage to Dotenv as possible, for ease of use. So give credit to Vlucas for providing such a great base to work from!
Of course there are a few things unique to Yamlenv, and that has been added as well.
The env.yml
file is generally kept out of version control since it can contain
sensitive API keys and passwords. A separate env.yml.dist
file can be created
with all the required environment variables defined except for the sensitive
ones, which are either user-supplied for their own development environments or
are communicated elsewhere to project collaborators. The project collaborators
then independently copy the env.yml.dist
file to a local env.yml
and ensure
all the settings are correct for their local environment, filling in the secret
keys or providing their own values when necessary. In this usage, the env.yml
file should be added to the project's .gitignore
file so that it will never
be committed by collaborators. This usage ensures that no sensitive passwords
or API keys will ever be in the version control history so there is less risk
of a security breach, and production values will never have to be shared with
all project collaborators.
Add your application configuration to a env.yml
file in the root of your
project. Make sure the env.yml
file is added to your .gitignore
so it is not
checked-in the code
Now create a file named env.yml.dist
and check this into the project. This
should have the ENV variables you need to have set, but the values should
either be blank or filled with dummy data. The idea is to let people know what
variables are required, but not give them the sensitive production values.
You can then load env.yml
in your application with:
Optionally you can pass in a filename as the second parameter, if you would like to use something other than env.yml
All of the defined variables are now accessible with the getenv
method, and are available in the $_ENV
and $_SERVER
super-globals.
You should also be able to access them using your framework's Request class (if you are using a framework).
Uppercase keys
With Yamlenv you can pass boolean true as the third contructor parameter. This will make sure all your keys will be cast to uppercase
Nesting Variables
Like with Dotenv it's possible to nest an environment variable within another. However, because we are using the Yaml format, nesting us supported natively.
These variables will be flattened into a single level before being added to the environment variables. The different keys will be concatenated into a single key, separated with underscores. So the above example wil give the same results as below:
This also works with multiple levels:
Immutability
By default, Yamlenv will NOT overwrite existing environment variables that are already set in the environment.
If you want Yamlenv to overwrite existing environment variables, use overload
instead of load
:
Requiring Variables to be Set
Using Yamlenv, you can require specific ENV vars to be defined, and throw an Exception if they are not. This is particularly useful to let people know any explicit required variables that your app will not work without.
You can use a single string:
Or an array of strings:
If any ENV vars are missing, Yamlenv will throw a RuntimeException
like this:
Empty Variables
Beyond simply requiring a variable to be set, you might also need to ensure the variable is not empty:
If the environment variable is empty, you'd get an Exception:
Integer Variables
You might also need to ensure the the variable is of an integer value. You may do the following:
If the environment variable is not an integer, you'd get an Exception:
Allowed Values
It is also possible to define a set of values that your environment variable should be. This is especially useful in situations where only a handful of options or drivers are actually supported by your code:
If the environment variable wasn't in this list of allowed values, you'd get a similar Exception:
Comments
You can comment your env.yml
file using the #
character. E.g.
This follows the normal Yaml syntax rules
When nesting variables, it is important to always use key->value based children. While the below is valid Yaml, it does not result in usable variables.
Correct would be:
Usage Notes
When a new developer clones your codebase, they will have an additional
one-time step to manually copy the env.yml.dist
file to env.yml
and fill-in
their own values (or get any sensitive values from a project co-worker).
Yamlenv is made for development environments, and generally should not be
used in production. In production, the actual environment variables should be
set so that there is no overhead of loading the env.yml
file on each request.
This can be achieved via an automated deployment process with tools like
Vagrant, chef, or Puppet, or can be set manually with cloud hosts like
Pagodabox and Heroku.