Skip to content

Commit 563c6e8

Browse files
authored
Merge pull request #22 from JoshuaBehrens/feature-rewrite-config-files-with-env-calls
Rewrite config files with env calls
2 parents 8c6a166 + 46a3afd commit 563c6e8

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

src/DataWriter/Rewrite.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* - booleans
2020
* - nulls
2121
* - single-dimension arrays
22+
* - default values in env function calls
2223
*
2324
* To do:
2425
* - When an entry does not exist, provide a way to create it
@@ -87,14 +88,15 @@ protected function parseContentValue(string $contents, string $path, $value): st
8788
$replaceValue = $this->writeValueToPhp($value);
8889

8990
$count = 0;
90-
$patterns = [];
91-
$patterns[] = $this->buildStringExpression($key, $items);
92-
$patterns[] = $this->buildStringExpression($key, $items, '"');
93-
$patterns[] = $this->buildConstantExpression($key, $items);
94-
$patterns[] = $this->buildArrayExpression($key, $items);
91+
$patterns = array();
92+
$patterns[$this->buildStringExpression($key, $items)] = '${1}${2}'.$replaceValue;
93+
$patterns[$this->buildStringExpression($key, $items, '"')] = '${1}${2}'.$replaceValue;
94+
$patterns[$this->buildEnvCallExpression($key, $items)] = '${1}${2}${4}' . $replaceValue . '${8}';
95+
$patterns[$this->buildConstantExpression($key, $items)] = '${1}${2}'.$replaceValue;
96+
$patterns[$this->buildArrayExpression($key, $items)] = '${1}${2}'.$replaceValue;
9597

96-
foreach ($patterns as $pattern) {
97-
$result = preg_replace($pattern, '${1}${2}' . $replaceValue, $result, 1, $count);
98+
foreach ($patterns as $pattern => $patternReplacement) {
99+
$result = preg_replace($pattern, $patternReplacement, $result, 1, $count);
98100

99101
if ($count > 0) {
100102
break;
@@ -164,6 +166,22 @@ protected function buildStringExpression(string $targetKey, array $arrayItems =
164166
return '/' . implode('', $expression) . '/';
165167
}
166168

169+
protected function buildEnvCallExpression(string $targetKey, array $arrayItems = [])
170+
{
171+
$expression = array();
172+
173+
// Opening expression for array items ($1)
174+
$expression[] = $this->buildArrayOpeningExpression($arrayItems);
175+
176+
// The target key opening
177+
$expression[] = '(([\'"])' . $targetKey . '\3\s*=>\s*)';
178+
179+
// The method call
180+
$expression[] = '(env\(([\'"]).*\5,\s*)([\'"])(.*)\6(\))';
181+
182+
return '/' . implode('', $expression) . '/';
183+
}
184+
167185
/**
168186
* Common constants only (true, false, null, integers)
169187
*/

tests/Config/RewriteTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@ public function testToContent()
110110

111111
$this->assertArrayHasKey('aNumber', $result);
112112
$this->assertEquals(69, $result['aNumber']);
113+
114+
/*
115+
* Rewrite config with values from env
116+
*/
117+
$contents = $writer->toContent($contents, [
118+
'envMethod' => 'default',
119+
'nestedEnv.envMethod.envChild' => 'default',
120+
]);
121+
$result = eval('?>'.$contents);
122+
123+
$this->assertEquals('default', $result['envMethod']);
124+
$this->assertEquals('default', $result['nestedEnv']['envMethod']['envChild']);
113125
}
114126

115127
}

tests/fixtures/Config/sample-config.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
2323

2424
'aNumber' => 55,
2525

26+
'envMethod' => env('___KEY_FOR_ENV___', "unknown fallback value"),
27+
28+
'nestedEnv' => [
29+
'envMethod' => [
30+
'envChild' => env('___KEY_FOR_CHILD_ENV___', "unknown fallback child value"),
31+
],
32+
],
33+
2634
/*
2735
|--------------------------------------------------------------------------
2836
| Application URL

0 commit comments

Comments
 (0)