@@ -48,19 +48,22 @@ protected function configure()
4848
4949 protected function execute (InputInterface $ input , OutputInterface $ output ): int
5050 {
51- if ($ env = $ input ->getArgument ('env ' )) {
52- $ _SERVER ['APP_ENV ' ] = $ env ;
51+ $ config = @json_decode (file_get_contents (Factory::getComposerFile ()), true )['extra ' ]['runtime ' ] ?? [];
52+ $ envKey = $ config ['env_var_name ' ] ?? 'APP_ENV ' ;
53+
54+ if ($ env = $ input ->getArgument ('env ' ) ?? $ config ['env ' ] ?? null ) {
55+ $ _SERVER [$ envKey ] = $ env ;
5356 }
5457
55- $ path = $ this ->options ->get ('root-dir ' ).'/. env ' ;
58+ $ path = $ this ->options ->get ('root-dir ' ).'/ ' .( $ config [ ' dotenv_path ' ] ?? ' . env ') ;
5659
5760 if (!$ env || !$ input ->getOption ('empty ' )) {
58- $ vars = $ this ->loadEnv ($ path , $ env );
59- $ env = $ vars [' APP_ENV ' ];
61+ $ vars = $ this ->loadEnv ($ path , $ env, $ config );
62+ $ env = $ vars [$ envKey ];
6063 }
6164
6265 if ($ input ->getOption ('empty ' )) {
63- $ vars = [' APP_ENV ' => $ env ];
66+ $ vars = [$ envKey => $ env ];
6467 }
6568
6669 $ vars = var_export ($ vars , true );
@@ -79,7 +82,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7982 return 0 ;
8083 }
8184
82- private function loadEnv (string $ path , ?string $ env ): array
85+ private function loadEnv (string $ path , ?string $ env, array $ config ): array
8386 {
8487 if (!file_exists ($ autoloadFile = $ this ->config ->get ('vendor-dir ' ).'/autoload.php ' )) {
8588 throw new \RuntimeException (sprintf ('Please run "composer install" before running this command: "%s" not found. ' , $ autoloadFile ));
@@ -91,9 +94,10 @@ private function loadEnv(string $path, ?string $env): array
9194 throw new \RuntimeException ('Please run "composer require symfony/dotenv" to load the ".env" files configuring the application. ' );
9295 }
9396
97+ $ envKey = $ config ['env_var_name ' ] ?? 'APP_ENV ' ;
9498 $ globalsBackup = [$ _SERVER , $ _ENV ];
95- unset($ _SERVER [' APP_ENV ' ]);
96- $ _ENV = [' APP_ENV ' => $ env ];
99+ unset($ _SERVER [$ envKey ]);
100+ $ _ENV = [$ envKey => $ env ];
97101 $ _SERVER ['SYMFONY_DOTENV_VARS ' ] = implode (', ' , array_keys ($ _SERVER ));
98102 putenv ('SYMFONY_DOTENV_VARS= ' .$ _SERVER ['SYMFONY_DOTENV_VARS ' ]);
99103
@@ -105,18 +109,17 @@ private function loadEnv(string $path, ?string $env): array
105109 }
106110
107111 if (!$ env && file_exists ($ p = "$ path.local " )) {
108- $ env = $ _ENV [' APP_ENV ' ] = $ dotenv ->parse (file_get_contents ($ p ), $ p )[' APP_ENV ' ] ?? null ;
112+ $ env = $ _ENV [$ envKey ] = $ dotenv ->parse (file_get_contents ($ p ), $ p )[$ envKey ] ?? null ;
109113 }
110114
111115 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. ' );
116+ 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 ) );
113117 }
114118
115- $ config = @json_decode (file_get_contents (Factory::getComposerFile ()), true );
116- $ testEnvs = $ config ['extra ' ]['runtime ' ]['test_envs ' ] ?? ['test ' ];
119+ $ testEnvs = $ config ['test_envs ' ] ?? ['test ' ];
117120
118121 if (method_exists ($ dotenv , 'loadEnv ' )) {
119- $ dotenv ->loadEnv ($ path , ' APP_ENV ' , 'dev ' , $ testEnvs );
122+ $ dotenv ->loadEnv ($ path , $ envKey , 'dev ' , $ testEnvs );
120123 } else {
121124 // fallback code in case your Dotenv component is not 4.2 or higher (when loadEnv() was added)
122125 $ dotenv ->load (file_exists ($ path ) || !file_exists ($ p = "$ path.dist " ) ? $ path : $ p );
0 commit comments