Skip to content

Commit

Permalink
show overridden vars too
Browse files Browse the repository at this point in the history
  • Loading branch information
HMRDevil authored and nicolas-grekas committed Apr 29, 2024
1 parent a4dc5f7 commit ca2040e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 44 deletions.
95 changes: 52 additions & 43 deletions src/Symfony/Component/Dotenv/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Dotenv\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
Expand Down Expand Up @@ -49,97 +50,105 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 1;
}

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

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

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

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

$variables = $this->getVariables($availableFiles);

$io->section('Variables');
$io->table(
array_merge(['Variable', 'Value'], $availableFiles),
$this->getVariables($availableFiles)
array_merge(['Variable', 'Value'], array_map([$this, 'getRelativeName'], $availableFiles)),
$variables
);

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

return 0;
}

private function getVariables(array $envFiles): array
{
$dotenvVars = $_SERVER['SYMFONY_DOTENV_VARS'] ?? '';
$variables = [];
$fileValues = [];
$dotenvVars = array_flip(explode(',', $_SERVER['SYMFONY_DOTENV_VARS'] ?? ''));

if ('' === $dotenvVars) {
return [];
foreach ($envFiles as $envFile) {
$fileValues[$envFile] = $this->loadValues($envFile);
$variables += $fileValues[$envFile];
}

$vars = explode(',', $dotenvVars);
sort($vars);
foreach ($variables as $var => $varDetails) {
$realValue = $_SERVER[$var] ?? '';
$varDetails = [$var, '<fg=green>'.OutputFormatter::escape($realValue).'</>'];

Check failure on line 98 in src/Symfony/Component/Dotenv/Command/DebugCommand.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidArgument

src/Symfony/Component/Dotenv/Command/DebugCommand.php:98:71: InvalidArgument: Argument 1 of Symfony\Component\Console\Formatter\OutputFormatter::escape expects string, but non-empty-list<string>|scalar provided (see https://psalm.dev/004)

Check failure on line 98 in src/Symfony/Component/Dotenv/Command/DebugCommand.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidArgument

src/Symfony/Component/Dotenv/Command/DebugCommand.php:98:71: InvalidArgument: Argument 1 of Symfony\Component\Console\Formatter\OutputFormatter::escape expects string, but non-empty-list<string>|scalar provided (see https://psalm.dev/004)
$varSeen = !isset($dotenvVars[$var]);

$output = [];
$fileValues = [];
foreach ($vars as $var) {
$realValue = $_SERVER[$var];
$varDetails = [$var, $realValue];
foreach ($envFiles as $envFile) {
$values = $fileValues[$envFile] ?? $fileValues[$envFile] = $this->loadValues($envFile);

$varString = $values[$var] ?? '<fg=yellow>n/a</>';
$shortenedVar = $this->getHelper('formatter')->truncate($varString, 30);
$varDetails[] = $varString === $realValue ? '<fg=green>'.$shortenedVar.'</>' : $shortenedVar;
if (null === $value = $fileValues[$envFile][$var] ?? null) {
$varDetails[] = '<fg=yellow>n/a</>';
continue;
}

$shortenedValue = OutputFormatter::escape($this->getHelper('formatter')->truncate($value, 30));
$varDetails[] = $value === $realValue && !$varSeen ? '<fg=green>'.$shortenedValue.'</>' : $shortenedValue;
$varSeen = $varSeen || $value === $realValue;
}

$output[] = $varDetails;
$variables[$var] = $varDetails;
}

return $output;
ksort($variables);

return $variables;
}

private function getEnvFiles(): array
private function getEnvFiles(string $filePath): array
{
$files = [
'.env.local.php',
sprintf('.env.%s.local', $this->kernelEnvironment),
sprintf('.env.%s', $this->kernelEnvironment),
sprintf('%s.local.php', $filePath),
sprintf('%s.%s.local', $filePath, $this->kernelEnvironment),
sprintf('%s.%s', $filePath, $this->kernelEnvironment),
];

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

if (!is_file($this->getFilePath('.env')) && is_file($this->getFilePath('.env.dist'))) {
$files[] = '.env.dist';
if (!is_file($filePath) && is_file(sprintf('%s.dist', $filePath))) {
$files[] = sprintf('%s.dist', $filePath);
} else {
$files[] = '.env';
$files[] = $filePath;
}

return $files;
}

private function getFilePath(string $file): string
private function getRelativeName(string $filePath): string
{
return $this->projectDirectory.\DIRECTORY_SEPARATOR.$file;
if (str_starts_with($filePath, $this->projectDirectory)) {
return substr($filePath, \strlen($this->projectDirectory) + 1);
}

return basename($filePath);
}

private function loadValues(string $file): array
private function loadValues(string $filePath): array
{
$filePath = $this->getFilePath($file);

if (str_ends_with($filePath, '.php')) {
return include $filePath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ public function testEmptyDotEnvVarsList()
---------- ------- ------------ ------%S
Variable Value .env.local .env%S
---------- ------- ------------ ------%S
FOO baz bar%S
TEST123 n/a true%S
---------- ------- ------------ ------%S
// Note real values might be different between web and CLI.%S
// Note that values might be different between web and CLI.%S
%a
OUTPUT;

Expand Down

0 comments on commit ca2040e

Please sign in to comment.