diff --git a/src/Command/DumpEnvCommand.php b/src/Command/DumpEnvCommand.php index da1bb0c8c..83db3190d 100644 --- a/src/Command/DumpEnvCommand.php +++ b/src/Command/DumpEnvCommand.php @@ -17,7 +17,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Dotenv\Dotenv; +use Symfony\Component\Process\PhpProcess; use Symfony\Flex\Options; class DumpEnvCommand extends BaseCommand @@ -50,12 +50,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $_SERVER['APP_ENV'] = $env = $input->getArgument('env'); $path = $this->options->get('root-dir').'/.env'; - if (!$input->getOption('empty')) { - $this->loadEnv($path, $env); - } - - $vars = array_flip(explode(',', ($_SERVER['SYMFONY_DOTENV_VARS'] ?? '').',APP_ENV')); - $vars = array_intersect_key($_SERVER, $vars); + $vars = $input->getOption('empty') ? ['APP_ENV' => $env] : $this->loadEnv($path, $env); $vars = var_export($vars, true); $vars = <<getIO()->writeError('Successfully dumped .env files in .env.local.php'); } - private function loadEnv(string $path, string $env) + private function loadEnv(string $path, string $env): array { - require $this->config->get('vendor-dir').'/autoload.php'; - - if (!class_exists(Dotenv::class)) { - throw new \RuntimeException('Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.'); + if (!file_exists($autoloadFile = $this->config->get('vendor-dir').'/autoload.php')) { + throw new \RuntimeException(sprintf('Please run "composer install" before running this command: "%s" not found.', $autoloadFile)); } - $dotenv = new Dotenv(); + $php = <<<'EOPHP' +loadEnv($path); - } else { - // fallback code in case your Dotenv component is not 4.2 or higher (when loadEnv() was added) - $dotenv->load(file_exists($path) || !file_exists($p = "$path.dist") ? $path : $p); +use Symfony\Component\Dotenv\Dotenv; - if ('test' !== $env && file_exists($p = "$path.local")) { - $dotenv->load($p); - } +require %s; - if (file_exists($p = "$path.$env")) { - $dotenv->load($p); - } +if (!class_exists(Dotenv::class)) { + exit; +} + +foreach ($_SERVER as $k => $v) { + if (\is_string($v) && false !== getenv($k)) { + unset($_SERVER[$k]); + putenv($k); + } +} + +$path = %s; +$env = %s; +$dotenv = new Dotenv(); +$_ENV = ['APP_ENV' => $env]; + +if (method_exists($dotenv, 'loadEnv')) { + $dotenv->loadEnv($path); +} else { + // fallback code in case your Dotenv component is not 4.2 or higher (when loadEnv() was added) + $dotenv->load(file_exists($path) || !file_exists($p = "$path.dist") ? $path : $p); + + if ('test' !== $env && file_exists($p = "$path.local")) { + $dotenv->load($p); + } - if (file_exists($p = "$path.$env.local")) { - $dotenv->load($p); - } + if (file_exists($p = "$path.$env")) { + $dotenv->load($p); + } + + if (file_exists($p = "$path.$env.local")) { + $dotenv->load($p); + } +} + +unset($_ENV['SYMFONY_DOTENV_VARS']); + +echo serialize($_ENV); + +EOPHP; + + $php = sprintf($php, var_export($autoloadFile, true), var_export($path, true), var_export($env, true)); + $process = new PhpProcess($php); + if (method_exists($process, 'inheritEnvironmentVariables')) { + $process->inheritEnvironmentVariables(); + } + $process->run(); + + if (!$process->isSuccessful()) { + throw new \RuntimeException($process->getErrorOutput()); } + + if (!$env = $process->getOutput()) { + throw new \RuntimeException('Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.'); + } + + return unserialize($env); } }