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 b4f9da4

Browse filesBrowse files
bug #54700 [Dotenv] show overridden vars too when running debug:dotenv (HMRDevil)
This PR was merged into the 5.4 branch. Discussion ---------- [Dotenv] show overridden vars too when running debug:dotenv | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #54640 | License | MIT See #54640 Commits ------- ca2040e show overridden vars too
2 parents 9f7fc26 + ca2040e commit b4f9da4
Copy full SHA for b4f9da4

File tree

Expand file treeCollapse file tree

2 files changed

+56
-44
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+56
-44
lines changed

‎src/Symfony/Component/Dotenv/Command/DebugCommand.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Dotenv/Command/DebugCommand.php
+52-43Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Dotenv\Command;
1313

1414
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Formatter\OutputFormatter;
1516
use Symfony\Component\Console\Input\InputInterface;
1617
use Symfony\Component\Console\Output\OutputInterface;
1718
use Symfony\Component\Console\Style\SymfonyStyle;
@@ -49,97 +50,105 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4950
return 1;
5051
}
5152

52-
$envFiles = $this->getEnvFiles();
53-
$availableFiles = array_filter($envFiles, function (string $file) {
54-
return is_file($this->getFilePath($file));
55-
});
53+
$filePath = $this->projectDirectory.\DIRECTORY_SEPARATOR.'.env';
54+
$envFiles = $this->getEnvFiles($filePath);
55+
$availableFiles = array_filter($envFiles, 'is_file');
5656

57-
if (\in_array('.env.local.php', $availableFiles, true)) {
57+
if (\in_array(sprintf('%s.local.php', $filePath), $availableFiles, true)) {
5858
$io->warning('Due to existing dump file (.env.local.php) all other dotenv files are skipped.');
5959
}
6060

61-
if (is_file($this->getFilePath('.env')) && is_file($this->getFilePath('.env.dist'))) {
62-
$io->warning('The file .env.dist gets skipped due to the existence of .env.');
61+
if (is_file($filePath) && is_file(sprintf('%s.dist', $filePath))) {
62+
$io->warning(sprintf('The file %s.dist gets skipped due to the existence of %1$s.', $this->getRelativeName($filePath)));
6363
}
6464

6565
$io->section('Scanned Files (in descending priority)');
66-
$io->listing(array_map(static function (string $envFile) use ($availableFiles) {
66+
$io->listing(array_map(function (string $envFile) use ($availableFiles) {
6767
return \in_array($envFile, $availableFiles, true)
68-
? sprintf('<fg=green>✓</> %s', $envFile)
69-
: sprintf('<fg=red>⨯</> %s', $envFile);
68+
? sprintf('<fg=green>✓</> %s', $this->getRelativeName($envFile))
69+
: sprintf('<fg=red>⨯</> %s', $this->getRelativeName($envFile));
7070
}, $envFiles));
7171

72+
$variables = $this->getVariables($availableFiles);
73+
7274
$io->section('Variables');
7375
$io->table(
74-
array_merge(['Variable', 'Value'], $availableFiles),
75-
$this->getVariables($availableFiles)
76+
array_merge(['Variable', 'Value'], array_map([$this, 'getRelativeName'], $availableFiles)),
77+
$variables
7678
);
7779

78-
$io->comment('Note real values might be different between web and CLI.');
80+
$io->comment('Note that values might be different between web and CLI.');
7981

8082
return 0;
8183
}
8284

8385
private function getVariables(array $envFiles): array
8486
{
85-
$dotenvVars = $_SERVER['SYMFONY_DOTENV_VARS'] ?? '';
87+
$variables = [];
88+
$fileValues = [];
89+
$dotenvVars = array_flip(explode(',', $_SERVER['SYMFONY_DOTENV_VARS'] ?? ''));
8690

87-
if ('' === $dotenvVars) {
88-
return [];
91+
foreach ($envFiles as $envFile) {
92+
$fileValues[$envFile] = $this->loadValues($envFile);
93+
$variables += $fileValues[$envFile];
8994
}
9095

91-
$vars = explode(',', $dotenvVars);
92-
sort($vars);
96+
foreach ($variables as $var => $varDetails) {
97+
$realValue = $_SERVER[$var] ?? '';
98+
$varDetails = [$var, '<fg=green>'.OutputFormatter::escape($realValue).'</>'];
99+
$varSeen = !isset($dotenvVars[$var]);
93100

94-
$output = [];
95-
$fileValues = [];
96-
foreach ($vars as $var) {
97-
$realValue = $_SERVER[$var];
98-
$varDetails = [$var, $realValue];
99101
foreach ($envFiles as $envFile) {
100-
$values = $fileValues[$envFile] ?? $fileValues[$envFile] = $this->loadValues($envFile);
101-
102-
$varString = $values[$var] ?? '<fg=yellow>n/a</>';
103-
$shortenedVar = $this->getHelper('formatter')->truncate($varString, 30);
104-
$varDetails[] = $varString === $realValue ? '<fg=green>'.$shortenedVar.'</>' : $shortenedVar;
102+
if (null === $value = $fileValues[$envFile][$var] ?? null) {
103+
$varDetails[] = '<fg=yellow>n/a</>';
104+
continue;
105+
}
106+
107+
$shortenedValue = OutputFormatter::escape($this->getHelper('formatter')->truncate($value, 30));
108+
$varDetails[] = $value === $realValue && !$varSeen ? '<fg=green>'.$shortenedValue.'</>' : $shortenedValue;
109+
$varSeen = $varSeen || $value === $realValue;
105110
}
106111

107-
$output[] = $varDetails;
112+
$variables[$var] = $varDetails;
108113
}
109114

110-
return $output;
115+
ksort($variables);
116+
117+
return $variables;
111118
}
112119

113-
private function getEnvFiles(): array
120+
private function getEnvFiles(string $filePath): array
114121
{
115122
$files = [
116-
'.env.local.php',
117-
sprintf('.env.%s.local', $this->kernelEnvironment),
118-
sprintf('.env.%s', $this->kernelEnvironment),
123+
sprintf('%s.local.php', $filePath),
124+
sprintf('%s.%s.local', $filePath, $this->kernelEnvironment),
125+
sprintf('%s.%s', $filePath, $this->kernelEnvironment),
119126
];
120127

121128
if ('test' !== $this->kernelEnvironment) {
122-
$files[] = '.env.local';
129+
$files[] = sprintf('%s.local', $filePath);
123130
}
124131

125-
if (!is_file($this->getFilePath('.env')) && is_file($this->getFilePath('.env.dist'))) {
126-
$files[] = '.env.dist';
132+
if (!is_file($filePath) && is_file(sprintf('%s.dist', $filePath))) {
133+
$files[] = sprintf('%s.dist', $filePath);
127134
} else {
128-
$files[] = '.env';
135+
$files[] = $filePath;
129136
}
130137

131138
return $files;
132139
}
133140

134-
private function getFilePath(string $file): string
141+
private function getRelativeName(string $filePath): string
135142
{
136-
return $this->projectDirectory.\DIRECTORY_SEPARATOR.$file;
143+
if (str_starts_with($filePath, $this->projectDirectory)) {
144+
return substr($filePath, \strlen($this->projectDirectory) + 1);
145+
}
146+
147+
return basename($filePath);
137148
}
138149

139-
private function loadValues(string $file): array
150+
private function loadValues(string $filePath): array
140151
{
141-
$filePath = $this->getFilePath($file);
142-
143152
if (str_ends_with($filePath, '.php')) {
144153
return include $filePath;
145154
}

‎src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@ public function testEmptyDotEnvVarsList()
5252
---------- ------- ------------ ------%S
5353
Variable Value .env.local .env%S
5454
---------- ------- ------------ ------%S
55+
FOO baz bar%S
56+
TEST123 n/a true%S
57+
---------- ------- ------------ ------%S
5558
56-
// Note real values might be different between web and CLI.%S
59+
// Note that values might be different between web and CLI.%S
5760
%a
5861
OUTPUT;
5962

0 commit comments

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