diff --git a/src/Symfony/Component/Dotenv/Command/DebugCommand.php b/src/Symfony/Component/Dotenv/Command/DebugCommand.php index 0585043cd9463..237d7b7cfd228 100644 --- a/src/Symfony/Component/Dotenv/Command/DebugCommand.php +++ b/src/Symfony/Component/Dotenv/Command/DebugCommand.php @@ -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; @@ -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('✓ %s', $envFile) - : sprintf('⨯ %s', $envFile); + ? sprintf('✓ %s', $this->getRelativeName($envFile)) + : sprintf('⨯ %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, ''.OutputFormatter::escape($realValue).'']; + $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] ?? 'n/a'; - $shortenedVar = $this->getHelper('formatter')->truncate($varString, 30); - $varDetails[] = $varString === $realValue ? ''.$shortenedVar.'' : $shortenedVar; + if (null === $value = $fileValues[$envFile][$var] ?? null) { + $varDetails[] = 'n/a'; + continue; + } + + $shortenedValue = OutputFormatter::escape($this->getHelper('formatter')->truncate($value, 30)); + $varDetails[] = $value === $realValue && !$varSeen ? ''.$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; } diff --git a/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php b/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php index b8b7e39008607..001baec0c2539 100644 --- a/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php +++ b/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php @@ -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;