项目作者: GeertHauwaerts

项目描述 :
Load the `now.json` variables into your development environment.
高级语言: Shell
项目地址: git://github.com/GeertHauwaerts/now-env.git
创建时间: 2018-12-06T20:14:12Z
项目社区:https://github.com/GeertHauwaerts/now-env

开源协议:BSD 3-Clause "New" or "Revised" License

下载


PHP NowEnv

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.

Build Status

Installation

Use composer to add the library:

  1. composer require geerthauwaerts/now-env

Usage

Add the env property to your now.json file and load the variables in your
application with:

  1. use NowEnv\NowEnv;
  2. $nowenv = new NowEnv(__DIR__);
  3. $nowenv->load();

Optionally you can pass in a filename as the second parameter, if you would like to use something other than now.json.

  1. use NowEnv\NowEnv;
  2. $nowenv = new NowEnv(__DIR__, 'now-secrets.json');
  3. $nowenv->load();

All of the defined variables are now accessible with the getenv method, and are
available in the $_ENV and $_SERVER super-globals.

  1. $example = getenv('EXAMPLE');
  2. $example = $_ENV['EXAMPLE'];
  3. $example = $_SERVER['EXAMPLE'];

Immutability

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

  1. use NowEnv\NowEnv;
  2. $nowenv = new NowEnv(__DIR__);
  3. $nowenv->overload();

Requiring variables to be set

You can require specific environment variables to be defined by passing a single string:

  1. $nowenv->required('EXAMPLE');

Or an array of strings:

  1. $nowenv->required(['EXAMPLE1', 'EXAMPLE2', 'EXAMPLE3']);

If any environment variables vars are missing, NowEnv will throw a RuntimeException
like this:

  1. One or more environment variables failed assertions: EXAMPLE is missing

Empty variables

Beyond simply requiring a variable to be set, you might also need to ensure the
variable is not empty:

  1. $nowenv->required('EXAMPLE')->notEmpty();

If the environment variable is empty, you’d get an Exception:

  1. One or more environment variables failed assertions: EXAMPLE is empty

Integer variables

You might also need to ensure that the variable is of an integer value. You may do the following:

  1. $nowenv->required('EXAMPLE')->isInteger();

If the environment variable is not an integer, you’d get an Exception:

  1. One or more environment variables failed assertions: EXAMPLE is not an integer

Boolean variables

You may need to ensure a variable is in the form of a boolean. You may do the following:

  1. $nowenv->required('EXAMPLE')->isBoolean();

If the environment variable is not a boolean, you’d get an Exception:

  1. One or more environment variables failed assertions: EXAMPLE is not a boolean

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:

  1. $nowenv->required('EXAMPLE')->allowedValues(['VALUE1', 'VALUE2']);

If the environment variable wasn’t in this list of allowed values, you’d get a
similar Exception:

  1. One or more environment variables failed assertions: EXAMPLE is not an allowed value

Collaboration

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:

  • Fork this repository on GitHub.
  • Create a feature branch for your set of patches.
  • Commit your changes to Git and push them to GitHub.
  • Submit a pull request.

Shout to @GeertHauwaerts on Twitter at
any time :)

Donations

If you like this project and you want to support the development, please consider to donate; all donations are greatly appreciated.

  • Coinbase Commerce: BTC, BCH, DAI, ETH, LTC, USDC
  • BTC: bc1q654z85zv6sujsjqk750sf4j4eahcckdtq0cqrp
  • ETH: 0x4d38b4EB5b0726Dc6bd5770F69348e7472954b41
  • LTC: MBEaP6e4zwro6oNP54yjfC29fVqZ881wdF
  • DOGE: D8LypNzP6GayEBWUKCw3KVc7gwbGBaXynT