-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8c44ff1
Showing
7 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
composer.lock | ||
composer.phar | ||
vendor/ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2021 Mohammad Ali Ghorbani Delijani | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Dot Env Resolver Variables | ||
|
||
|
||
## Install | ||
|
||
Via Composer | ||
```bash | ||
$ composer require alireaza/dot-env-resolver-variables | ||
``` | ||
|
||
|
||
## Usage | ||
|
||
```php | ||
use AliReaza\DotEnv\DotEnv; | ||
use AliReaza\DotEnv\Resolver\Variables; | ||
|
||
$env = new DotEnv('.env', [ | ||
new Variables($_SERVER + $_ENV), | ||
]); | ||
$env->toArray(); // Array of variables defined in .env | ||
``` | ||
|
||
|
||
## License | ||
|
||
The MIT License (MIT). Please see [License File](LICENSE) for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"name": "alireaza/dot-env-resolver-variables", | ||
"version": "1.0.0", | ||
"type": "library", | ||
"license": "MIT", | ||
"description": "This is Variables Resolver package for DotEnv.", | ||
"keywords": [ | ||
"alireaza", | ||
"env", | ||
"environment", | ||
"resolver", | ||
"variables" | ||
], | ||
"homepage": "https://gitlab.com/AliReaza/dot-env-resolver-variables", | ||
"authors": [ | ||
{ | ||
"name": "Mohammad Ali Ghorbani Delijani", | ||
"homepage": "https://www.alireaza.ir", | ||
"role": "Developer", | ||
"email": "alireazamagh@gmail.com" | ||
} | ||
], | ||
"require": { | ||
"php": "^8.0.0", | ||
"alireaza/dot-env": "1.0.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "9.5.9" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"AliReaza\\DotEnv\\Resolver\\": "./src" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"AliReaza\\Tests\\DotEnv\\Resolver\\": "./tests/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/schema/9.2.xsd" | ||
processIsolation="true" | ||
cacheResult="false" | ||
colors="true" | ||
testdox="true"> | ||
<testsuites> | ||
<testsuite name="Unit"> | ||
<!-- Testing smallest units or modules individually. --> | ||
<!-- The unit test ensures that individual code i.e. small piece of code works fine. --> | ||
<directory suffix="Test.php">./tests/Unit/</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
namespace AliReaza\DotEnv\Resolver; | ||
|
||
use LogicException; | ||
|
||
/** | ||
* Class Variables | ||
* | ||
* @package AliReaza\DotEnv\Resolver | ||
*/ | ||
class Variables | ||
{ | ||
protected array $variables; | ||
|
||
/** | ||
* Variables constructor. | ||
* | ||
* @param array $variables | ||
*/ | ||
public function __construct(array $variables = []) | ||
{ | ||
$this->variables = $variables; | ||
} | ||
|
||
/** | ||
* @param string $data | ||
* @param array $env | ||
* | ||
* @return string | ||
*/ | ||
public function __invoke(string $data, array $env): string | ||
{ | ||
if (str_contains($data, '$')) { | ||
$regex = '/ | ||
(?<!\\\\) | ||
(?P<backslashes>\\\\*) # escaped with a backslash? | ||
\$ | ||
(?!\() # no opening parenthesis | ||
(?P<opening_brace>\{)? # optional opening brace | ||
(?P<key>(?i:[A-Z][A-Z0-9_]*+))? # key | ||
(?P<default_value>:[-=][^\}]++)? # optional default value | ||
(?P<closing_brace>\})? # optional closing brace | ||
/x'; | ||
|
||
$data = preg_replace_callback($regex, function ($matches) use ($env): string { | ||
// odd number of backslashes means the $ character is escaped | ||
if (strlen($matches['backslashes']) % 2 === 1) { | ||
return substr($matches[0], 1); | ||
} | ||
|
||
// unescaped $ not followed by variable name | ||
if (!isset($matches['key'])) { | ||
return $matches[0]; | ||
} | ||
|
||
if ($matches['opening_brace'] === '{' && !isset($matches['closing_brace'])) { | ||
throw new LogicException('Unclosed braces on variable expansion: ' . $matches[0]); | ||
} | ||
|
||
$key = $matches['key']; | ||
|
||
$value = ''; | ||
|
||
if (isset($matches['default_value']) && $matches['default_value'] !== '') { | ||
$unsupportedChars = strpbrk($matches['default_value'], '\'"{'); | ||
if ($unsupportedChars !== false) { | ||
throw new LogicException(sprintf('Unsupported character "%s" found in the default value of variable "%s".', $unsupportedChars[0], $key)); | ||
} | ||
|
||
$value = substr($matches['default_value'], 2); | ||
$value = $this($value, $env); | ||
} | ||
|
||
if (isset($env[$key])) { | ||
$value = $env[$key]; | ||
} else if (isset($this->variables[$key])) { | ||
$value = $this->variables[$key]; | ||
} | ||
|
||
if (!$matches['opening_brace'] && isset($matches['closing_brace'])) { | ||
$value .= '}'; | ||
} | ||
|
||
return $matches['backslashes'] . $value; | ||
}, $data); | ||
} | ||
|
||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AliReaza\Tests\DotEnv\Resolver\Unit; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class VariablesTest extends TestCase | ||
{ | ||
public function test_Coming_Soon() | ||
{ | ||
$this->markTestIncomplete('This test has not been implemented yet.'); | ||
} | ||
} |