EnvironmentVariables
extends LoadEnvironmentVariables
in package
Class EnvironmentVariables
Table of Contents
Properties
- $environmentEnsuredKeys : array<string|int, mixed>
- An array of environment keys that are ensured to be set.
Methods
- getEnvironmentVariable() : mixed
- Gets an environment variable.
- checkEnvironmentFileExists() : bool
- Checks if the environment file exists and is readable.
- ensureRequiredEnvironmentVariables() : void
- Ensure required environment variables are set.
- load() : void
- Loads the environment variables from the specified path.
- normalizeEnvValue() : mixed
- Normalize environment variable values.
- setEnvironmentPath() : string
- Sets the environment path by trimming the path and adding the '.env' extension if needed.
Properties
$environmentEnsuredKeys
An array of environment keys that are ensured to be set.
protected
static array<string|int, mixed>
$environmentEnsuredKeys
= [
// 'app' => ['name', 'env', 'debug', 'url', 'timezone', 'locale', 'fallback_locale', 'key', 'cipher'],
'database' => ['DB_HOST', 'DB_PORT', 'DB_DATABASE', 'DB_USERNAME', 'DB_PASSWORD'],
]
The keys in this array are the names of the configs that are ensured to be set. The values of this array are arrays of environment variables that are ensured to be set for each config.
Example: [ 'app' => ['name', 'env', 'debug', 'url', 'timezone', 'locale', 'fallback_locale', 'key', 'cipher'], 'database' => ['DB_HOST', 'DB_PORT', 'DB_DATABASE', 'DB_USERNAME', 'DB_PASSWORD'], ]
Methods
getEnvironmentVariable()
Gets an environment variable.
public
static getEnvironmentVariable(string $key[, mixed $default = null ]) : mixed
Gets an environment variable from the global $_ENV array. If the environment variable is not set, the default value will be returned.
Parameters
- $key : string
-
The key of the environment variable.
- $default : mixed = null
-
The default value to return if the environment variable is not set.
Return values
mixed —The value of the environment variable or the default value.
checkEnvironmentFileExists()
Checks if the environment file exists and is readable.
protected
static checkEnvironmentFileExists(string $path) : bool
Parameters
- $path : string
-
The path to the environment file.
Return values
bool —True if the file exists and is readable, false otherwise.
ensureRequiredEnvironmentVariables()
Ensure required environment variables are set.
protected
static ensureRequiredEnvironmentVariables() : void
Iterate over the list of required environment variables and throw an exception if any of them are not set.
Tags
load()
Loads the environment variables from the specified path.
protected
static load(string $path) : void
Parameters
- $path : string
-
The path to the environment file.
normalizeEnvValue()
Normalize environment variable values.
protected
static normalizeEnvValue(mixed $value) : mixed
Normalize environment variable values from their string representation to their actual values. This includes values like "true", "(true)", "false", "(false)", "empty", "(empty)", "null" and "(null)".
Parameters
- $value : mixed
-
The value to normalize.
Return values
mixed —The normalized value.
setEnvironmentPath()
Sets the environment path by trimming the path and adding the '.env' extension if needed.
protected
static setEnvironmentPath(string|null $path, string $appPath) : string
Parameters
- $path : string|null
-
The path to be set. If empty, the default path will be used.
- $appPath : string
-
The path to the application.
Return values
string —The set environment path.