Env
in package
Environment configuration helper.
Load environment configs from the system or a file.
Table of Contents
Constants
- DEV : mixed = 'dev'
- ENV : mixed = [self::DEV, self::QA, self::TEST, self::PROD]
- PROD : mixed = 'prod'
- QA : mixed = 'qa'
- RE : mixed = '/^([^=#]+)=(.*)/'
- Pattern for matching file env files.
- TEST : mixed = 'test'
Properties
- $config : array<string|int, string>|null
- $DEFAULT : string
- The default environment, default 'DEV'.
- $ENV_NAME : string
- The key name to determine the current environment mode.
Methods
- config() : void
- Configure the config.
- env() : string|null
- A shorthand method for returning the appropriate value for a config.
- environment() : string
- What is the current environment mode?
- get() : string|null
- Get a variable.
- getConfig() : array<string, string>
- Create a config from a set of environment variables.
- is() : bool
- Is this app in an expected environment?
- isContainer() : bool
- Is this app inside a container? docker or podman?
- isDocker() : bool
- Is this app inside a docker container?
- isPodman() : bool
- Is this app inside a podman container?
- isProduction() : bool
- Is this app in production mode?
- isQA() : bool
- Is this app in QA (quality assurance) mode?
- loadFromFile() : array<string, string>
- Load the config from a file (Recommended).
- loadFromSystem() : array<string, string>
- Load the config from the system environment.
- load() : array<string, string>
- Get the current config or load it from the system environment if not already.
Constants
DEV
public
mixed
DEV
= 'dev'
ENV
public
mixed
ENV
= [self::DEV, self::QA, self::TEST, self::PROD]
PROD
public
mixed
PROD
= 'prod'
QA
public
mixed
QA
= 'qa'
RE
Pattern for matching file env files.
public
mixed
RE
= '/^([^=#]+)=(.*)/'
TEST
public
mixed
TEST
= 'test'
Properties
$config
public
static array<string|int, string>|null
$config
= null
$DEFAULT
The default environment, default 'DEV'.
public
static string
$DEFAULT
= self::DEV
$ENV_NAME
The key name to determine the current environment mode.
public
static string
$ENV_NAME
= 'SITES_ENVIRONMENT'
Methods
config()
Configure the config.
public
static config(array<string|int, mixed> $config) : void
- Set the default environment with 'DEFAULT'
- Set the environment lookup name with 'ENV_NAME'
Parameters
- $config : array<string|int, mixed>
-
(ENV_NAME, DEFAULT)
env()
A shorthand method for returning the appropriate value for a config.
public
static env(array<string|int, mixed> $config) : string|null
For example:
Env::env([
'prod' => 'secret',
'dev' => 'testing',
]);
If a matching environment key doesn't exist, it'll use the default
environment (Env::$DEFAULT, typically 'dev').
If the default env is also not present, it uses the first config item.
Parameters
- $config : array<string|int, mixed>
Return values
string|nullenvironment()
What is the current environment mode?
public
static environment() : string
This will normalize the environment name so 1-to-1 comparisons between
the Env::ENV variables will always work.
Return values
stringget()
Get a variable.
public
static get(string $key) : string|null
Note, This will load variables from the system if not already populated.
Parameters
- $key : string
Return values
string|nullgetConfig()
Create a config from a set of environment variables.
public
static getConfig([array<string|int, mixed>|null $keys = null ]) : array<string, string>
This is essential a shorthand mapping between environment variables and some esoteric config of your own.
There are 3 methods.
// 1. Provide a list of keys to get a subset config.
Env::getConfig([
'DB_HOST',
'DB_USER',
]);
// => [ 'DB_HOST' => 'abc', 'DB_USER' => 'def']
// 2. You can rename the keys like this.
Env::getConfig([
'host' => 'DB_HOST',
'user' => 'DB_USER',
]);
// => [ 'host' => 'abc', 'user' => 'def']
// 3. You can specify defaults like this.
Env::getConfig([
'host' => [null, 'localhost'],
'user' => ['DB_USER', 'test'],
]);
// => [ 'host' => 'localhost', 'user' => 'def']
Note, This will load variables from the system if not already populated.
Parameters
- $keys : array<string|int, mixed>|null = null
Return values
array<string, string> —[name => value]
is()
Is this app in an expected environment?
public
static is(string $expected) : bool
Parameters
- $expected : string
Return values
boolisContainer()
Is this app inside a container? docker or podman?
public
static isContainer() : bool
Return values
boolisDocker()
Is this app inside a docker container?
public
static isDocker() : bool
Return values
boolisPodman()
Is this app inside a podman container?
public
static isPodman() : bool
Return values
boolisProduction()
Is this app in production mode?
public
static isProduction() : bool
Return values
boolisQA()
Is this app in QA (quality assurance) mode?
public
static isQA() : bool
Return values
boolloadFromFile()
Load the config from a file (Recommended).
public
static loadFromFile(string $path[, bool $merge = false ]) : array<string, string>
Parameters
- $path : string
- $merge : bool = false
Return values
array<string, string> —[name => value]
loadFromSystem()
Load the config from the system environment.
public
static loadFromSystem([bool $clean = false ][, bool $merge = false ]) : array<string, string>
Parameters
- $clean : bool = false
-
Remove variables once we've read them.
- $merge : bool = false
Return values
array<string, string> —[name => value]
load()
Get the current config or load it from the system environment if not already.
protected
static load() : array<string, string>
It's recommended to first load the environment from a file in your bootstrap.
Return values
array<string, string> —[name => value]