Skip to content

Commit

Permalink
Merge branch '7.0' into 7.1
Browse files Browse the repository at this point in the history
* 7.0:
  show overridden vars too
  Bump Symfony version to 7.0.8
  Update VERSION for 7.0.7
  Update CHANGELOG for 7.0.7
  Bump Symfony version to 6.4.8
  Update VERSION for 6.4.7
  Update CHANGELOG for 6.4.7
  Bump Symfony version to 5.4.40
  Update VERSION for 5.4.39
  Update CONTRIBUTORS for 5.4.39
  Update CHANGELOG for 5.4.39
  [FrameworkBundle] Fix indentation
  • Loading branch information
nicolas-grekas committed Apr 29, 2024
2 parents 54003d1 + c37515b commit d6f7bd8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
54 changes: 31 additions & 23 deletions Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -69,7 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$filePath = $_SERVER['SYMFONY_DOTENV_PATH'] ?? $this->projectDirectory.\DIRECTORY_SEPARATOR.'.env';

$envFiles = $this->getEnvFiles($filePath);
$availableFiles = array_filter($envFiles, fn (string $file) => is_file($file));
$availableFiles = array_filter($envFiles, 'is_file');

if (\in_array(sprintf('%s.local.php', $filePath), $availableFiles, true)) {
$io->warning(sprintf('Due to existing dump file (%s.local.php) all other dotenv files are skipped.', $this->getRelativeName($filePath)));
Expand All @@ -92,7 +93,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if ($variables || null === $nameFilter) {
$io->table(
array_merge(['Variable', 'Value'], array_map($this->getRelativeName(...), $availableFiles)),
$this->getVariables($availableFiles, $nameFilter)
$variables
);

$io->comment('Note that values might be different between web and CLI.');
Expand All @@ -112,43 +113,50 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti

private function getVariables(array $envFiles, ?string $nameFilter): array
{
$vars = $this->getAvailableVars();

$output = [];
$variables = [];
$fileValues = [];
foreach ($vars as $var) {
$dotenvVars = array_flip(explode(',', $_SERVER['SYMFONY_DOTENV_VARS'] ?? ''));

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

foreach ($variables as $var => $varDetails) {
if (null !== $nameFilter && 0 !== stripos($var, $nameFilter)) {
unset($variables[$var]);
continue;
}

$realValue = $_SERVER[$var];
$varDetails = [$var, $realValue];
foreach ($envFiles as $envFile) {
$values = $fileValues[$envFile] ??= $this->loadValues($envFile);
$varDetails = [$var, '<fg=green>'.OutputFormatter::escape($realValue).'</>'];
$varSeen = !isset($dotenvVars[$var]);

$varString = $values[$var] ?? '<fg=yellow>n/a</>';
$shortenedVar = $this->getHelper('formatter')->truncate($varString, 30);
$varDetails[] = $varString === $realValue ? '<fg=green>'.$shortenedVar.'</>' : $shortenedVar;
foreach ($envFiles as $envFile) {
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 getAvailableVars(): array
{
$dotenvVars = $_SERVER['SYMFONY_DOTENV_VARS'] ?? '';

if ('' === $dotenvVars) {
return [];
}

$vars = explode(',', $dotenvVars);
sort($vars);
$filePath = $_SERVER['SYMFONY_DOTENV_PATH'] ?? $this->projectDirectory.\DIRECTORY_SEPARATOR.'.env';
$envFiles = $this->getEnvFiles($filePath);

return $vars;
return array_keys($this->getVariables(array_filter($envFiles, 'is_file'), null));
}

private function getEnvFiles(string $filePath): array
Expand Down
3 changes: 3 additions & 0 deletions Tests/Command/DebugCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public function testEmptyDotEnvVarsList()
---------- ------- ------------ ------%S
Variable Value .env.local .env%S
---------- ------- ------------ ------%S
FOO baz bar%S
TEST123 n/a true%S
---------- ------- ------------ ------%S
// Note that values might be different between web and CLI.%S
%a
Expand Down

0 comments on commit d6f7bd8

Please sign in to comment.