8000 bug #44997 [Runtime] Fix --env and --no-debug with dotenv_overload (f… · symfony/symfony@5bb11d5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5bb11d5

Browse files
bug #44997 [Runtime] Fix --env and --no-debug with dotenv_overload (fancyweb)
This PR was merged into the 5.4 branch. Discussion ---------- [Runtime] Fix --env and --no-debug with dotenv_overload | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - With the `dotenv_overload` option enabled, commands `--no-debug` and `--env=prod` options are not honored. Commits ------- 0460f81 [Runtime] Fix --env and --no-debug with dotenv_overload
2 parents 0eeb392 + 0460f81 commit 5bb11d5

File tree

6 files changed

+74
-1
lines changed

6 files changed

+74
-1
lines changed

src/Symfony/Component/Runtime/SymfonyRuntime.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,24 @@ public function __construct(array $options = [])
9898
} elseif (isset($_SERVER['argv']) && class_exists(ArgvInput::class)) {
9999
$this->options = $options;
100100
$this->getInput();
101+
$inputEnv = $_SERVER[$envKey] ?? null;
102+
$inputDebug = $_SERVER[$debugKey] ?? null;
101103
}
102104

103105
if (!($options['disable_dotenv'] ?? false) && isset($options['project_dir']) && !class_exists(MissingDotenv::class, false)) {
104106
(new Dotenv($envKey, $debugKey))
105107
->setProdEnvs((array) ($options['prod_envs'] ?? ['prod']))
106108
->usePutenv($options['use_putenv'] ?? false)
107-
->bootEnv($options['project_dir'].'/'.($options['dotenv_path'] ?? '.env'), 'dev', (array) ($options['test_envs'] ?? ['test']), $options['dotenv_overload'] ?? false);
109+
->bootEnv($options['project_dir'].'/'.($options['dotenv_path'] ?? '.env'), 'dev', (array) ($options['test_envs'] ?? ['test']), $dotenvOverload = $options['dotenv_overload'] ?? false);
110+
if ($dotenvOverload) {
111+
if (isset($inputEnv) && $inputEnv !== $_SERVER[$envKey]) {
112+
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));
113+
}
114+
115+
if (isset($inputDebug) && $inputDebug !== $_SERVER[$debugKey]) {
116+
putenv($debugKey.'='.$_SERVER[$debugKey] = $_ENV[$debugKey] = $inputDebug);
117+
}
118+
}
108119
$options['debug'] ?? $options['debug'] = '1' === $_SERVER[$debugKey];
109120
$options['disable_dotenv'] = true;
110121
} else {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
SOME_VAR=foo_bar
22
ENV_MODE=foo
33
DEBUG_MODE=0
4+
DEBUG_ENABLED=1
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
$_SERVER['APP_RUNTIME_OPTIONS'] = [
4+
'env_var_name' => 'ENV_MODE',
5+
'dotenv_overload' => true,
6+
];
7+
8+
require __DIR__.'/autoload.php';
9+
10+
return static function (): void {};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Test that a command --env option conflicts with the different one defined in .env when the dotenv_overload option is true
3+
--INI--
4+
display_errors=1
5+
--FILE--
6+
<?php
7+
8+
$_SERVER['argv'] = [
9+
'my_app',
10+
'--env=ccc',
11+
];
12+
$_SERVER['argc'] = 2;
13+
require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/dotenv_overload_command_env_conflict.php';
14+
15+
?>
16+
--EXPECTF--
17+
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
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Command\Command;
4+
use Symfony\Component\Console\Output\OutputInterfac 8000 e;
5+
6+
$_SERVER['APP_RUNTIME_OPTIONS'] = [
7+
'debug_var_name' => 'DEBUG_ENABLED',
8+
'dotenv_overload' => true,
9+
];
10+
11+
require __DIR__.'/autoload.php';
12+
13+
return static function (Command $command, OutputInterface $output, array $context): Command {
14+
return $command->setCode(static function () use ($output, $context): void {
15+
$output->writeln($context['DEBUG_ENABLED']);
16+
});
17+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
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
3+
--INI--
4+
display_errors=1
5+
--FILE--
6+
<?php
7+
8+
$_SERVER['argv'] = [
9+
'my_app',
10+
'--no-debug'
11+
];
12+
$_SERVER['argc'] = 2;
13+
require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/dotenv_overload_command_no_debug.php';
14+
15+
?>
16+
--EXPECTF--
17+
0

0 commit comments

Comments
 (0)
0