novemb3r / envision
A lightweight library to bring typing to env variables
Installs: 9 960
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.2
Requires (Dev)
- orklah/psalm-strict-visibility: ^2.0
- phpstan/phpstan: ^0.12.88
- phpunit/phpunit: ^8.5
- squizlabs/php_codesniffer: ^3.6
- vimeo/psalm: ^4.7
README
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