diff --git a/CHANGELOG.md b/CHANGELOG.md index 6add618..3f5d90c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ CHANGELOG * The component is not experimental anymore * Add options "env_var_name" and "debug_var_name" to `GenericRuntime` and `SymfonyRuntime` + * Add option "dotenv_overload" to `SymfonyRuntime` 5.3.0 ----- diff --git a/SymfonyRuntime.php b/SymfonyRuntime.php index f957890..149e76b 100644 --- a/SymfonyRuntime.php +++ b/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 * error_handler?: string|false, * env_var_name?: string, * debug_var_name?: string, + * dotenv_overload?: ?bool, * } $options */ public function __construct(array $options = []) @@ -102,7 +104,7 @@ public function __construct(array $options = []) (new Dotenv($envKey, $debugKey)) ->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($options['project_dir'].'/'.($options['dotenv_path'] ?? '.env'), 'dev', (array) ($options['test_envs'] ?? ['test']), $options['dotenv_overload'] ?? false); $options['debug'] ?? $options['debug'] = '1' === $_SERVER[$debugKey]; $options['disable_dotenv'] = true; } else { diff --git a/Tests/phpt/autoload.php b/Tests/phpt/autoload.php index 300e6bc..b109b80 100644 --- a/Tests/phpt/autoload.php +++ b/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__, ] + ($_SERVER['APP_RUNTIME_OPTIONS'] ?? []); diff --git a/Tests/phpt/dotenv_overload.php b/Tests/phpt/dotenv_overload.php new file mode 100644 index 0000000..4bfe6a2 --- /dev/null +++ b/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/Tests/phpt/dotenv_overload.phpt b/Tests/phpt/dotenv_overload.phpt new file mode 100644 index 0000000..eb39d68 --- /dev/null +++ b/Tests/phpt/dotenv_overload.phpt @@ -0,0 +1,14 @@ +--TEST-- +Test Dotenv overload +--SKIPIF-- + (new \ReflectionMethod(\Symfony\Component\Dotenv\Dotenv::class, 'bootEnv'))->getNumberOfParameters()) die('Skip because Dotenv version is too low'); +--INI-- +display_errors=1 +--FILE-- + +--EXPECTF-- +OK Request foo_bar