15 Commits

Author SHA1 Message Date
b9feacaded Add Block settings to dotenv reader
Now blocks can be set as prefix names for variables via [Block Name] type
grouping.
2024-08-21 11:41:15 +09:00
646456b86b Github actions cache version update 2024-05-22 17:59:16 +09:00
4b0550b8d2 github action cache folder change 2024-05-22 17:54:04 +09:00
efb99f259e Github action phpstan temp folder location 2024-05-22 17:52:33 +09:00
72f4827985 Github actions tmp cache 2024-05-22 17:48:46 +09:00
d5bf24c8cf Github action cache path change 2024-05-22 17:43:14 +09:00
773f40e2d1 Github actions 2024-05-22 17:42:01 +09:00
0bb137dff6 phpunit xml layout update 2024-05-22 17:06:13 +09:00
7e1a19b86b Note about PHPunit test setup 2024-05-22 17:00:40 +09:00
2524092cd8 phpunit tests 2024-05-22 16:55:55 +09:00
f692ca41b1 phpunit test 2024-05-22 16:52:56 +09:00
b9620704bc Github actions update 2024-05-22 16:38:19 +09:00
5004e3c9d8 Github actions add phpunit tests 2024-05-22 16:33:54 +09:00
e86528a366 git actions update 2024-05-22 15:49:10 +09:00
e99a995a2e CI github work flow added 2024-05-22 15:40:20 +09:00
9 changed files with 92 additions and 6 deletions

35
.github/workflows/ci.yml vendored Normal file
View File

@@ -0,0 +1,35 @@
name: CI
run-name: ${{ github.actor}} runs CI
on: [push]
jobs:
ci-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: php-actions/composer@v6
env:
COMPOSER_ROOT_VERSION: dev-master
- name: "Restore result cache"
uses: actions/cache/restore@v4
with:
path: ./tmp
key: "result-cache-v1-${{ matrix.php-version }}-${{ github.run_id }}"
restore-keys: |
result-cache-v1-${{ matrix.php-version }}-
- name: PHPStan Static Analysis
uses: php-actions/phpstan@v3
with:
path: src/
configuration: phpstan.neon
- name: "Save result cache"
uses: actions/cache/save@v4
if: always()
with:
path: ./tmp
key: "result-cache-v1-${{ matrix.php-version }}-${{ github.run_id }}"
# We need to use phpunit from the self install to get the class paths
- name: PHPunit Tests
run: |
vendor/bin/phpunit

View File

@@ -78,7 +78,8 @@ return [
// to parse, but not analyze
"exclude_analysis_directory_list" => [
'vendor',
'test'
'test',
'tmp'
],
'exclude_file_list' => [
],

View File

@@ -61,6 +61,29 @@ DOUBLE="I will be used"
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
### Phan

View File

@@ -1,10 +1,10 @@
# PHP Stan Config
parameters:
tmpDir: /tmp/phpstan-codeblocks-dotenv
tmpDir: %currentWorkingDirectory%/tmp/phpstan-codeblocks-dotenv
level: max
paths:
- %currentWorkingDirectory%
- %currentWorkingDirectory%/src
excludePaths:
- vendor
- test

View File

@@ -2,4 +2,9 @@
colors="true"
verbose="true"
>
<testsuites>
<testsuite name="unit">
<directory>test/phpUnitTests/</directory>
</testsuite>
</testsuites>
</phpunit>

View File

@@ -22,6 +22,9 @@ class DotEnv
* if there are two variables with the same name only the first is used
* 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 $env_file What file to load, default is .env
* @return int -1 other error
@@ -56,10 +59,14 @@ class DotEnv
$status = 1;
$block = false;
$var = '';
$prefix_name = '';
while ($line = fgets($fp)) {
// main match for variable = value part
if (preg_match("/^\s*([\w_.]+)\s*=\s*((\"?).*)/", $line, $matches)) {
$var = $matches[1];
// [] block must be a single line, or it will be ignored
if (preg_match("/^\s*\[([\w_.\s]+)\]/", $line, $matches)) {
$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];
$quotes = $matches[3];
// write only if env is not set yet, and write only the first time

View File

@@ -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_NO_SPACE' => 'Comment at end no quotes no space',
'COMMENT_IN_TEXT_QUOTES' => 'Foo bar # comment in here',
'HAS_EQUAL_NO_QUITES' => 'Is This = Valid',
'HAS_EQUAL_QUITES' => 'Is This = Valid',
'FAILURE' => 'ABC',
'SIMPLEBOX' => 'A B C',
'TITLE' => '1',
@@ -87,6 +89,8 @@ final class DotEnvTest extends TestCase
'__FOOFOO' => 'f ',
123123 => 'number',
'EMPTY' => '',
'Var_Test.TEST' => 'Block 1 D',
'OtherSet.TEST' => 'Block 2 D',
];
// 0: folder relative to test folder, if unset __DIR__
// 1: file, if unset .env

View File

@@ -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_NO_SPACE=Comment at end no quotes no space# Comment NQES
COMMENT_IN_TEXT_QUOTES="Foo bar # comment in here"
HAS_EQUAL_NO_QUITES=Is This = Valid
HAS_EQUAL_QUITES="Is This = Valid"
FAILURE = ABC
SIMPLEBOX= A B C
TITLE=1
@@ -47,3 +49,10 @@ SUPERLINE=
EMPTY=
= flase
asfasdf
# BLOCK TESTS
[Var Test]
TEST="Block 1 D"
[OtherSet]
TEST="Block 2 D"
[Ignore-Invalid-Block]
TEST="Block 3 D"

2
tmp/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
*
!.gitignore