Handle environment variables like a breeze.
composer require flavioheleno/envThis library comes with two helpers:
Env\Read, which will try to read a variable and return its value or fallback to the$defaultvalue when the variable isn't set;Env\Required, which will try to read a variable and return its value or throw an exception when the variable isn't set.
You can read environment variables with:
$value = Env\Read::asString('my_var', 'default_value');On the other hand, you can require environment variables with:
try {
$value = Env\Required::asString('my_var');
} catch (RuntimeException $exception) {
// handle exception
}The library loads the environment variables into a internal copy on the first method call.
If you ever need to update the internal copy, you can do that with:
Env\Read::updateEnv();or:
Env\Required::updateEnv();All methods below are available in both the Env\Read and Env\Required helpers.
- updateEnv: Updates the internal env var copy;
- asString:: Returns the value as a string;
- asArray: Returns the values as an array (comma separated);
- asInteger: Returns the value as an integer;
- asFloat: Returns the value as a float;
- asBool: Returns the value as a boolean;
- fromJson: Returns the value as a json decoded value/object;
- fromSerialized: Returns the value as a unserialized value/object.
There are a few helper scripts that can be called by composer, such as:
- Static Code Analysis:
php composer.phar run check - Code Linting:
php composer.phar run lint - Tests:
php composer.phar run test
NOTE: to run the Code Linting, you must download the ruleset from here first.
This library is licensed under the MIT License.