From 880893206fd43fd256f2ad583d349e9e00ab9af1 Mon Sep 17 00:00:00 2001 From: Geoffrey Bachelet Date: Tue, 28 Apr 2020 19:01:56 -0400 Subject: [PATCH] Fallback to configured environment when no server/env environment is found --- .../configuration/configuring_application_kernel.feature | 1 + src/ServiceContainer/SymfonyExtension.php | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/features/configuration/configuring_application_kernel.feature b/features/configuration/configuring_application_kernel.feature index fe11a19..c8fb9c2 100644 --- a/features/configuration/configuring_application_kernel.feature +++ b/features/configuration/configuring_application_kernel.feature @@ -81,6 +81,7 @@ Feature: Configuring application kernel Feature: Scenario: Then the application kernel should have environment "custom" + And the server and environment variable "APP_ENV" is "custom" """ When I run Behat Then it should pass diff --git a/src/ServiceContainer/SymfonyExtension.php b/src/ServiceContainer/SymfonyExtension.php index e27a8e1..7431483 100644 --- a/src/ServiceContainer/SymfonyExtension.php +++ b/src/ServiceContainer/SymfonyExtension.php @@ -71,7 +71,7 @@ final class SymfonyExtension implements Extension public function load(ContainerBuilder $container, array $config): void { - $this->fallbackToTestEnvironment(); + $this->setupTestEnvironment($config['kernel']['environment'] ?? 'test'); $this->loadBootstrap($this->autodiscoverBootstrap($config['bootstrap'])); @@ -182,11 +182,11 @@ final class SymfonyExtension implements Extension require_once $bootstrap; } - private function fallbackToTestEnvironment(): void + private function setupTestEnvironment(string $fallback): void { - // If there's no defined server / environment variable with an environment, default to test + // If there's no defined server / environment variable with an environment, default to configured fallback if (($_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? null) === null) { - putenv('APP_ENV=' . $_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = 'test'); + putenv('APP_ENV=' . $_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = $fallback); } }