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.
- Clone this repo.
- Copy
local.env.dist
tolocal.env
and update GitHub.com token as appropriate. - Run
make test
to install dependencies and run PHPUnit tests.
There is a Makefile in place to simplify common tasks.
make test
- doescomposer install
and runs phpunit tests
- Env:
use Sil\PhpEnv\Env;
- EnvVarNotFoundException:
use Sil\PhpEnv\EnvVarNotFoundException;
-
get -
public static function get($varname, $default = null)
- 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 oftrue
,false
, ornull
respectively - NOTE: Any value string containing 'true' with any combination of case and/or leading/trailing whitespace still returns php
true
.
false
andnull
are handled similarly. Other value strings will have leading/trailing whitespace trimmed.
- searches the local environment for
-
getString -
public static function getString($varname, $default = null): ?string
- 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
- searches the local environment for
-
getBoolean -
public static function getBoolean($varname, $default = null): ?bool
- 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 oftrue
,false
, ornull
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
andnull
are handled similarly.
- searches the local environment for
-
getArray -
public static function getArray($varname, array $default = [])
- 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
- searches the local environment for
-
requireEnv -
public static function requireEnv($varname)
- 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 throwsEnvVarNotFoundException
- 'true', 'false', and 'null' are handled the same as
get()
- searches the local environment for
-
requireArray -
public static function requireArray($varname)
- 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 throwsEnvVarNotFoundException
- searches the local environment for
class EnvVarNotFoundException extends \Exception
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)
EMPTY=
SPACES=
WHITESPACE= Some whitespace
FALSE=False
TRUE=TRUE
NULL=null
LOWERCASE=abc123
UPPERCASE=ABC123
ARRAY0=
ARRAY1=one
ARRAY=one,two,,three
-
get -
public static function get($varname, $default = null)
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)
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 = [])
Env::getArray('NOTFOUND')
- returns[]
Env::getArray('NOTFOUND', ['one', 'two'])
- returns['one', 'two']
Env::getArray('NOTFOUND', 'one,two,three')
- throwsTypeError
exceptionEnv::getArray('ARRAY0')
- returns['']
Env::getArray('ARRAY1')
- returns['one']
Env::getArray('ARRAY')
- returns['one', 'two', '', 'three']
-
requireArray -
public static function requireArray($varname)
Env::requireArray('NOTFOUND')
- throwsEnvVarNotFoundException
Env::requireArray('EMPTY')
- throwsEnvVarNotFoundException
Env::requireArray('ARRAY1')
- returns['one']
Env::requireArray('ARRAY')
- returns['one', 'two', '', 'three']