sil-org / 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: 877
Dependents: 2
Suggesters: 0
Security: 0
Stars: 3
Watchers: 8
Forks: 1
Open Issues: 0
pkg:composer/sil-org/php-env
Requires
- php: ^8.0
Requires (Dev)
- phpunit/phpunit: ^12.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.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- doescomposer installand 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
$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.
- searches the local environment for
-
getString -
public static function getString($varname, $default = null): ?string- 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
- searches the local environment for
-
getBoolean -
public static function getBoolean($varname, $default = null): ?bool- 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.
- searches the local environment for
-
getArray -
public static function getArray($varname, array $default = [])- 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
- searches the local environment for
-
requireEnv -
public static function requireEnv($varname)- 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()
- searches the local environment for
-
requireArray -
public static function requireArray($varname)- 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
- 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')- returnsnullEnv::get('NOTFOUND', 'bad data')- returns'bad data'Env::get('EMPTY')- returns''Env::get('SPACES')- returns''Env::get('WHITESPACE')- returns'Some whitespace'Env::get('FALSE')- returnsfalseEnv::get('TRUE')- returnstrueEnv::get('NULL')- returnsnullEnv::get('LOWERCASE')- returns'abc123'Env::get('UPPERCASE')- returns'ABC123'
-
requireEnv -
public static function requireEnv($varname)Env::requireEnv('NOTFOUND')- throwsEnvVarNotFoundExceptionEnv::requireEnv('EMPTY')- throwsEnvVarNotFoundExceptionEnv::requireEnv('WHITESPACE')- returns'Some whitespace'Env::requireEnv('FALSE')- returnsfalseEnv::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')- throwsTypeErrorexceptionEnv::getArray('ARRAY0')- returns['']Env::getArray('ARRAY1')- returns['one']Env::getArray('ARRAY')- returns['one', 'two', '', 'three']
-
requireArray -
public static function requireArray($varname)Env::requireArray('NOTFOUND')- throwsEnvVarNotFoundExceptionEnv::requireArray('EMPTY')- throwsEnvVarNotFoundExceptionEnv::requireArray('ARRAY1')- returns['one']Env::requireArray('ARRAY')- returns['one', 'two', '', 'three']