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)
Setup
- Clone this repo.
- Copy
local.env.dist
to local.env
and update GitHub.com token as
appropriate.
- Run
make test
to 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
$varname
and returns the corresponding value string
- if
$varname
is not set or the value is empty (only whitespace), get
returns $default
parameter
- if the value string corresponding to
$varname
is 'true', 'false' or 'null', get
returns
php values of true
, false
, or null
respectively
- NOTE: Any value string containing 'true' with any combination of case and/or leading/trailing whitespace still returns php
true
.
false
and null
are 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
$varname
and returns the corresponding trimmed value string
- if
$varname
is not set or the value is empty (only whitespace), getString
returns $default
parameter
-
getBoolean - public static function getBoolean($varname, $default = null): ?bool
, (*6)
- searches the local environment for
$varname
and returns the corresponding boolean value string
- if
$varname
is not set or the value is empty (only whitespace), getBoolean
returns $default
parameter
- if the value string corresponding to
$varname
is 'true', 'false' or 'null', getBoolean
returns
php values of true
, false
, or null
respectively
- if the value is not boolean,
getBoolean
returns $default
parameter
- NOTE: Any value string containing 'true' with any combination of case and/or leading/trailing whitespace still returns php
true
.
false
and null
are handled similarly.
-
getArray - public static function getArray($varname, array $default = [])
, (*7)
- searches the local environment for
$varname
and returns the corresponding value string with comma separated elements as a php array
- if
$varname
is not set or the value is empty (only whitespace), getArray
returns $default
parameter which must be an array
- if $default is not an array, it throws a
TypeError
exception
-
requireEnv - public static function requireEnv($varname)
, (*8)
- searches the local environment for
$varname
and returns the corresponding value string
- if
$varname
is not set or the value is empty (only whitespace), it throws EnvVarNotFoundException
- 'true', 'false', and 'null' are handled the same as
get()
-
requireArray - public static function requireArray($varname)
, (*9)
- searches the local environment for
$varname
and returns the corresponding value string with comma separated elements as a php array
- if
$varname
is not set or the value is empty (only whitespace), it throws EnvVarNotFoundException
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')
- returns null
-
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')
- returns false
-
Env::get('TRUE')
- returns true
-
Env::get('NULL')
- returns null
-
Env::get('LOWERCASE')
- returns 'abc123'
-
Env::get('UPPERCASE')
- returns 'ABC123'
-
requireEnv - public static function requireEnv($varname)
, (*13)
-
Env::requireEnv('NOTFOUND')
- throws EnvVarNotFoundException
-
Env::requireEnv('EMPTY')
- throws EnvVarNotFoundException
-
Env::requireEnv('WHITESPACE')
- returns 'Some whitespace'
-
Env::requireEnv('FALSE')
- returns false
-
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')
- throws TypeError
exception
-
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')
- throws EnvVarNotFoundException
-
Env::requireArray('EMPTY')
- throws EnvVarNotFoundException
-
Env::requireArray('ARRAY1')
- returns ['one']
-
Env::requireArray('ARRAY')
- returns ['one', 'two', '', 'three']