1313
1414use Composer \Command \BaseCommand ;
1515use Composer \Config ;
16- use Composer \Factory ;
1716use Symfony \Component \Console \Input \InputArgument ;
1817use Symfony \Component \Console \Input \InputInterface ;
1918use Symfony \Component \Console \Input \InputOption ;
@@ -48,19 +47,22 @@ protected function configure()
4847
4948 protected function execute (InputInterface $ input , OutputInterface $ output ): int
5049 {
51- if ($ env = $ input ->getArgument ('env ' )) {
52- $ _SERVER ['APP_ENV ' ] = $ env ;
50+ $ runtime = $ this ->options ->get ('runtime ' ) ?? [];
51+ $ envKey = $ runtime ['env_var_name ' ] ?? 'APP_ENV ' ;
52+
53+ if ($ env = $ input ->getArgument ('env ' ) ?? $ runtime ['env ' ] ?? null ) {
54+ $ _SERVER [$ envKey ] = $ env ;
5355 }
5456
55- $ path = $ this ->options ->get ('root-dir ' ).'/. env ' ;
57+ $ path = $ this ->options ->get ('root-dir ' ).'/ ' .( $ runtime [ ' dotenv_path ' ] ?? ' . env ') ;
5658
5759 if (!$ env || !$ input ->getOption ('empty ' )) {
58- $ vars = $ this ->loadEnv ($ path , $ env );
59- $ env = $ vars [' APP_ENV ' ];
60+ $ vars = $ this ->loadEnv ($ path , $ env, $ runtime );
61+ $ env = $ vars [$ envKey ];
6062 }
6163
6264 if ($ input ->getOption ('empty ' )) {
63- $ vars = [' APP_ENV ' => $ env ];
65+ $ vars = [$ envKey => $ env ];
6466 }
6567
6668 $ vars = var_export ($ vars , true );
@@ -79,7 +81,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7981 return 0 ;
8082 }
8183
82- private function loadEnv (string $ path , ?string $ env ): array
84+ private function loadEnv (string $ path , ?string $ env, array $ runtime ): array
8385 {
8486 if (!file_exists ($ autoloadFile = $ this ->config ->get ('vendor-dir ' ).'/autoload.php ' )) {
8587 throw new \RuntimeException (sprintf ('Please run "composer install" before running this command: "%s" not found. ' , $ autoloadFile ));
@@ -91,9 +93,10 @@ private function loadEnv(string $path, ?string $env): array
9193 throw new \RuntimeException ('Please run "composer require symfony/dotenv" to load the ".env" files configuring the application. ' );
9294 }
9395
96+ $ envKey = $ runtime ['env_var_name ' ] ?? 'APP_ENV ' ;
9497 $ globalsBackup = [$ _SERVER , $ _ENV ];
95- unset($ _SERVER [' APP_ENV ' ]);
96- $ _ENV = [' APP_ENV ' => $ env ];
98+ unset($ _SERVER [$ envKey ]);
99+ $ _ENV = [$ envKey => $ env ];
97100 $ _SERVER ['SYMFONY_DOTENV_VARS ' ] = implode (', ' , array_keys ($ _SERVER ));
98101 putenv ('SYMFONY_DOTENV_VARS= ' .$ _SERVER ['SYMFONY_DOTENV_VARS ' ]);
99102
@@ -105,18 +108,17 @@ private function loadEnv(string $path, ?string $env): array
105108 }
106109
107110 if (!$ env && file_exists ($ p = "$ path.local " )) {
108- $ env = $ _ENV [' APP_ENV ' ] = $ dotenv ->parse (file_get_contents ($ p ), $ p )[' APP_ENV ' ] ?? null ;
111+ $ env = $ _ENV [$ envKey ] = $ dotenv ->parse (file_get_contents ($ p ), $ p )[$ envKey ] ?? null ;
109112 }
110113
111114 if (!$ env ) {
112- throw new \RuntimeException ('Please provide the name of the environment either by passing it as command line argument or by defining the "APP_ENV " variable in the ".env.local" file. ' );
115+ throw new \RuntimeException (sprintf ( 'Please provide the name of the environment either by passing it as command line argument or by defining the "%s " variable in the ".env.local" file. ' , $ envKey ) );
113116 }
114117
115- $ config = @json_decode (file_get_contents (Factory::getComposerFile ()), true );
116- $ testEnvs = $ config ['extra ' ]['runtime ' ]['test_envs ' ] ?? ['test ' ];
118+ $ testEnvs = $ runtime ['test_envs ' ] ?? ['test ' ];
117119
118120 if (method_exists ($ dotenv , 'loadEnv ' )) {
119- $ dotenv ->loadEnv ($ path , ' APP_ENV ' , 'dev ' , $ testEnvs );
121+ $ dotenv ->loadEnv ($ path , $ envKey , 'dev ' , $ testEnvs );
120122 } else {
121123 // fallback code in case your Dotenv component is not 4.2 or higher (when loadEnv() was added)
122124 $ dotenv ->load (file_exists ($ path ) || !file_exists ($ p = "$ path.dist " ) ? $ path : $ p );
0 commit comments