Documentation

EnvLoader
in package

Simple .env file parser and loader.

This class parses .env files and makes the values available through environment variables and a static getter.

Usage:

use Lwt\Shared\Infrastructure\Bootstrap\EnvLoader;

// Load .env file
EnvLoader::load('/path/to/.env');

// Get values
$dbHost = EnvLoader::get('DB_HOST', 'localhost');
Tags
since
3.0.0

Table of Contents

Properties

$env  : array<string, string>
$loaded  : bool

Methods

all()  : array<string, string>
Get all loaded environment variables.
get()  : string|null
Get an environment variable value.
getBool()  : bool
Get an environment variable as a boolean.
getDatabaseConfig()  : array{server: string, userid: string, passwd: string, dbname: string, socket: string}
Get database configuration array.
getInt()  : int
Get an environment variable as an integer.
has()  : bool
Check if an environment variable exists.
isLoaded()  : bool
Check if a .env file has been loaded.
load()  : bool
Load and parse a .env file.
reset()  : void
Reset the loader state.

Properties

$env

private static array<string, string> $env = []

Loaded environment variables

$loaded

private static bool $loaded = false

Whether the .env file has been loaded

Methods

all()

Get all loaded environment variables.

public static all() : array<string, string>
Return values
array<string, string>

All loaded variables

get()

Get an environment variable value.

public static get(string $key[, string|null $default = null ]) : string|null

Checks in order:

  1. Values loaded from .env file
  2. $_ENV superglobal
  3. getenv() function
  4. Default value
Parameters
$key : string

The environment variable name

$default : string|null = null

Default value if not found

Return values
string|null

The value, or default if not found

getBool()

Get an environment variable as a boolean.

public static getBool(string $key[, bool $default = false ]) : bool

Recognizes: true, 1, yes, on as true false, 0, no, off, empty as false

Parameters
$key : string

The environment variable name

$default : bool = false

Default value if not found

Return values
bool

The boolean value

getDatabaseConfig()

Get database configuration array.

public static getDatabaseConfig() : array{server: string, userid: string, passwd: string, dbname: string, socket: string}

Convenience method that returns all database-related configuration in a single array, suitable for passing to connection functions.

Return values
array{server: string, userid: string, passwd: string, dbname: string, socket: string}

getInt()

Get an environment variable as an integer.

public static getInt(string $key[, int $default = 0 ]) : int
Parameters
$key : string

The environment variable name

$default : int = 0

Default value if not found or not numeric

Return values
int

The integer value

has()

Check if an environment variable exists.

public static has(string $key) : bool
Parameters
$key : string

The environment variable name

Return values
bool

True if the variable exists (even if empty)

isLoaded()

Check if a .env file has been loaded.

public static isLoaded() : bool
Return values
bool

True if loaded

load()

Load and parse a .env file.

public static load(string $path) : bool

The file format supports:

  • KEY=value
  • KEY="value with spaces"
  • KEY='value with spaces'
  • comments

  • Empty lines (ignored)
Parameters
$path : string

Path to the .env file

Return values
bool

True if file was loaded successfully, false otherwise

reset()

Reset the loader state.

public static reset() : void

Primarily used for testing.


        
On this page

Search results