From a7bca09bf457d709eaef5899dfd8e99dcc7d213d Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Wed, 8 Jun 2022 15:32:41 +0900 Subject: [PATCH 1/5] Comment fix in phpunit test file --- test/phpUnitTests/DotEnvTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/phpUnitTests/DotEnvTest.php b/test/phpUnitTests/DotEnvTest.php index f87ceb6..49c45f3 100644 --- a/test/phpUnitTests/DotEnvTest.php +++ b/test/phpUnitTests/DotEnvTest.php @@ -7,7 +7,7 @@ namespace tests; use PHPUnit\Framework\TestCase; /** - * Test class for ACL\Login + * Test class for DotEnv * @coversDefaultClass \gullevek\DotEnv * @testdox \gullevek\DotEnv method tests */ From 2ec6650c99a35851b13131c6ed1afabfa7f13cae Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Wed, 8 Jun 2022 15:37:43 +0900 Subject: [PATCH 2/5] Readme file update --- Readme.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 1c80ca8..3dac56c 100644 --- a/Readme.md +++ b/Readme.md @@ -9,7 +9,19 @@ repository. The `.env` should *NEVER* be checked into anything ## How to install -`comoser require gullevek/dotEnv` +`composer require gullevek/dotEnv` + +## Run it + +Create a `.env` file in the current folder. +Create a file like below + +```php +require '../vendor/autoload.php'; +gullevek\dotEnv\DotEnv::readEnvFile(__DIR__); +``` + +All data will be in the `$_ENV` array ## How it works From f8e5cb64ad835bc0c7635fe2a351e349ac914f3a Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Wed, 8 Jun 2022 18:20:11 +0900 Subject: [PATCH 3/5] Add git attributes file --- .gitattributes | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..bde73f2 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,6 @@ +test/ export-ignore +phpstan.neon export-ignore +phpunit.xml export-ignore +psalm.xml export-ignore +.phan/ export-ignore +.* export-ignore From 74b0d02a7c12ed7cca3e1242adf538d25803e0a6 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Thu, 9 Jun 2022 09:20:29 +0900 Subject: [PATCH 4/5] 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" From 899586ac8399a10961a58ac900655e59688b3292 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Thu, 9 Jun 2022 12:56:32 +0900 Subject: [PATCH 5/5] Update phpdoc info for error 1 --- src/DotEnv.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DotEnv.php b/src/DotEnv.php index d1549db..a75dd71 100644 --- a/src/DotEnv.php +++ b/src/DotEnv.php @@ -26,7 +26,7 @@ class DotEnv * @param string $env_file What file to load, default is .env * @return int -1 other error * 0 for success full load - * 1 for file loadable, but no data inside + * 1 for file loadable, no data or data already loaded * 2 for file not readable or open failed * 3 for file not found */