Work with environment variables and .env files
{@see Env::get()}, {@see Env::getInt()}, etc. check $_ENV
, $_SERVER
and
{@see getenv()} for a given variable and return the first value found. If the
value is not of the expected type, an {@see InvalidEnvironmentException} is
thrown. If the variable is not present in the environment, $default
is
returned if given, otherwise an {@see InvalidEnvironmentException} is thrown.
Methods |
public
static
|
loadFiles(string ...$filenames): void
Load values from one or more .env files into the environment
Load values from one or more .env files into the environment
Values are applied to $_ENV , $_SERVER and {@see putenv()} unless
already present in one of them.
Changes are applied after parsing all files. If a file contains invalid
syntax, an exception is thrown and the environment is not modified.
Later values override earlier ones.
Throws
|
#
|
public
static
|
apply(int-mask-of<Env::APPLY_*> $flags = Env::APPLY_ALL): void
Apply values from the environment to the running script
Apply values from the environment to the running script
|
#
|
public
static
|
set(string $name, string $value): void
Set an environment variable
Set an environment variable
The value is applied to $_ENV , $_SERVER and {@see putenv()}.
|
#
|
public
static
|
unset(string $name): void
Unset an environment variable
Unset an environment variable
The variable is removed from $_ENV , $_SERVER and {@see putenv()}.
|
#
|
public
static
|
has(string $name): bool
Check if a variable is present in the environment
Check if a variable is present in the environment
|
#
|
public
static
|
get<T is string|null|false>(
string $name,
T|(Closure(): T) $default = false,
): (T is string ? string : (T is null ? string|null : string|never))
Get a value from the environment
Get a value from the environment
|
#
|
public
static
|
getInt<T is int|null|false>(
string $name,
T|(Closure(): T) $default = false,
): (T is int ? int : (T is null ? int|null : int|never))
Get an integer value from the environment
Get an integer value from the environment
|
#
|
public
static
|
getBool<T is bool|null|-1>(
string $name,
T|(Closure(): T) $default = -1,
): (T is bool ? bool : (T is null ? bool|null : bool|never))
Get a boolean value from the environment
Get a boolean value from the environment
|
#
|
public
static
|
getList<T is string[]|null|false>(
string $name,
T|(Closure(): T) $default = false,
string $delimiter = ',',
): (T is string[] ? string[] : (T is null ? string[]|null : string[]|never))
Get a list of values from the environment
Get a list of values from the environment
|
#
|
public
static
|
getIntList<T is int[]|null|false>(
string $name,
T|(Closure(): T) $default = false,
string $delimiter = ',',
): (T is int[] ? int[] : (T is null ? int[]|null : int[]|never))
Get a list of integers from the environment
Get a list of integers from the environment
|
#
|
public
static
|
getNullable<T is string|null|false>(string $name, T|(Closure(): T) $default = false): ?string
Get a value from the environment, returning null if it's empty
Get a value from the environment, returning null if it's empty
|
#
|
public
static
|
getNullableInt<T is int|null|false>(string $name, T|(Closure(): T) $default = false): ?int
Get an integer value from the environment, returning null if it's empty
Get an integer value from the environment, returning null if it's empty
|
#
|
public
static
|
getNullableBool<T is bool|null|-1>(string $name, T|(Closure(): T) $default = -1): ?bool
Get a boolean value from the environment, returning null if it's empty
Get a boolean value from the environment, returning null if it's empty
|
#
|
public
static
|
getEnvironment(): ?string
Get the name of the current environment, e.g. "production" or
"development"
Get the name of the current environment, e.g. "production" or
"development"
Tries each of the following in turn and returns null if none are
present in the environment:
|
#
|
public
static
|
getDryRun(): bool
Check if dry-run mode is enabled in the environment
Check if dry-run mode is enabled in the environment
|
#
|
public
static
|
setDryRun(bool $value): void
Enable or disable dry-run mode in the environment
Enable or disable dry-run mode in the environment
|
#
|
public
static
|
getDebug(): bool
Check if debug mode is enabled in the environment
Check if debug mode is enabled in the environment
|
#
|
public
static
|
setDebug(bool $value): void
Enable or disable debug mode in the environment
Enable or disable debug mode in the environment
|
#
|
public
static
|
getFlag(string $name): bool
Check if a flag is enabled in the environment
Check if a flag is enabled in the environment
|
#
|
public
static
|
setFlag(string $name, bool $value): void
Enable or disable a flag in the environment
Enable or disable a flag in the environment
|
#
|
public
static
|
getHomeDir(): ?string
Get the current user's home directory from the environment
Get the current user's home directory from the environment
|
#
|