mirror of
https://gitlab.com/TheGamecraft/c-cms.git
synced 2026-04-22 02:59:10 -04:00
ALPHA 3.0.2
This commit is contained in:
73
vendor/vlucas/phpdotenv/src/Loader.php
vendored
73
vendor/vlucas/phpdotenv/src/Loader.php
vendored
@@ -28,6 +28,13 @@ class Loader
|
||||
*/
|
||||
protected $immutable;
|
||||
|
||||
/**
|
||||
* The list of environment variables declared inside the 'env' file.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $variableNames = array();
|
||||
|
||||
/**
|
||||
* Create a new loader instance.
|
||||
*
|
||||
@@ -42,6 +49,29 @@ class Loader
|
||||
$this->immutable = $immutable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set immutable value.
|
||||
*
|
||||
* @param bool $immutable
|
||||
* @return $this
|
||||
*/
|
||||
public function setImmutable($immutable = false)
|
||||
{
|
||||
$this->immutable = $immutable;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get immutable value.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getImmutable()
|
||||
{
|
||||
return $this->immutable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load `.env` file in given directory.
|
||||
*
|
||||
@@ -92,9 +122,7 @@ class Loader
|
||||
*/
|
||||
protected function normaliseEnvironmentVariable($name, $value)
|
||||
{
|
||||
list($name, $value) = $this->splitCompoundStringIntoParts($name, $value);
|
||||
list($name, $value) = $this->sanitiseVariableName($name, $value);
|
||||
list($name, $value) = $this->sanitiseVariableValue($name, $value);
|
||||
list($name, $value) = $this->processFilters($name, $value);
|
||||
|
||||
$value = $this->resolveNestedVariables($value);
|
||||
|
||||
@@ -104,7 +132,7 @@ class Loader
|
||||
/**
|
||||
* Process the runtime filters.
|
||||
*
|
||||
* Called from the `VariableFactory`, passed as a callback in `$this->loadFromFile()`.
|
||||
* Called from `normaliseEnvironmentVariable` and the `VariableFactory`, passed as a callback in `$this->loadFromFile()`.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
@@ -147,7 +175,9 @@ class Loader
|
||||
*/
|
||||
protected function isComment($line)
|
||||
{
|
||||
return strpos(ltrim($line), '#') === 0;
|
||||
$line = ltrim($line);
|
||||
|
||||
return isset($line[0]) && $line[0] === '#';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -203,16 +233,16 @@ class Loader
|
||||
$quote = $value[0];
|
||||
$regexPattern = sprintf(
|
||||
'/^
|
||||
%1$s # match a quote at the start of the value
|
||||
( # capturing sub-pattern used
|
||||
(?: # we do not need to capture this
|
||||
[^%1$s\\\\] # any character other than a quote or backslash
|
||||
|\\\\\\\\ # or two backslashes together
|
||||
|\\\\%1$s # or an escaped quote e.g \"
|
||||
)* # as many characters that match the previous rules
|
||||
) # end of the capturing sub-pattern
|
||||
%1$s # and the closing quote
|
||||
.*$ # and discard any string after the closing quote
|
||||
%1$s # match a quote at the start of the value
|
||||
( # capturing sub-pattern used
|
||||
(?: # we do not need to capture this
|
||||
[^%1$s\\\\]* # any character other than a quote or backslash
|
||||
|\\\\\\\\ # or two backslashes together
|
||||
|\\\\%1$s # or an escaped quote e.g \"
|
||||
)* # as many characters that match the previous rules
|
||||
) # end of the capturing sub-pattern
|
||||
%1$s # and the closing quote
|
||||
.*$ # and discard any string after the closing quote
|
||||
/mx',
|
||||
$quote
|
||||
);
|
||||
@@ -223,6 +253,11 @@ class Loader
|
||||
$parts = explode(' #', $value, 2);
|
||||
$value = trim($parts[0]);
|
||||
|
||||
// Check if value is a comment (usually triggered when empty value with comment)
|
||||
if (preg_match('/^#/', $value) > 0) {
|
||||
$value = '';
|
||||
}
|
||||
|
||||
// Unquoted values cannot contain whitespace
|
||||
if (preg_match('/\s+/', $value) > 0) {
|
||||
throw new InvalidFileException('Dotenv values containing spaces must be surrounded by quotes.');
|
||||
@@ -235,7 +270,7 @@ class Loader
|
||||
/**
|
||||
* Resolve the nested variables.
|
||||
*
|
||||
* Look for {$varname} patterns in the variable value and replace with an
|
||||
* Look for ${varname} patterns in the variable value and replace with an
|
||||
* existing environment variable.
|
||||
*
|
||||
* @param string $value
|
||||
@@ -247,7 +282,7 @@ class Loader
|
||||
if (strpos($value, '$') !== false) {
|
||||
$loader = $this;
|
||||
$value = preg_replace_callback(
|
||||
'/\${([a-zA-Z0-9_]+)}/',
|
||||
'/\${([a-zA-Z0-9_.]+)}/',
|
||||
function ($matchedPatterns) use ($loader) {
|
||||
$nestedVariable = $loader->getEnvironmentVariable($matchedPatterns[1]);
|
||||
if ($nestedVariable === null) {
|
||||
@@ -287,7 +322,7 @@ class Loader
|
||||
*/
|
||||
protected function beginsWithAQuote($value)
|
||||
{
|
||||
return strpbrk($value[0], '"\'') !== false;
|
||||
return isset($value[0]) && ($value[0] === '"' || $value[0] === '\'');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -329,6 +364,8 @@ class Loader
|
||||
{
|
||||
list($name, $value) = $this->normaliseEnvironmentVariable($name, $value);
|
||||
|
||||
$this->variableNames[] = $name;
|
||||
|
||||
// Don't overwrite existing environment variables if we're immutable
|
||||
// Ruby's dotenv does this with `ENV[key] ||= value`.
|
||||
if ($this->immutable && $this->getEnvironmentVariable($name) !== null) {
|
||||
|
||||
Reference in New Issue
Block a user