Dotenv
in package
Manages .env files.
Tags
Table of Contents
Constants
- STATE_VALUE = 1
- STATE_VARNAME = 0
- VARNAME_REGEX = '(?i:[A-Z][A-Z0-9_]*+)'
Properties
- $cursor : int
- $data : string
- $debugKey : string
- $end : int
- $envKey : string
- $lineno : int
- $path : string
- $prodEnvs : array<string|int, mixed>
- $usePutenv : bool
- $values : array<string|int, mixed>
Methods
- __construct() : mixed
- bootEnv() : void
- Loads env vars from .env.local.php if the file exists or from the other .env files otherwise.
- load() : void
- Loads one or several .env files.
- loadEnv() : void
- Loads a .env file and the corresponding .env.local, .env.$env and .env.$env.local files if they exist.
- overload() : void
- Loads one or several .env files and enables override existing vars.
- parse() : array<string|int, mixed>
- Parses the contents of an .env file.
- populate() : void
- Sets values as environment variables (via putenv, $_ENV, and $_SERVER).
- setProdEnvs() : $this
- usePutenv() : $this
- createFormatException() : FormatException
- doLoad() : void
- lexNestedExpression() : string
- lexValue() : string
- lexVarname() : string
- moveCursor() : mixed
- resolveCommands() : string
- resolveVariables() : string
- skipEmptyLines() : mixed
Constants
STATE_VALUE
public
mixed
STATE_VALUE
= 1
STATE_VARNAME
public
mixed
STATE_VARNAME
= 0
VARNAME_REGEX
public
mixed
VARNAME_REGEX
= '(?i:[A-Z][A-Z0-9_]*+)'
Properties
$cursor
private
int
$cursor
$data
private
string
$data
$debugKey
private
string
$debugKey
$end
private
int
$end
$envKey
private
string
$envKey
$lineno
private
int
$lineno
$path
private
string
$path
$prodEnvs
private
array<string|int, mixed>
$prodEnvs
= ['prod']
$usePutenv
private
bool
$usePutenv
= false
$values
private
array<string|int, mixed>
$values
= []
Methods
__construct()
public
__construct([string $envKey = 'APP_ENV' ][, string $debugKey = 'APP_DEBUG' ]) : mixed
Parameters
- $envKey : string = 'APP_ENV'
- $debugKey : string = 'APP_DEBUG'
bootEnv()
Loads env vars from .env.local.php if the file exists or from the other .env files otherwise.
public
bootEnv(string $path[, string $defaultEnv = 'dev' ][, array<string|int, mixed> $testEnvs = ['test'] ][, bool $overrideExistingVars = false ]) : void
This method also configures the APP_DEBUG env var according to the current APP_ENV.
See method loadEnv() for rules related to .env files.
Parameters
- $path : string
- $defaultEnv : string = 'dev'
- $testEnvs : array<string|int, mixed> = ['test']
- $overrideExistingVars : bool = false
load()
Loads one or several .env files.
public
load(string $path, array<string|int, string> ...$extraPaths) : void
Parameters
- $path : string
-
A file to load
- $extraPaths : array<string|int, string>
-
A list of additional files to load
Tags
loadEnv()
Loads a .env file and the corresponding .env.local, .env.$env and .env.$env.local files if they exist.
public
loadEnv(string $path[, string|null $envKey = null ][, string $defaultEnv = 'dev' ][, array<string|int, mixed> $testEnvs = ['test'] ][, bool $overrideExistingVars = false ]) : void
.env.local is always ignored in test env because tests should produce the same results for everyone. .env.dist is loaded when it exists and .env is not found.
Parameters
- $path : string
-
A file to load
- $envKey : string|null = null
-
The name of the env vars that defines the app env
- $defaultEnv : string = 'dev'
-
The app env to use when none is defined
- $testEnvs : array<string|int, mixed> = ['test']
-
A list of app envs for which .env.local should be ignored
- $overrideExistingVars : bool = false
Tags
overload()
Loads one or several .env files and enables override existing vars.
public
overload(string $path, array<string|int, string> ...$extraPaths) : void
Parameters
- $path : string
-
A file to load
- $extraPaths : array<string|int, string>
-
A list of additional files to load
Tags
parse()
Parses the contents of an .env file.
public
parse(string $data[, string $path = '.env' ]) : array<string|int, mixed>
Parameters
- $data : string
-
The data to be parsed
- $path : string = '.env'
-
The original file name where data where stored (used for more meaningful error messages)
Tags
Return values
array<string|int, mixed>populate()
Sets values as environment variables (via putenv, $_ENV, and $_SERVER).
public
populate(array<string|int, mixed> $values[, bool $overrideExistingVars = false ]) : void
Parameters
- $values : array<string|int, mixed>
-
An array of env variables
- $overrideExistingVars : bool = false
-
true when existing environment variables must be overridden
setProdEnvs()
public
setProdEnvs(array<string|int, mixed> $prodEnvs) : $this
Parameters
- $prodEnvs : array<string|int, mixed>
Return values
$thisusePutenv()
public
usePutenv([bool $usePutenv = true ]) : $this
Parameters
- $usePutenv : bool = true
-
If
putenv()
should be used to define environment variables or not. Beware thatputenv()
is not thread safe, that's why this setting defaults to false
Return values
$thiscreateFormatException()
private
createFormatException(string $message) : FormatException
Parameters
- $message : string
Return values
FormatExceptiondoLoad()
private
doLoad(bool $overrideExistingVars, array<string|int, mixed> $paths) : void
Parameters
- $overrideExistingVars : bool
- $paths : array<string|int, mixed>
lexNestedExpression()
private
lexNestedExpression() : string
Return values
stringlexValue()
private
lexValue() : string
Return values
stringlexVarname()
private
lexVarname() : string
Return values
stringmoveCursor()
private
moveCursor(string $text) : mixed
Parameters
- $text : string
resolveCommands()
private
resolveCommands(string $value, array<string|int, mixed> $loadedVars) : string
Parameters
- $value : string
- $loadedVars : array<string|int, mixed>
Return values
stringresolveVariables()
private
resolveVariables(string $value, array<string|int, mixed> $loadedVars) : string
Parameters
- $value : string
- $loadedVars : array<string|int, mixed>
Return values
stringskipEmptyLines()
private
skipEmptyLines() : mixed