From 83a2310904a4f5d4f42526227b5a578ac82232a9 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 15 Feb 2022 17:59:24 +0100 Subject: [PATCH] [Dotenv] Fix reading config for symfony/runtime when running dump command --- Command/DotenvDumpCommand.php | 38 ++++++++++++++++--------- Tests/Command/DotenvDumpCommandTest.php | 2 +- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/Command/DotenvDumpCommand.php b/Command/DotenvDumpCommand.php index e044199..4411054 100644 --- a/Command/DotenvDumpCommand.php +++ b/Command/DotenvDumpCommand.php @@ -24,18 +24,18 @@ * * @internal */ -#[Autoconfigure(bind: ['$dotenvPath' => '%kernel.project_dir%/.env', '$defaultEnv' => '%kernel.environment%'])] +#[Autoconfigure(bind: ['$projectDir' => '%kernel.project_dir%', '$defaultEnv' => '%kernel.environment%'])] final class DotenvDumpCommand extends Command { protected static $defaultName = 'dotenv:dump'; protected static $defaultDescription = 'Compiles .env files to .env.local.php'; - private $dotenvPath; + private $projectDir; private $defaultEnv; - public function __construct(string $dotenvPath, string $defaultEnv = null) + public function __construct(string $projectDir, string $defaultEnv = null) { - $this->dotenvPath = $dotenvPath; + $this->projectDir = $projectDir; $this->defaultEnv = $defaultEnv; parent::__construct(); @@ -65,13 +65,23 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output): int { + $config = []; + if (is_file($projectDir = $this->projectDir)) { + $config = ['dotenv_path' => basename($projectDir)]; + $projectDir = \dirname($projectDir); + } + + $composerFile = $projectDir.'/composer.json'; + $config += (is_file($composerFile) ? json_decode(file_get_contents($composerFile), true) : [])['extra']['runtime'] ?? []; + $dotenvPath = $projectDir.'/'.($config['dotenv_path'] ?? '.env'); $env = $input->getArgument('env') ?? $this->defaultEnv; + $envKey = $config['env_var_name'] ?? 'APP_ENV'; if ($input->getOption('empty')) { - $vars = ['APP_ENV' => $env]; + $vars = [$envKey => $env]; } else { - $vars = $this->loadEnv($env); - $env = $vars['APP_ENV']; + $vars = $this->loadEnv($dotenvPath, $env, $config); + $env = $vars[$envKey]; } $vars = var_export($vars, true); @@ -83,26 +93,26 @@ protected function execute(InputInterface $input, OutputInterface $output): int return $vars; EOF; - file_put_contents($this->dotenvPath.'.local.php', $vars, \LOCK_EX); + file_put_contents($dotenvPath.'.local.php', $vars, \LOCK_EX); $output->writeln(sprintf('Successfully dumped .env files in .env.local.php for the %s environment.', $env)); return 0; } - private function loadEnv(string $env): array + private function loadEnv(string $dotenvPath, string $env, array $config): array { $dotenv = new Dotenv(); - $composerFile = \dirname($this->dotenvPath).'/composer.json'; - $testEnvs = (is_file($composerFile) ? json_decode(file_get_contents($composerFile), true) : [])['extra']['runtime']['test_envs'] ?? ['test']; + $envKey = $config['env_var_name'] ?? 'APP_ENV'; + $testEnvs = $config['test_envs'] ?? ['test']; $globalsBackup = [$_SERVER, $_ENV]; - unset($_SERVER['APP_ENV']); - $_ENV = ['APP_ENV' => $env]; + unset($_SERVER[$envKey]); + $_ENV = [$envKey => $env]; $_SERVER['SYMFONY_DOTENV_VARS'] = implode(',', array_keys($_SERVER)); try { - $dotenv->loadEnv($this->dotenvPath, null, 'dev', $testEnvs); + $dotenv->loadEnv($dotenvPath, null, 'dev', $testEnvs); unset($_ENV['SYMFONY_DOTENV_VARS']); return $_ENV; diff --git a/Tests/Command/DotenvDumpCommandTest.php b/Tests/Command/DotenvDumpCommandTest.php index c1f3e95..44fc304 100644 --- a/Tests/Command/DotenvDumpCommandTest.php +++ b/Tests/Command/DotenvDumpCommandTest.php @@ -95,7 +95,7 @@ public function testExecuteTestEnvs() private function createCommand(): CommandTester { $application = new Application(); - $application->add(new DotenvDumpCommand(__DIR__.'/.env')); + $application->add(new DotenvDumpCommand(__DIR__)); return new CommandTester($application->find('dotenv:dump')); }