Skip to content

Commit

Permalink
Merge pull request #13 from greg-1-anderson/server-instead-of-env
Browse files Browse the repository at this point in the history
Fixes #9: Use $_SERVER instead of $_ENV for greater reliability.
  • Loading branch information
grasmash authored Feb 19, 2022
2 parents 84c2561 + ddbf8e1 commit 2c81d9d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/Expander.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ protected function doExpandArrayProperties(
// Recursive case.
if (is_array($value)) {
$this->doExpandArrayProperties($data, $value, $parent_keys . "$key.", $reference_data);
} // Base case.
else {
} else {
// Base case.
$this->expandStringProperties($data, $parent_keys, $reference_data, $value, $key);
}
}
Expand Down Expand Up @@ -208,8 +208,8 @@ public function expandStringPropertiesCallback(
// Use only values within the subject array's data.
if (!$reference_data) {
return $this->expandProperty($property_name, $unexpanded_value, $data);
} // Search both the subject array's data and the reference data for a value.
else {
} else {
// Search both the subject array's data and the reference data for a value.
return $this->expandPropertyWithReferenceData(
$property_name,
$unexpanded_value,
Expand Down Expand Up @@ -276,8 +276,8 @@ public function expandProperty($property_name, $unexpanded_value, $data)
if (strpos($property_name, "env.") === 0 &&
!$data->has($property_name)) {
$env_key = substr($property_name, 4);
if (isset($_ENV[$env_key])) {
$data->set($property_name, $_ENV[$env_key]);
if (isset($_SERVER[$env_key])) {
$data->set($property_name, $_SERVER[$env_key]);
}
}

Expand Down
9 changes: 8 additions & 1 deletion tests/phpunit/ExpanderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public function testExpandArrayProperties(array $array, array $reference_array)
{
$expander = new Expander();

putenv("test=gomjabbar");
$this->setEnvVarFixture('test', 'gomjabbar');

$expanded = $expander->expandArrayProperties($array);
$this->assertEquals('gomjabbar', $expanded['env-test']);
$this->assertEquals('Frank Herbert 1965', $expanded['book']['copyright']);
Expand Down Expand Up @@ -126,4 +127,10 @@ public function providerTestExpandProperty()
[ ['book' => ['author' => 'Frank Herbert' ]], 'book.author', '${book.author}', 'Frank Herbert' ],
];
}

protected function setEnvVarFixture($key, $value)
{
putenv("$key=$value");
$_SERVER[$key] = $value;
}
}

0 comments on commit 2c81d9d

Please sign in to comment.