Skip to content

[3.0] First-class multiline string support #301

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

Merged
merged 1 commit into from
Jan 2, 2019
Merged
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
63 changes: 62 additions & 1 deletion src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* It's responsible for loading 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"`.
* - multiline variable look always start with a " and end with it, e.g: `key="value
* value"`
*/
class Loader
{
Expand Down Expand Up @@ -84,8 +86,15 @@ public function load()

$filePath = $this->filePath;
$lines = $this->readLinesFromFile($filePath);

// multiline
$multiline = false;
$multilineBuffer = [];

foreach ($lines as $line) {
if (!$this->isComment($line) && $this->looksLikeSetter($line)) {
list($multiline, $line, $multilineBuffer) = $this->multilineProcess($multiline, $line, $multilineBuffer);

if (!$multiline && !$this->isComment($line) && $this->looksLikeSetter($line)) {
$this->setEnvironmentVariable($line);
}
}
Expand Down Expand Up @@ -167,6 +176,34 @@ protected function readLinesFromFile($filePath)
return $lines;
}

/**
* Used to make all multiline variable process.
*
* @param bool $multiline
* @param string $line
* @param array $buffer
*
* @return array
*/
protected function multilineProcess($multiline, $line, $buffer)
{
// check if $line can be multiline variable
if ($this->looksLikeMultilineStart($line)) {
$multiline = true;
}
if ($multiline) {
array_push($buffer, $line);

if ($this->looksLikeMultilineStop($line)) {
$multiline = false;
$line = implode("\n", $buffer);
$buffer = [];
}
}

return [$multiline, $line, $buffer];
}

/**
* Determine if the line in the file is a comment, e.g. begins with a #.
*
Expand All @@ -193,6 +230,30 @@ protected function looksLikeSetter($line)
return strpos($line, '=') !== false;
}

/**
* Determine if the given line can be the start of a multiline variable.
*
* @param string $line
*
* @return bool
*/
protected function looksLikeMultilineStart($line)
{
return strpos($line, '="') !== false && substr_count($line, '"') === 1;
}

/**
* Determine if the given line can be the start of a multiline variable.
*
* @param string $line
*
* @return bool
*/
protected function looksLikeMultilineStop($line)
{
return strpos($line, '"') !== false && substr_count($line, '="') === 0;
}

/**
* Split the compound string into parts.
*
Expand Down
14 changes: 13 additions & 1 deletion tests/Dotenv/DotenvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,26 +263,38 @@ public function testDotenvAssertions()
$this->assertEmpty(getenv('ASSERTVAR3'));
$this->assertSame('0', getenv('ASSERTVAR4'));
$this->assertSame('#foo', getenv('ASSERTVAR5'));
$this->assertSame("val1\nval2", getenv('ASSERTVAR6'));
$this->assertSame("val3", getenv('ASSERTVAR7'));
$this->assertSame("val3", getenv('ASSERTVAR8'));

$dotenv->required([
'ASSERTVAR1',
'ASSERTVAR2',
'ASSERTVAR3',
'ASSERTVAR4',
'ASSERTVAR5',
'ASSERTVAR6',
'ASSERTVAR7',
'ASSERTVAR8',
'ASSERTVAR9',
]);

$dotenv->required([
'ASSERTVAR1',
'ASSERTVAR4',
'ASSERTVAR5',
'ASSERTVAR6',
'ASSERTVAR7',
'ASSERTVAR8',
])->notEmpty();

$dotenv->required([
'ASSERTVAR1',
'ASSERTVAR4',
'ASSERTVAR5',
])->notEmpty()->allowedValues(['0', 'val1', '#foo']);
'ASSERTVAR7',
'ASSERTVAR8',
])->notEmpty()->allowedValues(['0', 'val1', 'val3', '#foo']);

$this->assertTrue(true); // anything wrong an an exception will be thrown
}
Expand Down
10 changes: 9 additions & 1 deletion tests/fixtures/env/assertions.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ ASSERTVAR1="val1"
ASSERTVAR2=""
ASSERTVAR3=" "
ASSERTVAR4="0" # empty looking value
ASSERTVAR5=#foo
ASSERTVAR5=#foo
ASSERTVAR6="val1
val2"
ASSERTVAR7="
val3" #
ASSERTVAR8="val3
"
ASSERTVAR9="
"