13 Commits

Author SHA1 Message Date
e95e0db218 Update phpdoc info for error 1 2022-06-09 12:56:11 +09:00
0d3e10fe26 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"
2022-06-09 09:17:21 +09:00
1389d5c768 Add git attributes file 2022-06-08 18:19:38 +09:00
df23a7a1b1 Update documentation 2022-06-08 15:37:10 +09:00
652c3554f8 Fix comment in phpunit test file 2022-06-08 15:31:48 +09:00
2c592f0289 Add back vendor folders for development 2022-06-08 14:28:56 +09:00
6ee4358579 Merge branch 'development' 2022-06-08 14:27:20 +09:00
135d3d2dd4 Add gitignore file for phpunit cache 2022-06-08 14:26:58 +09:00
c0f1d9daf7 Merge git.tokyo.tequila.jp:CodeBlocks/dotenv 2022-06-08 14:25:56 +09:00
1f3ec93fb9 Revert to development git ignore file 2022-06-08 14:23:42 +09:00
2c7931b7a5 Add static checker, phpunit config file 2022-06-08 14:12:35 +09:00
914b6c1da8 Clean out files that should not be pushed to git for composer publish 2022-06-08 14:08:55 +09:00
69a386d00f Add gitingore file for vendor, composer.lock, etc files 2022-06-08 14:07:58 +09:00
7 changed files with 69 additions and 5 deletions

6
.gitattributes vendored Normal file
View File

@@ -0,0 +1,6 @@
test/ export-ignore
phpstan.neon export-ignore
phpunit.xml export-ignore
psalm.xml export-ignore
.phan/ export-ignore
.* export-ignore

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
.phpunit.result.cache

View File

@@ -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

View File

@@ -7,7 +7,7 @@
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<directory name="." />
<ignoreFiles>
<directory name="vendor" />
<directory name="test" />

View File

@@ -6,6 +6,9 @@ namespace gullevek\dotEnv;
class DotEnv
{
/** @var string constant comment char, set to # */
private const COMMENT_CHAR = '#';
/**
* parses .env file
*
@@ -23,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
*/
@@ -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__

View File

@@ -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
*/
@@ -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",

View File

@@ -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"