Skip to content

Commit e08ad9c

Browse files
HMRDevilnicolas-grekas
authored andcommitted
show overridden vars too
1 parent a4dc5f7 commit e08ad9c

File tree

2 files changed

+57
-46
lines changed

2 files changed

+57
-46
lines changed

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

Lines changed: 53 additions & 45 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;
@@ -29,7 +30,6 @@ final class DebugCommand extends Command
2930

3031
private $kernelEnvironment;
3132
private $projectDirectory;
32-
3333
public function __construct(string $kernelEnvironment, string $projectDirectory)
3434
{
3535
$this->kernelEnvironment = $kernelEnvironment;
@@ -49,97 +49,105 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4949
return 1;
5050
}
5151

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

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

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.');
60+
if (is_file($filePath) && is_file(sprintf('%s.dist', $filePath))) {
61+
$io->warning(sprintf('The file %s.dist gets skipped due to the existence of %1$s.', $this->getRelativeName($filePath)));
6362
}
6463

6564
$io->section('Scanned Files (in descending priority)');
66-
$io->listing(array_map(static function (string $envFile) use ($availableFiles) {
65+
$io->listing(array_map(function (string $envFile) use ($availableFiles) {
6766
return \in_array($envFile, $availableFiles, true)
68-
? sprintf('<fg=green>✓</> %s', $envFile)
69-
: sprintf('<fg=red>⨯</> %s', $envFile);
70-
}, $envFiles));
67+
? sprintf('<fg=green>✓</> %s', $this->getRelativeName($envFile))
68+
: sprintf('<fg=red>⨯</> %s', $this->getRelativeName($envFile));
69+
}, $envFiles));
70+
71+
$variables = $this->getVariables($availableFiles);
7172

7273
$io->section('Variables');
7374
$io->table(
74-
array_merge(['Variable', 'Value'], $availableFiles),
75-
$this->getVariables($availableFiles)
75+
array_merge(['Variable', 'Value'], array_map([$this, 'getRelativeName'], $availableFiles)),
76+
$variables
7677
);
7778

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

8081
return 0;
8182
}
8283

8384
private function getVariables(array $envFiles): array
8485
{
85-
$dotenvVars = $_SERVER['SYMFONY_DOTENV_VARS'] ?? '';
86+
$variables = [];
87+
$fileValues = [];
88+
$dotenvVars = array_flip(explode(',', $_SERVER['SYMFONY_DOTENV_VARS'] ?? ''));
8689

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

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

94-
$output = [];
95-
$fileValues = [];
96-
foreach ($vars as $var) {
97-
$realValue = $_SERVER[$var];
98-
$varDetails = [$var, $realValue];
99100
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;
101+
if (null === $value = $fileValues[$envFile][$var] ?? null) {
102+
$varDetails[] = '<fg=yellow>n/a</>';
103+
continue;
104+
}
105+
106+
$shortenedValue = OutputFormatter::escape($this->getHelper('formatter')->truncate($value, 30));
107+
$varDetails[] = $value === $realValue && !$varSeen ? '<fg=green>'.$shortenedValue.'</>' : $shortenedValue;
108+
$varSeen = $varSeen || $value === $realValue;
105109
}
106110

107-
$output[] = $varDetails;
111+
$variables[$var] = $varDetails;
108112
}
109113

110-
return $output;
114+
ksort($variables);
115+
116+
return $variables;
111117
}
112118

113-
private function getEnvFiles(): array
119+
private function getEnvFiles(string $filePath): array
114120
{
115121
$files = [
116-
'.env.local.php',
117-
sprintf('.env.%s.local', $this->kernelEnvironment),
118-
sprintf('.env.%s', $this->kernelEnvironment),
122+
sprintf('%s.local.php', $filePath),
123+
sprintf('%s.%s.local', $filePath, $this->kernelEnvironment),
124+
sprintf('%s.%s', $filePath, $this->kernelEnvironment),
119125
];
120126

121127
if ('test' !== $this->kernelEnvironment) {
122-
$files[] = '.env.local';
128+
$files[] = sprintf('%s.local', $filePath);
123129
}
124130

125-
if (!is_file($this->getFilePath('.env')) && is_file($this->getFilePath('.env.dist'))) {
126-
$files[] = '.env.dist';
131+
if (!is_file($filePath) && is_file(sprintf('%s.dist', $filePath))) {
132+
$files[] = sprintf('%s.dist', $filePath);
127133
} else {
128-
$files[] = '.env';
134+
$files[] = $filePath;
129135
}
130136

131137
return $files;
132138
}
133139

134-
private function getFilePath(string $file): string
140+
private function getRelativeName(string $filePath): string
135141
{
136-
return $this->projectDirectory.\DIRECTORY_SEPARATOR.$file;
142+
if (str_starts_with($filePath, $this->projectDirectory)) {
143+
return substr($filePath, \strlen($this->projectDirectory) + 1);
144+
}
145+
146+
return basename($filePath);
137147
}
138148

139-
private function loadValues(string $file): array
149+
private function loadValues(string $filePath): array
140150
{
141-
$filePath = $this->getFilePath($file);
142-
143151
if (str_ends_with($filePath, '.php')) {
144152
return include $filePath;
145153
}

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

Lines 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)