From 4075269033abcce20c00d129a12c3e06d236e474 Mon Sep 17 00:00:00 2001 From: Thomas Calvet Date: Tue, 15 Feb 2022 14:01:14 +0100 Subject: [PATCH] [Runtime] Fix dotenv_overload with commands --- .../Component/Runtime/SymfonyRuntime.php | 14 +++++++------- ...nv_overload_command_debug_exists_0_to_1.php | 18 ++++++++++++++++++ ...v_overload_command_debug_exists_0_to_1.phpt | 16 ++++++++++++++++ ...nv_overload_command_debug_exists_1_to_0.php | 18 ++++++++++++++++++ ...v_overload_command_debug_exists_1_to_0.phpt | 16 ++++++++++++++++ .../dotenv_overload_command_env_exists.php | 18 ++++++++++++++++++ .../dotenv_overload_command_env_exists.phpt | 16 ++++++++++++++++ 7 files changed, 109 insertions(+), 7 deletions(-) create mode 100644 src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_debug_exists_0_to_1.php create mode 100644 src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_debug_exists_0_to_1.phpt create mode 100644 src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_debug_exists_1_to_0.php create mode 100644 src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_debug_exists_1_to_0.phpt create mode 100644 src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_env_exists.php create mode 100644 src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_env_exists.phpt diff --git a/src/Symfony/Component/Runtime/SymfonyRuntime.php b/src/Symfony/Component/Runtime/SymfonyRuntime.php index 52a8ce860caf2..0ca9713049545 100644 --- a/src/Symfony/Component/Runtime/SymfonyRuntime.php +++ b/src/Symfony/Component/Runtime/SymfonyRuntime.php @@ -98,24 +98,24 @@ public function __construct(array $options = []) } elseif (isset($_SERVER['argv']) && class_exists(ArgvInput::class)) { $this->options = $options; $this->getInput(); - $inputEnv = $_SERVER[$envKey] ?? null; - $inputDebug = $_SERVER[$debugKey] ?? null; } if (!($options['disable_dotenv'] ?? false) && isset($options['project_dir']) && !class_exists(MissingDotenv::class, false)) { (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']), $dotenvOverload = $options['dotenv_overload'] ?? false); - if ($dotenvOverload) { - if (isset($inputEnv) && $inputEnv !== $_SERVER[$envKey]) { + ->bootEnv($options['project_dir'].'/'.($options['dotenv_path'] ?? '.env'), 'dev', (array) ($options['test_envs'] ?? ['test']), $options['dotenv_overload'] ?? false); + + if ($this->input && ($options['dotenv_overload'] ?? false)) { + if ($this->input->getParameterOption(['--env', '-e'], $_SERVER[$envKey], true) !== $_SERVER[$envKey]) { throw new \LogicException(sprintf('Cannot use "--env" or "-e" when the "%s" file defines "%s" and the "dotenv_overload" runtime option is true.', $options['dotenv_path'] ?? '.env', $envKey)); } - if (isset($inputDebug) && $inputDebug !== $_SERVER[$debugKey]) { - putenv($debugKey.'='.$_SERVER[$debugKey] = $_ENV[$debugKey] = $inputDebug); + if ($_SERVER[$debugKey] && $this->input->hasParameterOption('--no-debug', true)) { + putenv($debugKey.'='.$_SERVER[$debugKey] = $_ENV[$debugKey] = '0'); } } + $options['debug'] ?? $options['debug'] = '1' === $_SERVER[$debugKey]; $options['disable_dotenv'] = true; } else { diff --git a/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_debug_exists_0_to_1.php b/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_debug_exists_0_to_1.php new file mode 100644 index 0000000000000..a2e5f5a6cb7b7 --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_debug_exists_0_to_1.php @@ -0,0 +1,18 @@ + 'DEBUG_ENABLED', + 'dotenv_overload' => true, +]; + +require __DIR__.'/autoload.php'; + +return static function (Command $command, OutputInterface $output, array $context): Command { + return $command->setCode(static function () use ($output, $context): void { + $output->writeln($context['DEBUG_ENABLED']); + }); +}; diff --git a/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_debug_exists_0_to_1.phpt b/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_debug_exists_0_to_1.phpt new file mode 100644 index 0000000000000..a5f785ae50f67 --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_debug_exists_0_to_1.phpt @@ -0,0 +1,16 @@ +--TEST-- +Test Dotenv overload with a command when debug=0 exists and debug=1 in .env and the --no-debug option is not used +--INI-- +display_errors=1 +--FILE-- + +--EXPECTF-- +1 diff --git a/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_debug_exists_1_to_0.php b/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_debug_exists_1_to_0.php new file mode 100644 index 0000000000000..bb2df9623e008 --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_debug_exists_1_to_0.php @@ -0,0 +1,18 @@ + 'DEBUG_MODE', + 'dotenv_overload' => true, +]; + +require __DIR__.'/autoload.php'; + +return static function (Command $command, OutputInterface $output, array $context): Command { + return $command->setCode(static function () use ($output, $context): void { + $output->writeln($context['DEBUG_MODE']); + }); +}; diff --git a/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_debug_exists_1_to_0.phpt b/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_debug_exists_1_to_0.phpt new file mode 100644 index 0000000000000..24ff962fea51f --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_debug_exists_1_to_0.phpt @@ -0,0 +1,16 @@ +--TEST-- +Test Dotenv overload with a command when debug=1 exists and debug=0 in .env and the --no-debug option is not used +--INI-- +display_errors=1 +--FILE-- + +--EXPECTF-- +0 diff --git a/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_env_exists.php b/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_env_exists.php new file mode 100644 index 0000000000000..2bb359352de94 --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_env_exists.php @@ -0,0 +1,18 @@ + 'ENV_MODE', + 'dotenv_overload' => true, +]; + +require __DIR__.'/autoload.php'; + +return static function (Command $command, OutputInterface $output, array $context): Command { + return $command->setCode(static function () use ($output, $context): void { + $output->writeln($context['ENV_MODE']); + }); +}; diff --git a/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_env_exists.phpt b/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_env_exists.phpt new file mode 100644 index 0000000000000..36c8644aaa89c --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_env_exists.phpt @@ -0,0 +1,16 @@ +--TEST-- +Test Dotenv overload with a command when existing env=notfoo and env=foo in .env and the --env option is not used +--INI-- +display_errors=1 +--FILE-- + +--EXPECTF-- +foo