diff --git a/src/Symfony/Component/Runtime/SymfonyRuntime.php b/src/Symfony/Component/Runtime/SymfonyRuntime.php index fb8b8421133d0..52a8ce860caf2 100644 --- a/src/Symfony/Component/Runtime/SymfonyRuntime.php +++ b/src/Symfony/Component/Runtime/SymfonyRuntime.php @@ -98,13 +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']), $options['dotenv_overload'] ?? 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]) { + 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); + } + } $options['debug'] ?? $options['debug'] = '1' === $_SERVER[$debugKey]; $options['disable_dotenv'] = true; } else { diff --git a/src/Symfony/Component/Runtime/Tests/phpt/.env b/src/Symfony/Component/Runtime/Tests/phpt/.env index 9fd6ab5426972..1d124dd203a81 100644 --- a/src/Symfony/Component/Runtime/Tests/phpt/.env +++ b/src/Symfony/Component/Runtime/Tests/phpt/.env @@ -1,3 +1,4 @@ SOME_VAR=foo_bar ENV_MODE=foo DEBUG_MODE=0 +DEBUG_ENABLED=1 diff --git a/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_env_conflict.php b/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_env_conflict.php new file mode 100644 index 0000000000000..d730e08dbf734 --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_env_conflict.php @@ -0,0 +1,10 @@ + 'ENV_MODE', + 'dotenv_overload' => true, +]; + +require __DIR__.'/autoload.php'; + +return static function (): void {}; diff --git a/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_env_conflict.phpt b/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_env_conflict.phpt new file mode 100644 index 0000000000000..a63acb22de295 --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_env_conflict.phpt @@ -0,0 +1,17 @@ +--TEST-- +Test that a command --env option conflicts with the different one defined in .env when the dotenv_overload option is true +--INI-- +display_errors=1 +--FILE-- + +--EXPECTF-- +Fatal error: Uncaught LogicException: Cannot use "--env" or "-e" when the ".env" file defines "ENV_MODE" and the "dotenv_overload" runtime option is true.%a diff --git a/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_no_debug.php b/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_no_debug.php new file mode 100644 index 0000000000000..fadbabb4fd871 --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_no_debug.php @@ -0,0 +1,17 @@ + '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_no_debug.phpt b/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_no_debug.phpt new file mode 100644 index 0000000000000..4c6ae429901e4 --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload_command_no_debug.phpt @@ -0,0 +1,17 @@ +--TEST-- +Test that a command --no-debug option has a higher priority than the debug value defined in .env when the dotenv_overload option is true +--INI-- +display_errors=1 +--FILE-- + +--EXPECTF-- +0