From 035ac95ceffb08f6f69d3a4ff8c12dd2a2e39372 Mon Sep 17 00:00:00 2001 From: Thomas Calvet Date: Fri, 11 Jun 2021 15:52:54 +0200 Subject: [PATCH] [Runtime] Add dotenv_overload option to SymfonyRuntime to tell Dotenv to override existing vars --- src/Symfony/Component/Runtime/SymfonyRuntime.php | 11 +++++++++-- .../Component/Runtime/Tests/phpt/autoload.php | 3 ++- .../Runtime/Tests/phpt/dotenv_overload.php | 15 +++++++++++++++ .../Runtime/Tests/phpt/dotenv_overload.phpt | 12 ++++++++++++ 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload.php create mode 100644 src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload.phpt diff --git a/src/Symfony/Component/Runtime/SymfonyRuntime.php b/src/Symfony/Component/Runtime/SymfonyRuntime.php index 9df2f5ca4b7b3..183497fe57df3 100644 --- a/src/Symfony/Component/Runtime/SymfonyRuntime.php +++ b/src/Symfony/Component/Runtime/SymfonyRuntime.php @@ -40,6 +40,7 @@ class_exists(MissingDotenv::class, false) || class_exists(Dotenv::class) || clas * - "prod_envs" to define the names of the production envs - defaults to ["prod"]; * - "test_envs" to define the names of the test envs - defaults to ["test"]; * - "use_putenv" to tell Dotenv to set env vars using putenv() (NOT RECOMMENDED.) + * - "dotenv_overload" to tell Dotenv to override existing vars * * When the "debug" / "env" options are not defined, they will fallback to the * "APP_DEBUG" / "APP_ENV" environment variables, and to the "--env|-e" / "--no-debug" @@ -84,6 +85,7 @@ class SymfonyRuntime extends GenericRuntime * use_putenv?: ?bool, * runtimes?: ?array, * error_handler?: string|false, + * dotenv_overload?: ?bool, * } $options */ public function __construct(array $options = []) @@ -96,10 +98,15 @@ public function __construct(array $options = []) } if (!($options['disable_dotenv'] ?? false) && isset($options['project_dir']) && !class_exists(MissingDotenv::class, false)) { - (new Dotenv()) + $fullDotenvPath = $options['project_dir'].'/'.($options['dotenv_path'] ?? '.env'); + ($dotenv = new Dotenv()) ->setProdEnvs((array) ($options['prod_envs'] ?? ['prod'])) ->usePutenv($options['use_putenv'] ?? false) - ->bootEnv($options['project_dir'].'/'.($options['dotenv_path'] ?? '.env'), 'dev', (array) ($options['test_envs'] ?? ['test'])); + ->bootEnv($fullDotenvPath, 'dev', (array) ($options['test_envs'] ?? ['test'])); + if ($options['dotenv_overload'] ?? false) { + $dotenv->overload($fullDotenvPath); + } + $options['debug'] ?? $options['debug'] = '1' === $_SERVER['APP_DEBUG']; $options['disable_dotenv'] = true; } else { diff --git a/src/Symfony/Component/Runtime/Tests/phpt/autoload.php b/src/Symfony/Component/Runtime/Tests/phpt/autoload.php index 78036ad2c494c..4f024bb72333d 100644 --- a/src/Symfony/Component/Runtime/Tests/phpt/autoload.php +++ b/src/Symfony/Component/Runtime/Tests/phpt/autoload.php @@ -2,7 +2,8 @@ use Symfony\Component\Runtime\SymfonyRuntime; -$_SERVER['APP_RUNTIME_OPTIONS'] = [ +$_SERVER['APP_RUNTIME_OPTIONS'] = $_SERVER['APP_RUNTIME_OPTIONS'] ?? []; +$_SERVER['APP_RUNTIME_OPTIONS'] += [ 'project_dir' => __DIR__, ]; diff --git a/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload.php b/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload.php new file mode 100644 index 0000000000000..4bfe6a2dd997b --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload.php @@ -0,0 +1,15 @@ + true, +]; + +require __DIR__.'/autoload.php'; + +return function (Request $request, array $context) { + return new Response('OK Request '.$context['SOME_VAR']); +}; diff --git a/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload.phpt b/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload.phpt new file mode 100644 index 0000000000000..d816c677c14e4 --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload.phpt @@ -0,0 +1,12 @@ +--TEST-- +Test Dotenv overload +--INI-- +display_errors=1 +--FILE-- + +--EXPECTF-- +OK Request foo_bar