Skip to content

Commit 59f90a7

Browse files
committed
fix(Parser): implement variable interpolation in DefaultParser
- Add interpolateValue method to DefaultParser - Update parse method to perform interpolation after initial parsing - Modify Dotenv class to utilize updated parsing logic This commit addresses the issue with variable interpolation, specifically resolving the problem with KARIRI_MAIL_FROM_NAME. The changes include: - Implementing a new interpolateValue method in DefaultParser to handle variable substitution using ${VAR_NAME} syntax - Updating the parse method to perform interpolation as a separate step after all variables are initially parsed - Adjusting the Dotenv class to work with the new parsing approach
1 parent 5543d2b commit 59f90a7

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

src/Parser/DefaultParser.php

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class DefaultParser implements Parser
1313
private const SETTER_CHAR = '=';
1414
private const INVALID_NAME_CHARS = '{}()[]';
1515

16+
private array $parsedValues = [];
17+
1618
public function __construct(
1719
private readonly bool $strictMode = false
1820
) {
@@ -21,15 +23,19 @@ public function __construct(
2123
public function parse(string $content): array
2224
{
2325
$lines = $this->splitLines($content);
24-
25-
return array_reduce($lines, function (array $output, string $line) {
26+
foreach ($lines as $line) {
2627
if ($this->isValidSetter($line)) {
2728
[$key, $value] = $this->parseEnvironmentVariable($line);
28-
$output[$key] = $value;
29+
$this->parsedValues[$key] = $value;
2930
}
31+
}
32+
33+
// Perform interpolation after all variables are parsed
34+
foreach ($this->parsedValues as $key => $value) {
35+
$this->parsedValues[$key] = $this->interpolateValue($value);
36+
}
3037

31-
return $output;
32-
}, []);
38+
return $this->parsedValues;
3339
}
3440

3541
private function splitLines(string $content): array
@@ -58,7 +64,7 @@ private function parseEnvironmentVariable(string $line): array
5864
{
5965
[$name, $value] = explode(self::SETTER_CHAR, $line, 2);
6066
$name = trim($name);
61-
$value = $this->interpolateValue(trim($value));
67+
$value = trim($value);
6268

6369
$this->validateVariableName($name);
6470

@@ -67,11 +73,13 @@ private function parseEnvironmentVariable(string $line): array
6773

6874
private function validateVariableName(string $name): void
6975
{
70-
match (true) {
71-
'' === $name => throw new InvalidValueException('Empty variable name'),
72-
$this->strictMode && $this->containsInvalidCharacters($name) => throw new InvalidValueException('Invalid character in variable name'),
73-
default => null,
74-
};
76+
if ('' === $name) {
77+
throw new InvalidValueException('Empty variable name');
78+
}
79+
80+
if ($this->strictMode && $this->containsInvalidCharacters($name)) {
81+
throw new InvalidValueException('Invalid character in variable name');
82+
}
7583
}
7684

7785
private function containsInvalidCharacters(string $name): bool
@@ -81,10 +89,10 @@ private function containsInvalidCharacters(string $name): bool
8189

8290
private function interpolateValue(string $value): string
8391
{
84-
return preg_replace_callback(
85-
'/\$\{([A-Z0-9_]+)\}/',
86-
fn ($matches) => $_ENV[$matches[1]] ?? $matches[0],
87-
$value
88-
);
92+
return preg_replace_callback('/\${([A-Z0-9_]+)}/', function ($matches) {
93+
$varName = $matches[1];
94+
95+
return $this->parsedValues[$varName] ?? $matches[0];
96+
}, $value);
8997
}
9098
}

tests/application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
$variables = [
1616
'KARIRI_APP_ENV' => 'string',
1717
'KARIRI_APP_NAME' => 'string',
18-
'KARIRI_PHP_VERSION' => 'string',
18+
'KARIRI_PHP_VERSION' => 'double',
1919
'KARIRI_PHP_PORT' => 'integer',
2020
'KARIRI_APP_DEBUG' => 'boolean',
2121
'KARIRI_APP_URL' => 'string',

0 commit comments

Comments
 (0)