8000 [Runtime] Fix dotenv_overload with commands · symfony/symfony@f4266f9 · GitHub
[go: up one dir, main page]

Skip to content

Commit f4266f9

Browse files
committed
[Runtime] Fix dotenv_overload with commands
1 parent 3784dbc commit f4266f9

7 files changed

+117
-5
lines changed

src/Symfony/Component/Runtime/SymfonyRuntime.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ public function __construct(array $options = [])
9797
$_SERVER[$envKey] = $options['env'];
9898
} elseif (isset($_SERVER['argv']) && class_exists(ArgvInput::class)) {
9999
$this->options = $options;
100-
$this->getInput();
101-
$inputEnv = $_SERVER[$envKey] ?? null;
102-
$inputDebug = $_SERVER[$debugKey] ?? null;
100+
$input = $this->getInput();
101+
$inputEnv = $this->getInputEnv($input);
102+
$inputDebug = $this->isInputNoDebug($input) ? '0' : null;
103103
}
104104

105105
if (!($options['disable_dotenv'] ?? false) && isset($options['project_dir']) && !class_exists(MissingDotenv::class, false)) {
@@ -226,14 +226,24 @@ private function getInput(): ArgvInput
226226
return $this->input = $input;
227227
}
228228

229-
if (null !== $env = $input->getParameterOption(['--env', '-e'], null, true)) {
229+
if (null !== $env = $this->getInputEnv($input)) {
230230
putenv($this->options['env_var_name'].'='.$_SERVER[$this->options['env_var_name']] = $_ENV[$this->options['env_var_name']] = $env);
231231
}
232232

233-
if ($input->hasParameterOption('--no-debug', true)) {
233+
if ($this->isInputNoDebug($input)) {
234234
putenv($this->options['debug_var_name'].'='.$_SERVER[$this->options['debug_var_name']] = $_ENV[$this->options['debug_var_name']] = '0');
235235
}
236236

237237
return $this->input = $input;
238238
}
239+
240+
private function getInputEnv(ArgvInput $input): ?string
241+
{
242+
return $input->getParameterOption(['--env', '-e'], null, true);
243+
}
244+
245+
private function isInputNoDebug(ArgvInput $input): bool
246+
{
247+
return $input->hasParameterOption('--no-debug', true);
248+
}
239249
}
Lines 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+
};
Lines 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
Lines 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+
};
Lines 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
Lines 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+
};
Lines 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+
?&g 416B t;
15+
--EXPECTF--
16+
foo

0 commit comments

Comments
 (0)
0