PHP Class Dotenv\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"
Show file Open project: vlucas/phpdotenv Class Usage Examples

Protected Properties

Property Type Description
$filePath string The file path.
$immutable boolean Are we immutable?

Public Methods

Method Description
__construct ( string $filePath, boolean $immutable = false ) : void Create a new loader instance.
clearEnvironmentVariable ( string $name ) : void Clear an environment variable.
getEnvironmentVariable ( string $name ) : string | null Search the different places for environment variables and return first value found.
load ( ) : array Load .env file in given directory.
processFilters ( string $name, string $value ) : array Process the runtime filters.
setEnvironmentVariable ( string $name, string | null $value = null ) : void Set an environment variable.

Protected Methods

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

Method Details

__construct() public method

Create a new loader instance.
public __construct ( string $filePath, boolean $immutable = false ) : void
$filePath string
$immutable boolean
return void

beginsWithAQuote() protected method

Determine if the given string begins with a quote.
protected beginsWithAQuote ( string $value ) : boolean
$value string
return boolean

clearEnvironmentVariable() public method

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).
See also: setEnvironmentVariable()
public clearEnvironmentVariable ( string $name ) : void
$name string
return void

ensureFileIsReadable() protected method

Ensures the given filePath is readable.
protected ensureFileIsReadable ( ) : void
return void

getEnvironmentVariable() public method

Search the different places for environment variables and return first value found.
public getEnvironmentVariable ( string $name ) : string | null
$name string
return string | null

isComment() protected method

Determine if the line in the file is a comment, e.g. begins with a #.
protected isComment ( string $line ) : boolean
$line string
return boolean

load() public method

Load .env file in given directory.
public load ( ) : array
return array

looksLikeSetter() protected method

Determine if the given line looks like it's setting a variable.
protected looksLikeSetter ( string $line ) : boolean
$line string
return boolean

normaliseEnvironmentVariable() protected method

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 normaliseEnvironmentVariable ( string $name, string $value ) : array
$name string
$value string
return array

processFilters() public method

Called from the VariableFactory, passed as a callback in $this->loadFromFile().
public processFilters ( string $name, string $value ) : array
$name string
$value string
return array

readLinesFromFile() protected method

Read lines from the file, auto detecting line endings.
protected readLinesFromFile ( string $filePath ) : array
$filePath string
return array

resolveNestedVariables() protected method

Look for {$varname} patterns in the variable value and replace with an existing environment variable.
protected resolveNestedVariables ( string $value ) : mixed
$value string
return mixed

sanitiseVariableName() protected method

Strips quotes and the optional leading "export " from the environment variable name.
protected sanitiseVariableName ( string $name, string $value ) : array
$name string
$value string
return array

sanitiseVariableValue() protected method

Strips quotes from the environment variable value.
protected sanitiseVariableValue ( string $name, string $value ) : array
$name string
$value string
return array

setEnvironmentVariable() public method

This is done using: - putenv, - $_ENV, - $_SERVER. The environment variable value is stripped of single and double quotes.
public setEnvironmentVariable ( string $name, string | null $value = null ) : void
$name string
$value string | null
return void

splitCompoundStringIntoParts() protected method

If the $name contains an = sign, then we split it into 2 parts, a name & value disregarding the $value passed in.
protected splitCompoundStringIntoParts ( string $name, string $value ) : array
$name string
$value string
return array

Property Details

$filePath protected property

The file path.
protected string $filePath
return string

$immutable protected property

Are we immutable?
protected bool $immutable
return boolean