Class Dotenv\Loader
Inheritance | Dotenv\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
Property | Type | Description | Defined By |
---|---|---|---|
$filePath | string | Dotenv\Loader | |
$immutable | bool | Dotenv\Loader |
Public Methods
Method | Description | Defined 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
Method | Description | Defined 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
Method Details
Create a new loader instance.
public void __construct ( $filePath, $immutable = false ) | ||
$filePath | string | |
$immutable | bool |
Determine if the given string begins with a quote.
protected bool beginsWithAQuote ( $value ) | ||
$value | string |
Ensures the given filePath is readable.
protected void ensureFileIsReadable ( ) | ||
throws | InvalidArgumentException |
---|
Search the different places for environment variables and return first value found.
public string getEnvironmentVariable ( $name ) | ||
$name | string |
Determine if the line in the file is a comment, e.g. begins with a #.
protected bool isComment ( $line ) | ||
$line | string |
Load .env
file in given directory.
public void load ( ) |
Determine if the given line looks like it's setting a variable.
protected bool looksLikeSetter ( $line ) | ||
$line | string |
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 |
Process the runtime filters.
Called from the VariableFactory
, passed as a callback in $this->loadFromFile()
.
public array processFilters ( $name, $value ) | ||
$name | string | |
$value | string |
Read lines from the file, auto detecting line endings.
protected array readLinesFromFile ( $filePath ) | ||
$filePath | string |
Resolve the nested variables.
Look for {$varname} patterns in the variable value and replace with an existing environment variable.
protected mixed resolveNestedVariables ( $value ) | ||
$value |
Strips quotes and the optional leading "export " from the environment variable name.
protected array sanitiseVariableName ( $name, $value ) | ||
$name | string | |
$value | string |
Strips quotes from the environment variable value.
protected array sanitiseVariableValue ( $name, $value ) | ||
$name | string | |
$value | string | |
throws | InvalidArgumentException |
---|
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 |
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 |