Sil/PhpEnv/
Simple PHP library for getting (or requiring) environment variables, designed 
to handle true, false, and null more intelligently. If desired, an 
environment variable's value can be split into an array automatically., (*1)
Build Status
 , (*2)
, (*2)
Setup
- Clone this repo.
- Copy local.env.disttolocal.envand update GitHub.com token as 
appropriate.
- Run make testto install dependencies and run PHPUnit tests.
Makefile script
There is a Makefile in place to simplify common tasks.
- make test - does composer install and runs phpunit tests, (*3)
Classes in Sil/PhpEnv namespace
- 
Env: use Sil\PhpEnv\Env;
- 
EnvVarNotFoundException: use Sil\PhpEnv\EnvVarNotFoundException;
Class Env summary of functions
- 
get - public static function get($varname, $default = null), (*4)
 
- searches the local environment for $varnameand returns the corresponding value string
- if $varnameis not set or the value is empty (only whitespace),getreturns$defaultparameter
- if the value string corresponding to $varnameis 'true', 'false' or 'null',getreturns 
php values oftrue,false, ornullrespectively
- NOTE: Any value string containing 'true' with any combination of case and/or leading/trailing whitespace still returns php true.
 falseandnullare handled similarly.  Other value strings will have leading/trailing whitespace trimmed.
 
- 
getString - public static function getString($varname, $default = null): ?string, (*5)
 
- searches the local environment for $varnameand returns the corresponding trimmed value string
- if $varnameis not set or the value is empty (only whitespace),getStringreturns$defaultparameter
 
- 
getBoolean - public static function getBoolean($varname, $default = null): ?bool, (*6)
 
- searches the local environment for $varnameand returns the corresponding boolean value string
- if $varnameis not set or the value is empty (only whitespace),getBooleanreturns$defaultparameter
- if the value string corresponding to $varnameis 'true', 'false' or 'null',getBooleanreturns
php values oftrue,false, ornullrespectively
- if the value is not boolean, getBooleanreturns$defaultparameter
- NOTE: Any value string containing 'true' with any combination of case and/or leading/trailing whitespace still returns php true.falseandnullare handled similarly.
 
- 
getArray - public static function getArray($varname, array $default = []), (*7)
 
- searches the local environment for $varnameand returns the corresponding value string with comma separated elements as a php array
- if $varnameis not set or the value is empty (only whitespace),getArrayreturns$defaultparameter which must be an array
- if $default is not an array, it throws a TypeErrorexception
 
- 
requireEnv - public static function requireEnv($varname), (*8)
 
- searches the local environment for $varnameand returns the corresponding value string
- if $varnameis not set or the value is empty (only whitespace), it throwsEnvVarNotFoundException
- 'true', 'false', and 'null' are handled the same as get()
 
- 
requireArray - public static function requireArray($varname), (*9)
 
- searches the local environment for $varnameand returns the corresponding value string with comma separated elements as a php array
- if $varnameis not set or the value is empty (only whitespace), it throwsEnvVarNotFoundException
 
Class EnvVarNotFoundException
class EnvVarNotFoundException extends \Exception, (*10)
EnvVarNotFoundException is thrown by requireEnv() and requireArray() when $varname is not found in the local
environment or the corresponding value string is empty (only whitespace), (*11)
Env example function calls
Assume this local.env file
EMPTY=
SPACES=      
WHITESPACE= Some whitespace    
FALSE=False
TRUE=TRUE
NULL=null
LOWERCASE=abc123
UPPERCASE=ABC123
ARRAY0=
ARRAY1=one
ARRAY=one,two,,three
Example function calls and results
- 
get - public static function get($varname, $default = null), (*12)
 
- 
Env::get('NOTFOUND')- returnsnull
- 
Env::get('NOTFOUND', 'bad data')- returns'bad data'
- 
Env::get('EMPTY')- returns''
- 
Env::get('SPACES')- returns''
- 
Env::get('WHITESPACE')- returns'Some whitespace'
- 
Env::get('FALSE')- returnsfalse
- 
Env::get('TRUE')- returnstrue
- 
Env::get('NULL')- returnsnull
- 
Env::get('LOWERCASE')- returns'abc123'
- 
Env::get('UPPERCASE')- returns'ABC123'
 
- 
requireEnv - public static function requireEnv($varname), (*13)
 
- 
Env::requireEnv('NOTFOUND')- throwsEnvVarNotFoundException
- 
Env::requireEnv('EMPTY')- throwsEnvVarNotFoundException
- 
Env::requireEnv('WHITESPACE')- returns'Some whitespace'
- 
Env::requireEnv('FALSE')- returnsfalse
- 
Env::requireEnv('LOWERCASE')- returns'abc123'
 
- 
getArray - public static function getArray($varname, array $default = []), (*14)
 
- 
Env::getArray('NOTFOUND')- returns[]
- 
Env::getArray('NOTFOUND', ['one', 'two'])- returns['one', 'two']
- 
Env::getArray('NOTFOUND', 'one,two,three')- throwsTypeErrorexception
- 
Env::getArray('ARRAY0')- returns['']
- 
Env::getArray('ARRAY1')- returns['one']
- 
Env::getArray('ARRAY')- returns['one', 'two', '', 'three']
 
- 
requireArray - public static function requireArray($varname), (*15)
 
- 
Env::requireArray('NOTFOUND')- throwsEnvVarNotFoundException
- 
Env::requireArray('EMPTY')- throwsEnvVarNotFoundException
- 
Env::requireArray('ARRAY1')- returns['one']
- 
Env::requireArray('ARRAY')- returns['one', 'two', '', 'three']