Skip to content

Commit f9de6bc

Browse files
fancywebnicolas-grekas
authored andcommitted
[Runtime] Fix dotenv_overload with commands
1 parent 3784dbc commit f9de6bc

7 files changed

+108
-6
lines changed

src/Symfony/Component/Runtime/SymfonyRuntime.php

Lines changed: 6 additions & 6 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)
109107
->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]) {
108+
109+
if ($dotenvOverload && $this->input) {
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 {
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+
?>
15+
--EXPECTF--
16+
foo

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy