Read Environment file (.env).
Can complete or override data from getenv()
/ $_ENV
/ $_SERVER
composer require rancoud/environment
# type of variables used
STRING=STRING
STRING_QUOTES=" STRING QUOTES "
INTEGER=9
FLOAT=9.0
BOOL_TRUE=TRUE
BOOL_FALSE=FALSE
NULL_VALUE=NULL
# variable in value
HOME=/user/www
CORE=$HOME/core
; $HOME won't be interpreted
USE_DOLLAR_IN_STRING="$HOME"
#comment line 1
;comment line 2
# import another env file use @ and the filename
@database.env
# multilines
RGPD="
i understand
enough of email for \"me\"
thanks
"
Warning, call constructor will not load values, you can:
- use
load()
function - use any functions that automatically call
load()
inside
// search .env file
$env = new Environment(__DIR__);
$values = $env->getAll();
$value = $env->get('a', 'defaultvalue');
// check if a key exists
$env = new Environment(__DIR__);
$isExists = $env->exists('key1');
$isExists = $env->exists(['key1', 'key2']);
// check if value is set with allowed values
$isAllowed = $env->allowedValues('key1', ['value1', NULL, 'value2']);
Only type conversion will be done on those variables (no replacement with $
).
You have 3 differents flags:
- Environment::GETENV
- Environment::ENV
- Environment::SERVER
Complete is for filling values belong to keys having empty string or no values.
Override is for erasing values belong to keys.
The treatment given by the flags is always in the same order:
getenv()
$_ENV
$_SERVER
You can also use 3 others flags.
Those will inject all keys and values found, your env file is not used for checkings keys.
- Environment::GETENV_ALL
- Environment::ENV_ALL
- Environment::SERVER_ALL
$env = new Environment(__DIR__);
// complete with only getenv()
$env->complete(Environment::GETENV);
// complete with $_ENV then $_SERVER
$env->complete(Environment::SERVER | Environment::ENV);
// override with getenv() will erase values
$env->override(Environment::GETENV);
The file cached will not contains informations from getenv()
/ $_ENV
/ $_SERVER
// force using cache (if not exist it will be created)
$env = new Environment(__DIR__);
$env->enableCache();
$values = $env->getAll();
For simplicity load()
is automatically called when using thoses functions:
- get
- getAll
- exists
- complete
You can check what kind of endline it using, by default it's PHP_EOL
You can change it with for using <br>
// force using cache (if not exist it will be created)
$env = new Environment(__DIR__);
$env->setEndline('<br />');
Inside .env file you can include another .env file with the @
operator at the begining of the line
// search .env file
$env = new Environment(__DIR__);
// search dev.env file
$env = new Environment(__DIR__, 'dev.env');
// search .env file in folders __DIR__ then '/usr'
$env = new Environment([__DIR__, '/usr']);
// search dev.env file in folders __DIR__ then '/usr'
$env = new Environment([__DIR__, '/usr'], 'dev.env');
Parameter | Type | Description |
---|---|---|
folder | string OR array | folder to seek .env file |
Parameter | Type | Default value | Description |
---|---|---|---|
filename | string | .env | custom name of .env file (don't forget to add file extension) |
- load():void
- get(name: string, [default: mixed = null]): mixed|null
- getAll(): arrray
- exists(name: string|array): bool
- allowedValues(name: string, values: array): bool
- enableCache(): void
- disableCache(): void
- flushCache(): void
- complete(flags: Environment::GETENV | Environment::ENV | Environment::SERVER): void
- override(flags: Environment::GETENV | Environment::ENV | Environment::SERVER): void
- setEndline(endline: string): void
- getEndline(): string
composer ci
for php-cs-fixer and phpunit and coverage
composer lint
for php-cs-fixer
composer test
for phpunit and coverage