zadorin / better-getenv
Simple helper to retrieve environment variables
v0.0.1
2022-01-06 15:04 UTC
Requires
- php: >=7.4
Requires (Dev)
- pestphp/pest: ^1.21
- vlucas/phpdotenv: ^5.4
Suggests
- vlucas/phpdotenv: Environment variables loader
This package is auto-updated.
Last update: 2024-12-06 21:40:11 UTC
README
Package provides framework-agnostic helper env($key, $default)
to retrieve environment variables in convenient way.
Highly inspired by Laravel env helper.
Installation
composer require zadorin/better-getenv
Usage
$varName = 'APP_ENV'; $defaultValue = 'prod'; $value = env($varName, $defaultValue);
Second parameter can be a function. In that case helper will use result of this function as default value:
$value = env('CACHE_STORAGE', function () { return env('APP_ENV', 'prod') === 'prod' ? 'redis' : 'file'; });
Usage Notes
Helper will search variable in $_ENV
first, using getenv()
as fallback.
If env()
function already exists in your codebase, you can use namespaced call with same signature:
$value = Zadorin\env('APP_ENV', 'prod');
Helper automatically casts string values 'true', 'false'
to boolean type. Similarly, 'null'
string casts to null.
Tests
./vendor/bin/pest