Add Block settings to dotenv reader
Now blocks can be set as prefix names for variables via [Block Name] type grouping.
This commit is contained in:
@@ -78,7 +78,8 @@ return [
|
|||||||
// to parse, but not analyze
|
// to parse, but not analyze
|
||||||
"exclude_analysis_directory_list" => [
|
"exclude_analysis_directory_list" => [
|
||||||
'vendor',
|
'vendor',
|
||||||
'test'
|
'test',
|
||||||
|
'tmp'
|
||||||
],
|
],
|
||||||
'exclude_file_list' => [
|
'exclude_file_list' => [
|
||||||
],
|
],
|
||||||
|
|||||||
23
Readme.md
23
Readme.md
@@ -61,6 +61,29 @@ DOUBLE="I will be used"
|
|||||||
DOUBLE="This will be ignored"
|
DOUBLE="This will be ignored"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
A prefix name can be set with `[PrefixName]`. Tne name rules are like for variables, but spaces
|
||||||
|
are allowed, but will be converted to "_".
|
||||||
|
The prefix is valid from the time set until the next prefix block appears or the file ends.
|
||||||
|
|
||||||
|
Example
|
||||||
|
|
||||||
|
```ini
|
||||||
|
FOO="bar"
|
||||||
|
FOOBAR="bar bar"
|
||||||
|
[SecitonA]
|
||||||
|
FOO="other bar"
|
||||||
|
FOOBAR="other bar bar"
|
||||||
|
```
|
||||||
|
|
||||||
|
Will have environmen variables as
|
||||||
|
|
||||||
|
```php
|
||||||
|
$_ENV["FOO"];
|
||||||
|
$_ENV["FOOBAR"];
|
||||||
|
$_ENV["SecitonA.FOO"];
|
||||||
|
$_ENV["SecitonA.FOOBAR"];
|
||||||
|
```
|
||||||
|
|
||||||
## Development
|
## Development
|
||||||
|
|
||||||
### Phan
|
### Phan
|
||||||
|
|||||||
@@ -22,6 +22,9 @@ class DotEnv
|
|||||||
* if there are two variables with the same name only the first is used
|
* if there are two variables with the same name only the first is used
|
||||||
* variables are case sensitive
|
* variables are case sensitive
|
||||||
*
|
*
|
||||||
|
* [] Grouping Block Name as prefix until next or end if set,
|
||||||
|
* space replaced by _, all other var rules apply
|
||||||
|
*
|
||||||
* @param string $path Folder to file, default is __DIR__
|
* @param string $path Folder to file, default is __DIR__
|
||||||
* @param string $env_file What file to load, default is .env
|
* @param string $env_file What file to load, default is .env
|
||||||
* @return int -1 other error
|
* @return int -1 other error
|
||||||
@@ -56,10 +59,14 @@ class DotEnv
|
|||||||
$status = 1;
|
$status = 1;
|
||||||
$block = false;
|
$block = false;
|
||||||
$var = '';
|
$var = '';
|
||||||
|
$prefix_name = '';
|
||||||
while ($line = fgets($fp)) {
|
while ($line = fgets($fp)) {
|
||||||
// main match for variable = value part
|
// [] block must be a single line, or it will be ignored
|
||||||
if (preg_match("/^\s*([\w_.]+)\s*=\s*((\"?).*)/", $line, $matches)) {
|
if (preg_match("/^\s*\[([\w_.\s]+)\]/", $line, $matches)) {
|
||||||
$var = $matches[1];
|
$prefix_name = preg_replace("/\s+/", "_", $matches[1]) . ".";
|
||||||
|
} elseif (preg_match("/^\s*([\w_.]+)\s*=\s*((\"?).*)/", $line, $matches)) {
|
||||||
|
// main match for variable = value part
|
||||||
|
$var = $prefix_name . $matches[1];
|
||||||
$value = $matches[2];
|
$value = $matches[2];
|
||||||
$quotes = $matches[3];
|
$quotes = $matches[3];
|
||||||
// write only if env is not set yet, and write only the first time
|
// write only if env is not set yet, and write only the first time
|
||||||
|
|||||||
@@ -60,6 +60,8 @@ final class DotEnvTest extends TestCase
|
|||||||
'HAS_COMMENT_NO_QUOTES_SPACE' => 'Comment at end no quotes and 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',
|
'HAS_COMMENT_NO_QUOTES_NO_SPACE' => 'Comment at end no quotes no space',
|
||||||
'COMMENT_IN_TEXT_QUOTES' => 'Foo bar # comment in here',
|
'COMMENT_IN_TEXT_QUOTES' => 'Foo bar # comment in here',
|
||||||
|
'HAS_EQUAL_NO_QUITES' => 'Is This = Valid',
|
||||||
|
'HAS_EQUAL_QUITES' => 'Is This = Valid',
|
||||||
'FAILURE' => 'ABC',
|
'FAILURE' => 'ABC',
|
||||||
'SIMPLEBOX' => 'A B C',
|
'SIMPLEBOX' => 'A B C',
|
||||||
'TITLE' => '1',
|
'TITLE' => '1',
|
||||||
@@ -87,6 +89,8 @@ final class DotEnvTest extends TestCase
|
|||||||
'__FOOFOO' => 'f ',
|
'__FOOFOO' => 'f ',
|
||||||
123123 => 'number',
|
123123 => 'number',
|
||||||
'EMPTY' => '',
|
'EMPTY' => '',
|
||||||
|
'Var_Test.TEST' => 'Block 1 D',
|
||||||
|
'OtherSet.TEST' => 'Block 2 D',
|
||||||
];
|
];
|
||||||
// 0: folder relative to test folder, if unset __DIR__
|
// 0: folder relative to test folder, if unset __DIR__
|
||||||
// 1: file, if unset .env
|
// 1: file, if unset .env
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ 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_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
|
HAS_COMMENT_NO_QUOTES_NO_SPACE=Comment at end no quotes no space# Comment NQES
|
||||||
COMMENT_IN_TEXT_QUOTES="Foo bar # comment in here"
|
COMMENT_IN_TEXT_QUOTES="Foo bar # comment in here"
|
||||||
|
HAS_EQUAL_NO_QUITES=Is This = Valid
|
||||||
|
HAS_EQUAL_QUITES="Is This = Valid"
|
||||||
FAILURE = ABC
|
FAILURE = ABC
|
||||||
SIMPLEBOX= A B C
|
SIMPLEBOX= A B C
|
||||||
TITLE=1
|
TITLE=1
|
||||||
@@ -47,3 +49,10 @@ SUPERLINE=
|
|||||||
EMPTY=
|
EMPTY=
|
||||||
= flase
|
= flase
|
||||||
asfasdf
|
asfasdf
|
||||||
|
# BLOCK TESTS
|
||||||
|
[Var Test]
|
||||||
|
TEST="Block 1 D"
|
||||||
|
[OtherSet]
|
||||||
|
TEST="Block 2 D"
|
||||||
|
[Ignore-Invalid-Block]
|
||||||
|
TEST="Block 3 D"
|
||||||
|
|||||||
Reference in New Issue
Block a user