Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[dotenv] Support array notation #25942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions 5 src/Symfony/Component/Dotenv/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.1.0
-----

* Support array notation

3.3.0
-----

Expand Down
53 changes: 50 additions & 3 deletions 53 src/Symfony/Component/Dotenv/Dotenv.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ final class Dotenv
const VARNAME_REGEX = '(?i:[A-Z][A-Z0-9_]*+)';
const STATE_VARNAME = 0;
const STATE_VALUE = 1;
const STATE_KEY = 2;

private $path;
private $cursor;
Expand Down Expand Up @@ -114,25 +115,39 @@ public function parse(string $data, string $path = '.env'): array
$this->state = self::STATE_VARNAME;
$this->values = array();
$name = '';
$key = null;

$this->skipEmptyLines();

while ($this->cursor < $this->end) {
switch ($this->state) {
case self::STATE_VARNAME:
$name = $this->lexVarname();

if ('[' === $this->data[$this->cursor]) {
$this->state = self::STATE_KEY;
} else {
$this->state = self::STATE_VALUE;
++$this->cursor;
}

break;

case self::STATE_KEY:
$key = $this->lexKey();
$this->state = self::STATE_VALUE;
break;

case self::STATE_VALUE:
$this->values[$name] = $this->lexValue();
$value = $this->lexValue();
$this->setValue($name, $key, $value);
$this->state = self::STATE_VARNAME;
break;
}
}

if (self::STATE_VALUE === $this->state) {
$this->values[$name] = '';
$this->setValue($name, $key, '');
}

try {
Expand Down Expand Up @@ -164,12 +179,27 @@ private function lexVarname()
throw $this->createFormatException('Whitespace are not supported after the variable name');
}

if ('=' !== $this->data[$this->cursor] && '[' !== $this->data[$this->cursor]) {
throw $this->createFormatException('Missing = in the environment variable declaration');
}

return $matches[2];
}

private function lexKey()
{
if (!preg_match('/\[(.*?)\]/', $this->data, $matches, 0, $this->cursor)) {
throw $this->createFormatException('Invalid character in variable name');
return;
}
$this->moveCursor($matches[0]);

if ('=' !== $this->data[$this->cursor]) {
throw $this->createFormatException('Missing = in the environment variable declaration');
}
++$this->cursor;

return $matches[2];
return $matches[1];
}

private function lexValue()
Expand Down Expand Up @@ -399,4 +429,21 @@ private function createFormatException($message)
{
return new FormatException($message, new FormatExceptionContext($this->data, $this->path, $this->lineno, $this->cursor));
}

private function setValue($name, $key, $value)
{
if (null === $key) {
$this->values[$name] = $value;
} else {
if (!isset($this->values[$name])) {
$this->values[$name] = array();
}

if ('' === $key) {
$this->values[$name][] = $value;
} else {
$this->values[$name][$key] = $value;
}
}
}
}
6 changes: 6 additions & 0 deletions 6 src/Symfony/Component/Dotenv/Tests/DotenvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ public function getEnvData()
array('BAR=$LOCAL', array('BAR' => 'local')),
array('BAR=$REMOTE', array('BAR' => 'remote')),
array('FOO=$NOTDEFINED', array('FOO' => '')),

// array
array('FOO[]=', array('FOO' => array(''))),
array('FOO[]=BAR', array('FOO' => array('BAR'))),
array("FOO[]=BAR\nFOO[]=BAZ", array('FOO' => array('BAR', 'BAZ'))),
array("FOO[bar]=BAR\nFOO[baz]=BAZ", array('FOO' => array('bar' => 'BAR', 'baz' => 'BAZ'))),
);

if ('\\' !== DIRECTORY_SEPARATOR) {
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.