novemb3r/envision

A lightweight library to bring typing to env variables

v5.1.1 2021-07-12 14:07 UTC

This package is auto-updated.

Last update: 2024-10-12 21:25:10 UTC


README

packagist-dt-badge php-version-badge travis-build-status codecov license

Simplest library to bring types for your env variables!

Installing

This library installs as a composer package with

$ composer require novemb3r/envision

or

{
    "require": {
        "novemb3r/envision": "dev-master"
    }
}

Usage

Examples

Booleans

Get boolean variable (default is false):

Envision::getBool('FOO');

or with true as default:

Envision::getBool('FOO', true);

Boolean env variables should be declared like

B1=true
B2=True
B3=TrUe
B4=TRUE # because Envision converts boolean variables to lovercase

and shouldn't be declared like

BW1=1
BW2=yes
BW3=positive
BW4=ok

Numeric types

Get an integer or float:

Envision::getInt('FOO');
Envision::getFloat('FOO', 3.141592);

Strings

Get string:

Envision::getString('FOO');
Envision::getString('FOO', 'default');

Arrays

Get an array specified like

FOO=v1,v2,v3
FOO2=v1,v2,v3,
FOO3=v1/v2/v3

by passing delimiter (default is ,)

Envision::getArray('FOO');
Envision::getArray('FOO1', ',');
Envision::getArray('FOO3', ',', ['v2']);

Configuration

Envision has a set of options to control it's behaviour. Default behaviour is not to look for env variables in $_ENV and $_SERVER arrays. It's okay if you are using symfony/dotenv component to load .env files. But you can change this using

Envision::$options = Envision::USE_ENV_ARRAY | Envision::USE_SERVER_ARRAY;

Also, you can configure Envision to throw InvalidArgumentException if env variable cannot be found:

Envision::$options = Envision::ON_EMPTY_THROW;

or if variable has invalid format:

Envision::$options = Envision::ON_INVALID_THROW;

by default Envision will return default value on empty and throw InvalidArgumentException on invalid value, which equals to

Envision::$options = Envision::ON_EMPTY_RETURN_DEFAULT | Envision::ON_INVALID_THROW;

Testing

To execute test suites run

$ composer test

always keep test coverage at 100% and always run

$ composer prepare-push

before making any changes