rancoud/environment

Environment package

2.1.13 2024-11-09 21:35 UTC

README

Packagist PHP Version Support Packagist Version Packagist Downloads Composer dependencies Test workflow Codecov

Read Environment file (.env).
Can complete or override data from getenv() / $_ENV / $_SERVER

Installation

composer require rancoud/environment

.env File example

# 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
"

How to use it?

Warning, call constructor will not load values, you can:

  • use load() function
  • use any functions that automatically call load() inside

Simple example

// search .env file
$env = new Environment(__DIR__);
$values = $env->getAll();
$value = $env->get('a', 'defaultvalue');

Check keys and values

// 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']);

Complete and Override values

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:

  1. getenv()
  2. $_ENV
  3. $_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);

Enable cache

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

When load() is called?

For simplicity load() is automatically called when using thoses functions:

  • get
  • getAll
  • exists
  • complete

Multiline

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 />');

Include another .env

Inside .env file you can include another .env file with the @ operator at the begining of the line

Constructor variations

// 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');

Environment Constructor

Settings

Mandatory

Optionnals

Environment Methods

General Commands

  • load():void
  • get(name: string, [default: mixed = null]): mixed|null
  • getAll(): arrray
  • exists(name: string|array): bool
  • allowedValues(name: string, values: array): bool

Cache File

  • enableCache(): void
  • disableCache(): void
  • flushCache(): void

Env variables

  • complete(flags: Environment::GETENV | Environment::ENV | Environment::SERVER): void
  • override(flags: Environment::GETENV | Environment::ENV | Environment::SERVER): void

Multilines endline interpretation

  • setEndline(endline: string): void
  • getEndline(): string

How to Dev

composer ci for php-cs-fixer and phpunit and coverage
composer lint for php-cs-fixer
composer test for phpunit and coverage