Documentation

Loader
in package

This is the loaded class.

It's responsible for loading variables by reading a file from disk and:

  • stripping comments beginning with a #,
  • parsing lines that look shell variable setters, e.g export key = value, key="value".

Table of Contents

$variableNames  : array<string|int, mixed>
The list of environment variables declared inside the 'env' file.
$filePath  : string
The file path.
$immutable  : bool
Are we immutable?
__construct()  : void
Create a new loader instance.
clearEnvironmentVariable()  : void
Clear an environment variable.
getEnvironmentVariable()  : string|null
Search the different places for environment variables and return first value found.
getImmutable()  : bool
Get immutable value.
load()  : array<string|int, mixed>
Load `.env` file in given directory.
processFilters()  : array<string|int, mixed>
Process the runtime filters.
setEnvironmentVariable()  : void
Set an environment variable.
setImmutable()  : $this
Set immutable value.
ensureFileIsReadable()  : void
Ensures the given filePath is readable.
isComment()  : bool
Determine if the line in the file is a comment, e.g. begins with a #.
looksLikeSetter()  : bool
Determine if the given line looks like it's setting a variable.
normaliseEnvironmentVariable()  : array<string|int, mixed>
Normalise the given environment variable.
readLinesFromFile()  : array<string|int, mixed>
Read lines from the file, auto detecting line endings.
resolveNestedVariables()  : mixed
Resolve the nested variables.
sanitiseVariableName()  : array<string|int, mixed>
Strips quotes and the optional leading "export " from the environment variable name.
sanitiseVariableValue()  : array<string|int, mixed>
Strips quotes from the environment variable value.
splitCompoundStringIntoParts()  : array<string|int, mixed>
Split the compound string into parts.

Properties

$variableNames

The list of environment variables declared inside the 'env' file.

public array<string|int, mixed> $variableNames = array()

$filePath

The file path.

protected string $filePath

$immutable

Are we immutable?

protected bool $immutable

Methods

__construct()

Create a new loader instance.

public __construct(string $filePath[, bool $immutable = false ]) : void
Parameters
$filePath : string
$immutable : bool = false
Return values
void

clearEnvironmentVariable()

Clear an environment variable.

public clearEnvironmentVariable(string $name) : void

This is not (currently) used by Dotenv but is provided as a utility method for 3rd party code.

This is done using:

  • putenv,
  • unset($_ENV, $_SERVER).
Parameters
$name : string
Tags
see
setEnvironmentVariable()
Return values
void

getEnvironmentVariable()

Search the different places for environment variables and return first value found.

public getEnvironmentVariable(string $name) : string|null
Parameters
$name : string
Return values
string|null

getImmutable()

Get immutable value.

public getImmutable() : bool
Return values
bool

processFilters()

Process the runtime filters.

public processFilters(string $name, string $value) : array<string|int, mixed>

Called from normaliseEnvironmentVariable and the VariableFactory, passed as a callback in $this->loadFromFile().

Parameters
$name : string
$value : string
Tags
throws
InvalidFileException
Return values
array<string|int, mixed>

setEnvironmentVariable()

Set an environment variable.

public setEnvironmentVariable(string $name[, string|null $value = null ]) : void

This is done using:

  • putenv,
  • $_ENV,
  • $_SERVER.

The environment variable value is stripped of single and double quotes.

Parameters
$name : string
$value : string|null = null
Tags
throws
InvalidFileException
Return values
void

setImmutable()

Set immutable value.

public setImmutable([bool $immutable = false ]) : $this
Parameters
$immutable : bool = false
Return values
$this

ensureFileIsReadable()

Ensures the given filePath is readable.

protected ensureFileIsReadable() : void
Tags
throws
InvalidPathException
Return values
void

isComment()

Determine if the line in the file is a comment, e.g. begins with a #.

protected isComment(string $line) : bool
Parameters
$line : string
Return values
bool

looksLikeSetter()

Determine if the given line looks like it's setting a variable.

protected looksLikeSetter(string $line) : bool
Parameters
$line : string
Return values
bool

normaliseEnvironmentVariable()

Normalise the given environment variable.

protected normaliseEnvironmentVariable(string $name, string $value) : array<string|int, mixed>

Takes value as passed in by developer and:

  • ensures we're dealing with a separate name and value, breaking apart the name string if needed,
  • cleaning the value of quotes,
  • cleaning the name of quotes,
  • resolving nested variables.
Parameters
$name : string
$value : string
Tags
throws
InvalidFileException
Return values
array<string|int, mixed>

readLinesFromFile()

Read lines from the file, auto detecting line endings.

protected readLinesFromFile(string $filePath) : array<string|int, mixed>
Parameters
$filePath : string
Return values
array<string|int, mixed>

resolveNestedVariables()

Resolve the nested variables.

protected resolveNestedVariables(string $value) : mixed

Look for ${varname} patterns in the variable value and replace with an existing environment variable.

Parameters
$value : string
Return values
mixed

sanitiseVariableName()

Strips quotes and the optional leading "export " from the environment variable name.

protected sanitiseVariableName(string $name, string $value) : array<string|int, mixed>
Parameters
$name : string
$value : string
Return values
array<string|int, mixed>

sanitiseVariableValue()

Strips quotes from the environment variable value.

protected sanitiseVariableValue(string $name, string $value) : array<string|int, mixed>
Parameters
$name : string
$value : string
Tags
throws
InvalidFileException
Return values
array<string|int, mixed>

splitCompoundStringIntoParts()

Split the compound string into parts.

protected splitCompoundStringIntoParts(string $name, string $value) : array<string|int, mixed>

If the $name contains an = sign, then we split it into 2 parts, a name & value disregarding the $value passed in.

Parameters
$name : string
$value : string
Return values
array<string|int, mixed>

Search results