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