Dotenv
in package
Manages .env files.
Tags
Table of Contents
- STATE_VALUE = 1
- STATE_VARNAME = 0
- VARNAME_REGEX = '(?i:_?[A-Z][A-Z0-9_]*+)'
- $cursor : mixed
- $data : mixed
- $debugKey : mixed
- $end : mixed
- $envKey : mixed
- $lineno : mixed
- $path : mixed
- $prodEnvs : mixed
- $usePutenv : mixed
- $values : mixed
- __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
mixed
$cursor
$data
private
mixed
$data
$debugKey
private
mixed
$debugKey
$end
private
mixed
$end
$envKey
private
mixed
$envKey
$lineno
private
mixed
$lineno
$path
private
mixed
$path
$prodEnvs
private
mixed
$prodEnvs
= ['prod']
$usePutenv
private
mixed
$usePutenv
= false
$values
private
mixed
$values
Methods
__construct()
public
__construct([string $envKey = 'APP_ENV' ][, string $debugKey = 'APP_DEBUG' ]) : mixed
Parameters
- $envKey : string = 'APP_ENV'
- $debugKey : string = 'APP_DEBUG'
Return values
mixed —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
Return values
void —load()
Loads one or several .env files.
public
load(string $path, string ...$extraPaths) : void
Parameters
- $path : string
-
A file to load
- $extraPaths : string
-
A list of additional files to load
Tags
Return values
void —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
-
Whether existing environment variables set by the system should be overridden
Tags
Return values
void —overload()
Loads one or several .env files and enables override existing vars.
public
overload(string $path, string ...$extraPaths) : void
Parameters
- $path : string
-
A file to load
- $extraPaths : string
-
A list of additional files to load
Tags
Return values
void —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
-
Whether existing environment variables set by the system should be overridden
Return values
void —setProdEnvs()
public
setProdEnvs(array<string|int, mixed> $prodEnvs) : $this
Parameters
- $prodEnvs : array<string|int, mixed>
Return values
$this —usePutenv()
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 it's not enabled by default
Return values
$this —createFormatException()
private
createFormatException(string $message) : FormatException
Parameters
- $message : string
Return values
FormatException —doLoad()
private
doLoad(bool $overrideExistingVars, array<string|int, mixed> $paths) : void
Parameters
- $overrideExistingVars : bool
- $paths : array<string|int, mixed>
Return values
void —lexNestedExpression()
private
lexNestedExpression() : string
Return values
string —lexValue()
private
lexValue() : string
Return values
string —lexVarname()
private
lexVarname() : string
Return values
string —moveCursor()
private
moveCursor(string $text) : mixed
Parameters
- $text : string
Return values
mixed —resolveCommands()
private
resolveCommands(string $value, array<string|int, mixed> $loadedVars) : string
Parameters
- $value : string
- $loadedVars : array<string|int, mixed>
Return values
string —resolveVariables()
private
resolveVariables(string $value, array<string|int, mixed> $loadedVars) : string
Parameters
- $value : string
- $loadedVars : array<string|int, mixed>
Return values
string —skipEmptyLines()
private
skipEmptyLines() : mixed