Class Dotenv\Loader

InheritanceDotenv\Loader

Loader.

Loads 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"

Protected Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$filePath string Dotenv\Loader
$immutable bool Dotenv\Loader

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__construct() Create a new loader instance. Dotenv\Loader
getEnvironmentVariable() Search the different places for environment variables and return first value found. Dotenv\Loader
load() Load .env file in given directory. Dotenv\Loader
processFilters() Process the runtime filters. Dotenv\Loader
setEnvironmentVariable() Set an environment variable. Dotenv\Loader

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
beginsWithAQuote() Determine if the given string begins with a quote. Dotenv\Loader
ensureFileIsReadable() Ensures the given filePath is readable. Dotenv\Loader
isComment() Determine if the line in the file is a comment, e.g. begins with a #. Dotenv\Loader
looksLikeSetter() Determine if the given line looks like it's setting a variable. Dotenv\Loader
normaliseEnvironmentVariable() Normalise the given environment variable. Dotenv\Loader
readLinesFromFile() Read lines from the file, auto detecting line endings. Dotenv\Loader
resolveNestedVariables() Resolve the nested variables. Dotenv\Loader
sanitiseVariableName() Strips quotes and the optional leading "export " from the environment variable name. Dotenv\Loader
sanitiseVariableValue() Strips quotes from the environment variable value. Dotenv\Loader
splitCompoundStringIntoParts() Split the compound string into parts. Dotenv\Loader

Property Details

$filePath protected property
protected string $filePath null
$immutable protected property
protected bool $immutable false

Method Details

__construct() public method

Create a new loader instance.

public void __construct ( $filePath, $immutable false )
$filePath string
$immutable bool
beginsWithAQuote() protected method

Determine if the given string begins with a quote.

protected bool beginsWithAQuote ( $value )
$value string
ensureFileIsReadable() protected method

Ensures the given filePath is readable.

protected void ensureFileIsReadable ( )
throws InvalidArgumentException
getEnvironmentVariable() public method

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

public string getEnvironmentVariable ( $name )
$name string
isComment() protected method

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

protected bool isComment ( $line )
$line string
load() public method

Load .env file in given directory.

public void load ( )
looksLikeSetter() protected method

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

protected bool looksLikeSetter ( $line )
$line string
normaliseEnvironmentVariable() protected method

Normalise the given environment variable.

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
protected array normaliseEnvironmentVariable ( $name, $value )
$name
$value
processFilters() public method

Process the runtime filters.

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

public array processFilters ( $name, $value )
$name string
$value string
readLinesFromFile() protected method

Read lines from the file, auto detecting line endings.

protected array readLinesFromFile ( $filePath )
$filePath string
resolveNestedVariables() protected method

Resolve the nested variables.

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

protected mixed resolveNestedVariables ( $value )
$value
sanitiseVariableName() protected method

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

protected array sanitiseVariableName ( $name, $value )
$name string
$value string
sanitiseVariableValue() protected method

Strips quotes from the environment variable value.

protected array sanitiseVariableValue ( $name, $value )
$name string
$value string
throws InvalidArgumentException
setEnvironmentVariable() public method

Set an environment variable.

This is done using:

  • putenv
  • $_ENV
  • $_SERVER.

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

public void setEnvironmentVariable ( $name, $value null )
$name
$value string|null
splitCompoundStringIntoParts() protected method

Split the compound string into parts.

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

protected array splitCompoundStringIntoParts ( $name, $value )
$name string
$value string