2017 © Pedro Peláez
 

library env

Environment variable loader and retriever for PHP

image

adhocore/env

Environment variable loader and retriever for PHP

  • Sunday, July 22, 2018
  • by adhocore
  • Repository
  • 2 Watchers
  • 6 Stars
  • 15 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 114 % Grown

The README.md

adhocore/env

Latest Version Travis Build Scrutinizer CI Codecov branch StyleCI Software License Tweet Support , (*1)

  • Environment variable loader and retriever for PHP.
  • Sanitization/Filters can be applied on retrieval if filter extension is loaded.
  • Using env to configure application is one of the 12 postulates.

Installation

composer require adhocore/env

Usage

Loading

use Ahc\Env\Loader;

// Load env variables from .env file to `putenv` by default:
(new Loader)->load('/project/root/.env');

// Pass in boolean second param to control if the env should be reloaded:
(new Loader)->load('/project/root/.env', true);

// Load to $_SERVER global:
(new Loader)->load('/project/root/.env', true, Loader::SERVER);

// Load to $_ENV global and putenv():
(new Loader)->load('/project/root/.env', true, Loader::ENV | Loader::PUTENV);

// Load to all targets:
(new Loader)->load('/project/root/.env', true, Loader::ALL);

Always wrap complex values within double quotes in .env file. Eg: APP_KEY="K&^¢*&D(?<µ}^(P\]X", (*2)

ENV Format

Supports # or ; comments. Literal double quote should be escaped like "". See more examples below:, (*3)

# comment line
a=1
b="2"
c=$3#
; also comment line
d="lol"
# empty
e=
# f is `"6"`
f=""6""
1_2=one_two
# empty too
E=""
A_B=Apple Ball
x=Y

Reference is possible like so:, (*4)

MAIN=1
REF=${MAIN}/2
REF2=${REF}/3
# below will not be parsed as INV is not resolved
REF3=${INV}

Retrieving

use Ahc\Env\Retriever;

// Retrieve:
echo Retriever::getEnv($key);

// Default value:
echo Retriever::getEnv('PAYMENT_GATEWAY', 'stripe');

// Sanitization (pass third and optionally fourth parameters):
echo Retriever::getEnv('MYSQL_PORT', 3306, FILTER_VALIDATE_INT);

// Or you can use `env()` which is alias of `Retriever::getEnv()`:
echo env('THE_KEY');

See filter_var for more on sanitizing/filtering values!, (*5)

Benchmark

If you are interested here is a simple benchmark., (*6)


Consideration

By default this library only loads env to putenv(). Be cautious exposing confidential credentials into $_ENV and $_SERVER which bug/error catchers may log., (*7)

Although this libray is already fast enough, in production you might want to boost performance a little by loading if only required:, (*8)

if (!getenv('<LAST_ENV_APP_SHOULD_BE_AWARE_OF>')) {
    // Override false :)
    (new Loader)->load('/project/root/.env', false);
}

For example if your app last introduced FB_APP_ID env, but this value is not already hard set in the machine, it would be loaded via .env file else you are already covered., (*9)

Credits

This project is release managed by please., (*10)

The Versions

22/07 2018

dev-master

9999999-dev

Environment variable loader and retriever for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Jitendra Adhikari

env env-php env-loader

15/07 2018

v0.0.4

0.0.4.0

Environment variable loader and retriever for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Jitendra Adhikari

env env-php env-loader

15/07 2018

v0.0.3

0.0.3.0

Environment variable loader and retriever for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Jitendra Adhikari

env env-php env-loader

22/10 2017

v0.0.2

0.0.2.0

Environment variable loader and retriever for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Jitendra Adhikari

env env-php env-loader

22/10 2017

v0.0.1

0.0.1.0

Environment variable loader and retriever for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Jitendra Adhikari

env env-php env-loader