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 f91f18b

Browse filesBrowse files
committed
[Runtime] Fix --env and --no-debug with dotenv_overload
1 parent b41251a commit f91f18b
Copy full SHA for f91f18b

File tree

4 files changed

+51
-1
lines changed
Filter options

4 files changed

+51
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Runtime/SymfonyRuntime.php
+12-1Lines 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']), $overrideExistingVars = $options['dotenv_overload'] ?? false);
110+
if ($overrideExistingVars) {
111+
if (($inputEnv ?? $_SERVER[$envKey]) !== $_SERVER[$envKey]) {
112+
putenv($envKey.'='.$_SERVER[$envKey] = $_ENV[$envKey] = $inputEnv);
113+
}
114+
115+
if (($inputDebug ?? $_SERVER[$debugKey]) !== $_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 {
+1Lines 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
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Command\Command;
4+
use Symfony\Component\Console\Output\OutputInterface;
5+
6+
$_SERVER['APP_RUNTIME_OPTIONS'] = [
7+
'env_var_name' => 'ENV_MODE',
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['ENV_MODE']);
17+
$output->writeln($context['DEBUG_ENABLED']);
18+
});
19+
};
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Test that a command --env and --no-debug options have a higher priority than the dotenv_overload option
3+
--INI--
4+
display_errors=1
5+
--FILE--
6+
<?php
7+
8+
$_SERVER['argv'] = [
9+
'my_app',
10+
'--env=ccc',
11+
'--no-debug'
12+
];
13+
$_SERVER['argc'] = 3;
14+
require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/dotenv_overload_command.php';
15+
16+
?>
17+
--EXPECTF--
18+
ccc
19+
0

0 commit comments

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