zadorin/better-getenv

Simple helper to retrieve environment variables

v0.0.1 2022-01-06 15:04 UTC

This package is auto-updated.

Last update: 2024-11-06 21:31:16 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