24
24
*
25
25
* @internal
26
26
*/
27
- #[Autoconfigure(bind: ['$dotenvPath ' => '%kernel.project_dir%/.env ' , '$defaultEnv ' => '%kernel.environment% ' ])]
27
+ #[Autoconfigure(bind: ['$projectDir ' => '%kernel.project_dir% ' , '$defaultEnv ' => '%kernel.environment% ' ])]
28
28
final class DotenvDumpCommand extends Command
29
29
{
30
30
protected static $ defaultName = 'dotenv:dump ' ;
31
31
protected static $ defaultDescription = 'Compiles .env files to .env.local.php ' ;
32
32
33
- private $ dotenvPath ;
33
+ private $ projectDir ;
34
34
private $ defaultEnv ;
35
35
36
- public function __construct (string $ dotenvPath , string $ defaultEnv = null )
36
+ public function __construct (string $ projectDir , string $ defaultEnv = null )
37
37
{
38
- $ this ->dotenvPath = $ dotenvPath ;
38
+ $ this ->projectDir = $ projectDir ;
39
39
$ this ->defaultEnv = $ defaultEnv ;
40
40
41
41
parent ::__construct ();
@@ -65,13 +65,23 @@ protected function configure()
65
65
*/
66
66
protected function execute (InputInterface $ input , OutputInterface $ output ): int
67
67
{
68
+ $ config = [];
69
+ if (is_file ($ projectDir = $ this ->projectDir )) {
70
+ $ config = ['dotenv_path ' => basename ($ projectDir )];
71
+ $ projectDir = \dirname ($ projectDir );
72
+ }
73
+
74
+ $ composerFile = $ projectDir .'/composer.json ' ;
75
+ $ config += (is_file ($ composerFile ) ? json_decode (file_get_contents ($ composerFile ), true ) : [])['extra ' ]['runtime ' ] ?? [];
76
+ $ dotenvPath = $ projectDir .'/ ' .($ config ['dotenv_path ' ] ?? '.env ' );
68
77
$ env = $ input ->getArgument ('env ' ) ?? $ this ->defaultEnv ;
78
+ $ envKey = $ config ['env_var_name ' ] ?? 'APP_ENV ' ;
69
79
70
80
if ($ input ->getOption ('empty ' )) {
71
- $ vars = [' APP_ENV ' => $ env ];
81
+ $ vars = [$ envKey => $ env ];
72
82
} else {
73
- $ vars = $ this ->loadEnv ($ env );
74
- $ env = $ vars [' APP_ENV ' ];
83
+ $ vars = $ this ->loadEnv ($ dotenvPath , $ env, $ config );
84
+ $ env = $ vars [$ envKey ];
75
85
}
76
86
77
87
$ vars = var_export ($ vars , true );
@@ -83,26 +93,26 @@ protected function execute(InputInterface $input, OutputInterface $output): int
83
93
return $ vars;
84
94
85
95
EOF ;
86
- file_put_contents ($ this -> dotenvPath .'.local.php ' , $ vars , \LOCK_EX );
96
+ file_put_contents ($ dotenvPath .'.local.php ' , $ vars , \LOCK_EX );
87
97
88
98
$ output ->writeln (sprintf ('Successfully dumped .env files in <info>.env.local.php</> for the <info>%s</> environment. ' , $ env ));
89
99
90
100
return 0 ;
91
101
}
92
102
93
- private function loadEnv (string $ env ): array
103
+ private function loadEnv (string $ dotenvPath , string $ env, array $ config ): array
94
104
{
95
105
$ dotenv = new Dotenv ();
96
- $ composerFile = \dirname ( $ this -> dotenvPath ). ' /composer.json ' ;
97
- $ testEnvs = ( is_file ( $ composerFile ) ? json_decode ( file_get_contents ( $ composerFile ), true ) : [])[ ' extra ' ][ ' runtime ' ] ['test_envs ' ] ?? ['test ' ];
106
+ $ envKey = $ config [ ' env_var_name ' ] ?? ' APP_ENV ' ;
107
+ $ testEnvs = $ config ['test_envs ' ] ?? ['test ' ];
98
108
99
109
$ globalsBackup = [$ _SERVER , $ _ENV ];
100
- unset($ _SERVER [' APP_ENV ' ]);
101
- $ _ENV = [' APP_ENV ' => $ env ];
110
+ unset($ _SERVER [$ envKey ]);
111
+ $ _ENV = [$ envKey => $ env ];
102
112
$ _SERVER ['SYMFONY_DOTENV_VARS ' ] = implode (', ' , array_keys ($ _SERVER ));
103
113
104
114
try {
105
- $ dotenv ->loadEnv ($ this -> dotenvPath , null , 'dev ' , $ testEnvs );
115
+ $ dotenv ->loadEnv ($ dotenvPath , null , 'dev ' , $ testEnvs );
106
116
unset($ _ENV ['SYMFONY_DOTENV_VARS ' ]);
107
117
108
118
return $ _ENV ;
0 commit comments