From a2e289c69fb75145f21f81e87f60bbfd0cf22d28 Mon Sep 17 00:00:00 2001 From: Lewis Voncken Date: Tue, 4 Jun 2019 20:47:22 +0200 Subject: [PATCH 1/3] [TASK] set env variables based on $_SERVER --- src/Context.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Context.php b/src/Context.php index a22286a..ae56696 100644 --- a/src/Context.php +++ b/src/Context.php @@ -64,7 +64,7 @@ public static function fromRequest(Request $request): self 'query' => $request->getQuery()->toArray(), ], ], - 'env' => getenv(), + 'env' => $_SERVER, ]); } From 2af14889f7fa3124611f3faa2dc81642cd13a3e6 Mon Sep 17 00:00:00 2001 From: Lewis Voncken Date: Thu, 6 Jun 2019 01:03:24 +0200 Subject: [PATCH 2/3] [TASK] Updated test to use $_SERVER and not getenv --- dev/bootstrap-controller.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dev/bootstrap-controller.php b/dev/bootstrap-controller.php index 8bfd347..b0eaab6 100644 --- a/dev/bootstrap-controller.php +++ b/dev/bootstrap-controller.php @@ -16,7 +16,10 @@ exit(1); } -$upwardConfig = getenv('UPWARD_PHP_UPWARD_PATH'); +if (empty($_SERVER['UPWARD_PHP_UPWARD_PATH']) || !isset($_SERVER['UPWARD_PHP_UPWARD_PATH'])) { + $_SERVER = array_merge(getenv(), $_SERVER); +} +$upwardConfig = $_SERVER['UPWARD_PHP_UPWARD_PATH']; if (!$upwardConfig) { echo 'No path to UPWARD YAML file provided.' . PHP_EOL; exit(1); From 267234122c2436bbb6cd6a8c3bc16dc7f1157267 Mon Sep 17 00:00:00 2001 From: Tommy Wiebell Date: Thu, 6 Jun 2019 10:08:17 -0500 Subject: [PATCH 3/3] - Cleanup logic for environment varibale merge case - Fix failing unit test --- dev/bootstrap-controller.php | 5 +++-- test/ContextTest.php | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/dev/bootstrap-controller.php b/dev/bootstrap-controller.php index b0eaab6..1e0b169 100644 --- a/dev/bootstrap-controller.php +++ b/dev/bootstrap-controller.php @@ -16,10 +16,11 @@ exit(1); } -if (empty($_SERVER['UPWARD_PHP_UPWARD_PATH']) || !isset($_SERVER['UPWARD_PHP_UPWARD_PATH'])) { +// populate the $_SERVER superglobal with environment variables if we're running with PHP's built-in server +if (PHP_SAPI === 'cli-server') { $_SERVER = array_merge(getenv(), $_SERVER); } -$upwardConfig = $_SERVER['UPWARD_PHP_UPWARD_PATH']; +$upwardConfig = $_SERVER['UPWARD_PHP_UPWARD_PATH'] ?? null; if (!$upwardConfig) { echo 'No path to UPWARD YAML file provided.' . PHP_EOL; exit(1); diff --git a/test/ContextTest.php b/test/ContextTest.php index 68ac6d2..091e6a1 100644 --- a/test/ContextTest.php +++ b/test/ContextTest.php @@ -68,8 +68,8 @@ public function testFromRequest(): void $request->shouldReceive('getUri->getQuery') ->andReturn('query=query-value'); - putenv('ENV_NAME=envValue'); - $subject = Context::fromRequest($request); + $_SERVER['ENV_NAME'] = 'envValue'; + $subject = Context::fromRequest($request); verify($subject->get('request.headers.Request-Header'))->is()->sameAs('header value'); verify($subject->get('request.headerEntries.0.key'))->is()->sameAs('Request-Header');