From 0d3e10fe260c5beb6bacebbd0b4e4e1b001f5960 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Thu, 9 Jun 2022 09:17:21 +0900 Subject: [PATCH] Ignore comments at the end of line If the variable value had no quotes a comment at the end of the line was added to the variable. Spaces between text and comment mark will be removed too Old: FOO=Test # Comment -> $_ENV['FOO'] = "Test # Comment" New: FOO=Test # Comment -> $_ENV['FOO'] = "Test" --- psalm.xml | 2 +- src/DotEnv.php | 9 ++++++++- test/phpUnitTests/DotEnvTest.php | 17 +++++++++++++++++ test/phpUnitTests/dotenv/test.env | 21 +++++++++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/psalm.xml b/psalm.xml index 5cde799..3c555d4 100644 --- a/psalm.xml +++ b/psalm.xml @@ -7,7 +7,7 @@ xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" > - + diff --git a/src/DotEnv.php b/src/DotEnv.php index bdfa44a..d1549db 100644 --- a/src/DotEnv.php +++ b/src/DotEnv.php @@ -6,6 +6,9 @@ namespace gullevek\dotEnv; class DotEnv { + /** @var string constant comment char, set to # */ + private const COMMENT_CHAR = '#'; + /** * parses .env file * @@ -72,6 +75,11 @@ class DotEnv // add removed new line back because this is a multi line $value = ltrim($value, '"') . PHP_EOL; } + } else { + // strip any quotes at end for unquoted single line + // an right hand spaces are removed too + $value = false !== ($pos = strpos($value, self::COMMENT_CHAR)) ? + rtrim(substr($value, 0, $pos)) : $value; } // if block is set, we strip line of slashes $_ENV[$var] = $block === true ? stripslashes($value) : $value; @@ -95,5 +103,4 @@ class DotEnv } } - // __END__ diff --git a/test/phpUnitTests/DotEnvTest.php b/test/phpUnitTests/DotEnvTest.php index 49c45f3..11296fd 100644 --- a/test/phpUnitTests/DotEnvTest.php +++ b/test/phpUnitTests/DotEnvTest.php @@ -25,12 +25,29 @@ final class DotEnvTest extends TestCase 'OTHER' => 'B IS B', 'Complex' => 'A B \"D is F', 'HAS_SPACE' => 'ABC', + 'HAS_COMMENT_QUOTES_SPACE' => 'Comment at end with quotes and space', + 'HAS_COMMENT_QUOTES_NO_SPACE' => 'Comment at end with quotes no space', + 'HAS_COMMENT_NO_QUOTES_SPACE' => 'Comment at end no quotes and space', + 'HAS_COMMENT_NO_QUOTES_NO_SPACE' => 'Comment at end no quotes no space', + 'COMMENT_IN_TEXT_QUOTES' => 'Foo bar # comment in here', 'FAILURE' => 'ABC', 'SIMPLEBOX' => 'A B C', 'TITLE' => '1', 'FOO' => '1.2', 'SOME.TEST' => 'Test Var', 'SOME.LIVE' => 'Live Var', + 'A_TEST1' => 'foo', + 'A_TEST2' => '${TEST1:-bar}', + 'A_TEST3' => '${TEST4:-bar}', + 'A_TEST5' => 'null', + 'A_TEST6' => '${TEST5-bar}', + 'A_TEST7' => '${TEST6:-bar}', + 'B_TEST1' => 'foo', + 'B_TEST2' => '${TEST1:=bar}', + 'B_TEST3' => '${TEST4:=bar}', + 'B_TEST5' => 'null', + 'B_TEST6' => '${TEST5=bar}', + 'B_TEST7' => '${TEST6=bar}', 'Test' => 'A', 'TEST' => 'B', 'LINE' => "ABC\nDEF", diff --git a/test/phpUnitTests/dotenv/test.env b/test/phpUnitTests/dotenv/test.env index 43f8e33..46d3456 100644 --- a/test/phpUnitTests/dotenv/test.env +++ b/test/phpUnitTests/dotenv/test.env @@ -4,12 +4,33 @@ OTHER="B IS B" Complex="A B \"D is F" # COMMENT HAS_SPACE= "ABC"; +# COMMENT AT END +HAS_COMMENT_QUOTES_SPACE="Comment at end with quotes and space" # Comment QE +HAS_COMMENT_QUOTES_NO_SPACE="Comment at end with quotes no space"# Comment QES +HAS_COMMENT_NO_QUOTES_SPACE=Comment at end no quotes and space # Comment NQE +HAS_COMMENT_NO_QUOTES_NO_SPACE=Comment at end no quotes no space# Comment NQES +COMMENT_IN_TEXT_QUOTES="Foo bar # comment in here" FAILURE = ABC SIMPLEBOX= A B C TITLE=1 FOO=1.2 SOME.TEST=Test Var SOME.LIVE=Live Var +# VAR TESTS - +A_TEST1 = foo +A_TEST2 = ${TEST1:-bar} # TEST1 is set so the value of TEST2 = foo +A_TEST3 = ${TEST4:-bar} # TEST4 is not set so the value of TEST3 = bar +A_TEST5 = null +A_TEST6 = ${TEST5-bar} # TEST5 is set but empty so the value of TEST6 = null +A_TEST7 = ${TEST6:-bar} # TEST5 is set and empty so the value of TEST7 = bar +# VAR TESTS = +B_TEST1 = foo +B_TEST2 = ${TEST1:=bar} # TEST1 is set so the value of TEST2 = foo +B_TEST3 = ${TEST4:=bar} # TEST4 is not set so the value of TEST3 = bar and TEST4 = bar +B_TEST5 = null +B_TEST6 = ${TEST5=bar} # TEST5 is set but emtpy so the value of TEST6 = null +B_TEST7 = ${TEST6=bar} # TEST5 is set and empty so the value of TEST7 = bar and TEST5 = bar +# VAR TEST END Test="A" TEST="B" TEST="D"