[Dotenv] parse concatenated variable values
This commit is contained in:
parent
bfd4173860
commit
aaaf64ddde
@ -168,73 +168,110 @@ final class Dotenv
|
||||
throw $this->createFormatException('Whitespace are not supported before the value');
|
||||
}
|
||||
|
||||
$value = '';
|
||||
$singleQuoted = false;
|
||||
$notQuoted = false;
|
||||
if ("'" === $this->data[$this->cursor]) {
|
||||
$singleQuoted = true;
|
||||
++$this->cursor;
|
||||
while ("\n" !== $this->data[$this->cursor]) {
|
||||
if ("'" === $this->data[$this->cursor]) {
|
||||
if ($this->cursor + 1 === $this->end) {
|
||||
$v = '';
|
||||
|
||||
do {
|
||||
if ("'" === $this->data[$this->cursor]) {
|
||||
$value = '';
|
||||
++$this->cursor;
|
||||
|
||||
while ("\n" !== $this->data[$this->cursor]) {
|
||||
if ("'" === $this->data[$this->cursor]) {
|
||||
break;
|
||||
}
|
||||
if ("'" !== $this->data[$this->cursor + 1]) {
|
||||
break;
|
||||
$value .= $this->data[$this->cursor];
|
||||
++$this->cursor;
|
||||
|
||||
if ($this->cursor === $this->end) {
|
||||
throw $this->createFormatException('Missing quote to end the value');
|
||||
}
|
||||
}
|
||||
if ("\n" === $this->data[$this->cursor]) {
|
||||
throw $this->createFormatException('Missing quote to end the value');
|
||||
}
|
||||
++$this->cursor;
|
||||
$v .= $value;
|
||||
} elseif ('"' === $this->data[$this->cursor]) {
|
||||
$value = '';
|
||||
++$this->cursor;
|
||||
|
||||
while ('"' !== $this->data[$this->cursor] || ('\\' === $this->data[$this->cursor - 1] && '\\' !== $this->data[$this->cursor - 2])) {
|
||||
$value .= $this->data[$this->cursor];
|
||||
++$this->cursor;
|
||||
|
||||
if ($this->cursor === $this->end) {
|
||||
throw $this->createFormatException('Missing quote to end the value');
|
||||
}
|
||||
}
|
||||
if ("\n" === $this->data[$this->cursor]) {
|
||||
throw $this->createFormatException('Missing quote to end the value');
|
||||
}
|
||||
++$this->cursor;
|
||||
$value = str_replace(array('\\\\', '\\"', '\r', '\n'), array('\\', '"', "\r", "\n"), $value);
|
||||
$resolvedValue = $value;
|
||||
$resolvedValue = $this->resolveVariables($resolvedValue);
|
||||
$resolvedValue = $this->resolveCommands($resolvedValue);
|
||||
$v .= $resolvedValue;
|
||||
} else {
|
||||
$value = '';
|
||||
$prevChr = $this->data[$this->cursor - 1];
|
||||
while ($this->cursor < $this->end && !in_array($this->data[$this->cursor], array("\n", '"', "'"), true) && !((' ' === $prevChr || "\t" === $prevChr) && '#' === $this->data[$this->cursor])) {
|
||||
if ('\\' === $this->data[$this->cursor] && isset($this->data[$this->cursor + 1]) && ('"' === $this->data[$this->cursor + 1] || "'" === $this->data[$this->cursor + 1])) {
|
||||
++$this->cursor;
|
||||
}
|
||||
|
||||
$value .= $prevChr = $this->data[$this->cursor];
|
||||
|
||||
if ('$' === $this->data[$this->cursor] && isset($this->data[$this->cursor + 1]) && '(' === $this->data[$this->cursor + 1]) {
|
||||
++$this->cursor;
|
||||
$value .= '('.$this->lexNestedExpression().')';
|
||||
}
|
||||
|
||||
++$this->cursor;
|
||||
}
|
||||
$value .= $this->data[$this->cursor];
|
||||
++$this->cursor;
|
||||
$value = rtrim($value);
|
||||
$resolvedValue = $value;
|
||||
$resolvedValue = $this->resolveVariables($resolvedValue);
|
||||
$resolvedValue = $this->resolveCommands($resolvedValue);
|
||||
|
||||
if ($this->cursor === $this->end) {
|
||||
throw $this->createFormatException('Missing quote to end the value');
|
||||
}
|
||||
}
|
||||
if ("\n" === $this->data[$this->cursor]) {
|
||||
throw $this->createFormatException('Missing quote to end the value');
|
||||
}
|
||||
++$this->cursor;
|
||||
} elseif ('"' === $this->data[$this->cursor]) {
|
||||
++$this->cursor;
|
||||
while ('"' !== $this->data[$this->cursor] || ('\\' === $this->data[$this->cursor - 1] && '\\' !== $this->data[$this->cursor - 2])) {
|
||||
$value .= $this->data[$this->cursor];
|
||||
++$this->cursor;
|
||||
|
||||
if ($this->cursor === $this->end) {
|
||||
throw $this->createFormatException('Missing quote to end the value');
|
||||
}
|
||||
}
|
||||
if ("\n" === $this->data[$this->cursor]) {
|
||||
throw $this->createFormatException('Missing quote to end the value');
|
||||
}
|
||||
++$this->cursor;
|
||||
$value = str_replace(array('\\\\', '\\"', '\r', '\n'), array('\\', '"', "\r", "\n"), $value);
|
||||
} else {
|
||||
$notQuoted = true;
|
||||
$prevChr = $this->data[$this->cursor - 1];
|
||||
while ($this->cursor < $this->end && "\n" !== $this->data[$this->cursor] && !((' ' === $prevChr || "\t" === $prevChr) && '#' === $this->data[$this->cursor])) {
|
||||
if ('\\' === $this->data[$this->cursor] && isset($this->data[$this->cursor + 1]) && ('"' === $this->data[$this->cursor + 1] || "'" === $this->data[$this->cursor + 1])) {
|
||||
++$this->cursor;
|
||||
if ($resolvedValue === $value && preg_match('/\s+/', $value)) {
|
||||
throw $this->createFormatException('A value containing spaces must be surrounded by quotes');
|
||||
}
|
||||
|
||||
$value .= $prevChr = $this->data[$this->cursor];
|
||||
++$this->cursor;
|
||||
$v .= $resolvedValue;
|
||||
|
||||
if ($this->cursor < $this->end && '#' === $this->data[$this->cursor]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
$value = rtrim($value);
|
||||
}
|
||||
} while ($this->cursor < $this->end && "\n" !== $this->data[$this->cursor]);
|
||||
|
||||
$this->skipEmptyLines();
|
||||
|
||||
$currentValue = $value;
|
||||
if (!$singleQuoted) {
|
||||
$value = $this->resolveVariables($value);
|
||||
$value = $this->resolveCommands($value);
|
||||
return $v;
|
||||
}
|
||||
|
||||
private function lexNestedExpression()
|
||||
{
|
||||
++$this->cursor;
|
||||
$value = '';
|
||||
|
||||
while ("\n" !== $this->data[$this->cursor] && ')' !== $this->data[$this->cursor]) {
|
||||
$value .= $this->data[$this->cursor];
|
||||
|
||||
if ('(' === $this->data[$this->cursor]) {
|
||||
$value .= $this->lexNestedExpression().')';
|
||||
}
|
||||
|
||||
++$this->cursor;
|
||||
|
||||
if ($this->cursor === $this->end) {
|
||||
throw $this->createFormatException('Missing closing parenthesis.');
|
||||
}
|
||||
}
|
||||
|
||||
if ($notQuoted && $currentValue == $value && preg_match('/\s+/', $value)) {
|
||||
throw $this->createFormatException('A value containing spaces must be surrounded by quotes');
|
||||
if ("\n" === $this->data[$this->cursor]) {
|
||||
throw $this->createFormatException('Missing closing parenthesis.');
|
||||
}
|
||||
|
||||
return $value;
|
||||
|
@ -85,7 +85,6 @@ class DotenvTest extends TestCase
|
||||
array("FOO='bar'\n", array('FOO' => 'bar')),
|
||||
array("FOO='bar\"foo'\n", array('FOO' => 'bar"foo')),
|
||||
array("FOO=\"bar\\\"foo\"\n", array('FOO' => 'bar"foo')),
|
||||
array("FOO='bar''foo'\n", array('FOO' => 'bar\'foo')),
|
||||
array('FOO="bar\nfoo"', array('FOO' => "bar\nfoo")),
|
||||
array('FOO="bar\rfoo"', array('FOO' => "bar\rfoo")),
|
||||
array('FOO=\'bar\nfoo\'', array('FOO' => 'bar\nfoo')),
|
||||
@ -99,6 +98,10 @@ class DotenvTest extends TestCase
|
||||
array('FOO=\\"BAR', array('FOO' => '"BAR')),
|
||||
|
||||
// concatenated values
|
||||
array("FOO='bar''foo'\n", array('FOO' => 'barfoo')),
|
||||
array("FOO='bar '' baz'", array('FOO' => 'bar baz')),
|
||||
array("FOO=bar\nBAR='baz'\"\$FOO\"", array('FOO' => 'bar', 'BAR' => 'bazbar')),
|
||||
array("FOO='bar '\\'' baz'", array('FOO' => "bar ' baz")),
|
||||
|
||||
// comments
|
||||
array("#FOO=bar\nBAR=foo", array('BAR' => 'foo')),
|
||||
|
Reference in New Issue
Block a user