silinternational / php-env
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.
Installs: 44 568
Dependents: 22
Suggesters: 0
Security: 0
Stars: 2
Watchers: 9
Forks: 1
Open Issues: 0
Requires
- php: ^7.4 || ^8.0
Requires (Dev)
- phpunit/phpunit: ^9.0
README
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.
Build Status
Setup
- 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.
Makefile script
There is a Makefile in place to simplify common tasks.
make test
- doescomposer install
and runs phpunit tests
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)
- 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
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)
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)
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']