|
12 | 12 | namespace Symfony\Component\Dotenv\Command;
|
13 | 13 |
|
14 | 14 | use Symfony\Component\Console\Command\Command;
|
| 15 | +use Symfony\Component\Console\Formatter\OutputFormatter; |
15 | 16 | use Symfony\Component\Console\Input\InputInterface;
|
16 | 17 | use Symfony\Component\Console\Output\OutputInterface;
|
17 | 18 | use Symfony\Component\Console\Style\SymfonyStyle;
|
@@ -49,97 +50,105 @@ protected function execute(InputInterface $input, OutputInterface $output): int
|
49 | 50 | return 1;
|
50 | 51 | }
|
51 | 52 |
|
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'); |
56 | 56 |
|
57 |
| - if (\in_array('.env.local.php', $availableFiles, true)) { |
| 57 | + if (\in_array(sprintf('%s.local.php', $filePath), $availableFiles, true)) { |
58 | 58 | $io->warning('Due to existing dump file (.env.local.php) all other dotenv files are skipped.');
|
59 | 59 | }
|
60 | 60 |
|
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))); |
63 | 63 | }
|
64 | 64 |
|
65 | 65 | $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) { |
67 | 67 | 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)); |
70 | 70 | }, $envFiles));
|
71 | 71 |
|
| 72 | + $variables = $this->getVariables($availableFiles); |
| 73 | + |
72 | 74 | $io->section('Variables');
|
73 | 75 | $io->table(
|
74 |
| - array_merge(['Variable', 'Value'], $availableFiles), |
75 |
| - $this->getVariables($availableFiles) |
| 76 | + array_merge(['Variable', 'Value'], array_map([$this, 'getRelativeName'], $availableFiles)), |
| 77 | + $variables |
76 | 78 | );
|
77 | 79 |
|
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.'); |
79 | 81 |
|
80 | 82 | return 0;
|
81 | 83 | }
|
82 | 84 |
|
83 | 85 | private function getVariables(array $envFiles): array
|
84 | 86 | {
|
85 |
| - $dotenvVars = $_SERVER['SYMFONY_DOTENV_VARS'] ?? ''; |
| 87 | + $variables = []; |
| 88 | + $fileValues = []; |
| 89 | + $dotenvVars = array_flip(explode(',', $_SERVER['SYMFONY_DOTENV_VARS'] ?? '')); |
86 | 90 |
|
87 |
| - if ('' === $dotenvVars) { |
88 |
| - return []; |
| 91 | + foreach ($envFiles as $envFile) { |
| 92 | + $fileValues[$envFile] = $this->loadValues($envFile); |
| 93 | + $variables += $fileValues[$envFile]; |
89 | 94 | }
|
90 | 95 |
|
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]); |
93 | 100 |
|
94 |
| - $output = []; |
95 |
| - $fileValues = []; |
96 |
| - foreach ($vars as $var) { |
97 |
| - $realValue = $_SERVER[$var]; |
98 |
| - $varDetails = [$var, $realValue]; |
99 | 101 | 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; |
105 | 110 | }
|
106 | 111 |
|
107 |
| - $output[] = $varDetails; |
| 112 | + $variables[$var] = $varDetails; |
108 | 113 | }
|
109 | 114 |
|
110 |
| - return $output; |
| 115 | + ksort($variables); |
| 116 | + |
| 117 | + return $variables; |
111 | 118 | }
|
112 | 119 |
|
113 |
| - private function getEnvFiles(): array |
| 120 | + private function getEnvFiles(string $filePath): array |
114 | 121 | {
|
115 | 122 | $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), |
119 | 126 | ];
|
120 | 127 |
|
121 | 128 | if ('test' !== $this->kernelEnvironment) {
|
122 |
| - $files[] = '.env.local'; |
| 129 | + $files[] = sprintf('%s.local', $filePath); |
123 | 130 | }
|
124 | 131 |
|
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); |
127 | 134 | } else {
|
128 |
| - $files[] = '.env'; |
| 135 | + $files[] = $filePath; |
129 | 136 | }
|
130 | 137 |
|
131 | 138 | return $files;
|
132 | 139 | }
|
133 | 140 |
|
134 |
| - private function getFilePath(string $file): string |
| 141 | + private function getRelativeName(string $filePath): string |
135 | 142 | {
|
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); |
137 | 148 | }
|
138 | 149 |
|
139 |
| - private function loadValues(string $file): array |
| 150 | + private function loadValues(string $filePath): array |
140 | 151 | {
|
141 |
| - $filePath = $this->getFilePath($file); |
142 |
| - |
143 | 152 | if (str_ends_with($filePath, '.php')) {
|
144 | 153 | return include $filePath;
|
145 | 154 | }
|
|
0 commit comments