From c56ac3c635ba1598429063abbd875cd2aa3280d2 Mon Sep 17 00:00:00 2001 From: Greg Anderson Date: Fri, 18 Feb 2022 16:33:58 -0800 Subject: [PATCH 1/2] Use $_SERVER instead of $_ENV for greater reliability. --- src/Expander.php | 4 ++-- tests/phpunit/ExpanderTest.php | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Expander.php b/src/Expander.php index e2dcbf2..e4f4ac9 100644 --- a/src/Expander.php +++ b/src/Expander.php @@ -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]); } } diff --git a/tests/phpunit/ExpanderTest.php b/tests/phpunit/ExpanderTest.php index c0e845b..7437cc2 100644 --- a/tests/phpunit/ExpanderTest.php +++ b/tests/phpunit/ExpanderTest.php @@ -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']); @@ -126,4 +127,11 @@ public function providerTestExpandProperty() [ ['book' => ['author' => 'Frank Herbert' ]], 'book.author', '${book.author}', 'Frank Herbert' ], ]; } + + protected function setEnvVarFixture($key, $value) + { + putenv("$key=$value"); + $_SERVER[$key] = $value; + + } } From ddbf8e1720568be0aca22f020c8fbcb2e554fb62 Mon Sep 17 00:00:00 2001 From: Greg Anderson Date: Fri, 18 Feb 2022 16:42:03 -0800 Subject: [PATCH 2/2] Code style --- src/Expander.php | 10 +++++----- tests/phpunit/ExpanderTest.php | 1 - 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Expander.php b/src/Expander.php index e4f4ac9..1b70597 100644 --- a/src/Expander.php +++ b/src/Expander.php @@ -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); } } @@ -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, @@ -277,7 +277,7 @@ public function expandProperty($property_name, $unexpanded_value, $data) !$data->has($property_name)) { $env_key = substr($property_name, 4); if (isset($_SERVER[$env_key])) { - $data->set($property_name, $_SERVER[$env_key]); + $data->set($property_name, $_SERVER[$env_key]); } } diff --git a/tests/phpunit/ExpanderTest.php b/tests/phpunit/ExpanderTest.php index 7437cc2..a23a8fe 100644 --- a/tests/phpunit/ExpanderTest.php +++ b/tests/phpunit/ExpanderTest.php @@ -132,6 +132,5 @@ protected function setEnvVarFixture($key, $value) { putenv("$key=$value"); $_SERVER[$key] = $value; - } }