Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 20111d2

Browse filesBrowse files
bug #45426 [Runtime] Fix dotenv_overload with commands (fancyweb)
This PR was merged into the 5.4 branch. Discussion ---------- [Runtime] Fix dotenv_overload with commands | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | - | Tickets | - | License | MIT | Doc PR | - Fixes bugs introduced in #44997. For example, if `$_SERVER['APP_ENV'] = 'foo';` exists and with `APP_ENV=prod` in `.env`, any command will throw `Cannot use "--env" or "-e" when the...` because `foo` is considered as the input env. Determining the input env and debug from `$_SERVER` is wrong because when the `--no-debug` and `--env` are not used, we actually consider the existing env and debug as the input end and debug. We need to know and use the real input env and debug. Commits ------- 4075269 [Runtime] Fix dotenv_overload with commands
2 parents 3784dbc + 4075269 commit 20111d2
Copy full SHA for 20111d2
Expand file treeCollapse file tree

7 files changed

+109
-7
lines changed

‎src/Symfony/Component/Runtime/SymfonyRuntime.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Runtime/SymfonyRuntime.php
+7-7Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,24 +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;
103101
}
104102

105103
if (!($options['disable_dotenv'] ?? false) && isset($options['project_dir']) && !class_exists(MissingDotenv::class, false)) {
106104
(new Dotenv($envKey, $debugKey))
107105
->setProdEnvs((array) ($options['prod_envs'] ?? ['prod']))
108106
->usePutenv($options['use_putenv'] ?? 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]) {
107+
->bootEnv($options['project_dir'].'/'.($options['dotenv_path'] ?? '.env'), 'dev', (array) ($options['test_envs'] ?? ['test']), $options['dotenv_overload'] ?? false);
108+
109+
if ($this->input && ($options['dotenv_overload'] ?? false)) {
110+
if ($this->input->getParameterOption(['--env', '-e'], $_SERVER[$envKey], true) !== $_SERVER[$envKey]) {
112111
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));
113112
}
114113

115-
if (isset($inputDebug) && $inputDebug !== $_SERVER[$debugKey]) {
116-
putenv($debugKey.'='.$_SERVER[$debugKey] = $_ENV[$debugKey] = $inputDebug);
114+
if ($_SERVER[$debugKey] && $this->input->hasParameterOption('--no-debug', true)) {
115+
putenv($debugKey.'='.$_SERVER[$debugKey] = $_ENV[$debugKey] = '0');
117116
}
118117
}
118+
119119
$options['debug'] ?? $options['debug'] = '1' === $_SERVER[$debugKey];
120120
$options['disable_dotenv'] = true;
121121
} else {
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Command\Command;
4+
use Symfony\Component\Console\Output\OutputInterface;
5+
6+
$_SERVER['DEBUG_ENABLED'] = '0';
7+
$_SERVER['APP_RUNTIME_OPTIONS'] = [
8+
'debug_var_name' => 'DEBUG_ENABLED',
9+
'dotenv_overload' => true,
10+
];
11+
12+
require __DIR__.'/autoload.php';
13+
14+
return static function (Command $command, OutputInterface $output, array $context): Command {
15+
return $command->setCode(static function () use ($output, $context): void {
16+
$output->writeln($context['DEBUG_ENABLED']);
17+
});
18+
};
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Test Dotenv overload with a command when debug=0 exists and debug=1 in .env and the --no-debug option is not used
3+
--INI--
4+
display_errors=1
5+
--FILE--
6+
<?php
7+
8+
$_SERVER['argv'] = [
9+
'my_app',
10+
];
11+
$_SERVER['argc'] = 1;
12+
require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/dotenv_overload_command_debug_exists_0_to_1.php';
13+
14+
?>
15+
--EXPECTF--
16+
1
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Command\Command;
4+
use Symfony\Component\Console\Output\OutputInterface;
5+
6+
$_SERVER['DEBUG_MODE'] = '1';
7+
$_SERVER['APP_RUNTIME_OPTIONS'] = [
8+
'debug_var_name' => 'DEBUG_MODE',
9+
'dotenv_overload' => true,
10+
];
11+
12+
require __DIR__.'/autoload.php';
13+
14+
return static function (Command $command, OutputInterface $output, array $context): Command {
15+
return $command->setCode(static function () use ($output, $context): void {
16+
$output->writeln($context['DEBUG_MODE']);
17+
});
18+
};
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Test Dotenv overload with a command when debug=1 exists and debug=0 in .env and the --no-debug option is not used
3+
--INI--
4+
display_errors=1
5+
--FILE--
6+
<?php
7+
8+
$_SERVER['argv'] = [
9+
'my_app',
10+
];
11+
$_SERVER['argc'] = 1;
12+
require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/dotenv_overload_command_debug_exists_1_to_0.php';
13+
14+
?>
15+
--EXPECTF--
16+
0
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Command\Command;
4+
use Symfony\Component\Console\Output\OutputInterface;
5+
6+
$_SERVER['ENV_MODE'] = 'notfoo';
7+
$_SERVER['APP_RUNTIME_OPTIONS'] = [
8+
'env_var_name' => 'ENV_MODE',
9+
'dotenv_overload' => true,
10+
];
11+
12+
require __DIR__.'/autoload.php';
13+
14+
return static function (Command $command, OutputInterface $output, array $context): Command {
15+
return $command->setCode(static function () use ($output, $context): void {
16+
$output->writeln($context['ENV_MODE']);
17+
});
18+
};
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Test Dotenv overload with a command when existing env=notfoo and env=foo in .env and the --env option is not used
3+
--INI--
4+
display_errors=1
5+
--FILE--
6+
<?php
7+
8+
$_SERVER['argv'] = [
9+
'my_app',
10+
];
11+
$_SERVER['argc'] = 1;
12+
require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/dotenv_overload_command_env_exists.php';
13+
14+
?>
15+
--EXPECTF--
16+
foo

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.