Skip to content

Commit ccefc51

Browse files
KorbeilGrahamCampbell
authored andcommitted
Robust comments (#272)
1 parent f84952f commit ccefc51

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

src/Loader.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ protected function sanitiseVariableValue($name, $value)
246246
$parts = explode(' #', $value, 2);
247247
$value = trim($parts[0]);
248248

249+
// Check if value is a comment (usually triggered when empty value with comment)
250+
if (preg_match('/^#/', $value) > 0) {
251+
$value = '';
252+
}
253+
249254
// Unquoted values cannot contain whitespace
250255
if (preg_match('/\s+/', $value) > 0) {
251256
throw new InvalidFileException('Dotenv values containing spaces must be surrounded by quotes.');

tests/fixtures/env/commented.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ CFOO=bar
55
CSPACED="with spaces" # this is a comment
66
CQUOTES="a value with a # character" # this is a comment
77
CQUOTESWITHQUOTE="a value with a # character & a quote \" character inside quotes" # " this is a comment
8+
EMPTY= # comment with empty variable
9+
BOOLEAN=yes # (yes, no)
810

911
CNULL=
1012

0 commit comments

Comments
 (0)