diff --git a/SymfonyRuntime.php b/SymfonyRuntime.php index 8e1cc39..bb2ad64 100644 --- a/SymfonyRuntime.php +++ b/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/Tests/phpt/dotenv_overload_command_debug_exists_0_to_1.php b/Tests/phpt/dotenv_overload_command_debug_exists_0_to_1.php new file mode 100644 index 0000000..a2e5f5a --- /dev/null +++ b/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/Tests/phpt/dotenv_overload_command_debug_exists_0_to_1.phpt b/Tests/phpt/dotenv_overload_command_debug_exists_0_to_1.phpt new file mode 100644 index 0000000..a5f785a --- /dev/null +++ b/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/Tests/phpt/dotenv_overload_command_debug_exists_1_to_0.php b/Tests/phpt/dotenv_overload_command_debug_exists_1_to_0.php new file mode 100644 index 0000000..bb2df96 --- /dev/null +++ b/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/Tests/phpt/dotenv_overload_command_debug_exists_1_to_0.phpt b/Tests/phpt/dotenv_overload_command_debug_exists_1_to_0.phpt new file mode 100644 index 0000000..24ff962 --- /dev/null +++ b/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/Tests/phpt/dotenv_overload_command_env_exists.php b/Tests/phpt/dotenv_overload_command_env_exists.php new file mode 100644 index 0000000..2bb3593 --- /dev/null +++ b/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/Tests/phpt/dotenv_overload_command_env_exists.phpt b/Tests/phpt/dotenv_overload_command_env_exists.phpt new file mode 100644 index 0000000..36c8644 --- /dev/null +++ b/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