Load the `now.json` variables into your development environment.
Load the now.json
variables into your development environment.
With the help of this package, you can easily use your Now environment variables
for the use in development.
If you’re already using a now.json
file, the env
sub property will be assigned
to $_ENV
, $_SERVER
, and getenv()
automatically.
This package does absolutely nothing when running in a Now instance.
Use composer to add the library:
composer require geerthauwaerts/now-env
Add the env
property to your now.json
file and load the variables in your
application with:
use NowEnv\NowEnv;
$nowenv = new NowEnv(__DIR__);
$nowenv->load();
Optionally you can pass in a filename as the second parameter, if you would like to use something other than now.json
.
use NowEnv\NowEnv;
$nowenv = new NowEnv(__DIR__, 'now-secrets.json');
$nowenv->load();
All of the defined variables are now accessible with the getenv
method, and are
available in the $_ENV
and $_SERVER
super-globals.
$example = getenv('EXAMPLE');
$example = $_ENV['EXAMPLE'];
$example = $_SERVER['EXAMPLE'];
By default, NowEnv will NOT overwrite existing environment variables that are
already set in the environment.
If you want NowEnv to overwrite existing environment variables, use overload()
instead of load()
:
use NowEnv\NowEnv;
$nowenv = new NowEnv(__DIR__);
$nowenv->overload();
You can require specific environment variables to be defined by passing a single string:
$nowenv->required('EXAMPLE');
Or an array of strings:
$nowenv->required(['EXAMPLE1', 'EXAMPLE2', 'EXAMPLE3']);
If any environment variables vars are missing, NowEnv will throw a RuntimeException
like this:
One or more environment variables failed assertions: EXAMPLE is missing
Beyond simply requiring a variable to be set, you might also need to ensure the
variable is not empty:
$nowenv->required('EXAMPLE')->notEmpty();
If the environment variable is empty, you’d get an Exception:
One or more environment variables failed assertions: EXAMPLE is empty
You might also need to ensure that the variable is of an integer value. You may do the following:
$nowenv->required('EXAMPLE')->isInteger();
If the environment variable is not an integer, you’d get an Exception:
One or more environment variables failed assertions: EXAMPLE is not an integer
You may need to ensure a variable is in the form of a boolean. You may do the following:
$nowenv->required('EXAMPLE')->isBoolean();
If the environment variable is not a boolean, you’d get an Exception:
One or more environment variables failed assertions: EXAMPLE is not a boolean
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:
$nowenv->required('EXAMPLE')->allowedValues(['VALUE1', 'VALUE2']);
If the environment variable wasn’t in this list of allowed values, you’d get a
similar Exception:
One or more environment variables failed assertions: EXAMPLE is not an allowed value
The GitHub repository is used to keep track of all the bugs and feature
requests; I prefer to work exclusively via GitHib and Twitter.
If you have a patch to contribute:
Shout to @GeertHauwaerts on Twitter at
any time :)
If you like this project and you want to support the development, please consider to donate; all donations are greatly appreciated.