diff --git a/src/Symfony/Component/Dotenv/Command/DotenvDumpCommand.php b/src/Symfony/Component/Dotenv/Command/DotenvDumpCommand.php index 051f4b05a04b3..3505508c96028 100644 --- a/src/Symfony/Component/Dotenv/Command/DotenvDumpCommand.php +++ b/src/Symfony/Component/Dotenv/Command/DotenvDumpCommand.php @@ -47,6 +47,7 @@ protected function configure(): void new InputArgument('env', null === $this->defaultEnv ? InputArgument::REQUIRED : InputArgument::OPTIONAL, 'The application environment to dump .env files for - e.g. "prod".'), ]) ->addOption('empty', null, InputOption::VALUE_NONE, 'Ignore the content of .env files') + ->addOption('dotenv-path', null, InputOption::VALUE_OPTIONAL, 'Path to .env file') ->setHelp(<<<'EOT' The %command.name% command compiles .env files into a PHP-optimized file called .env.local.php. @@ -66,7 +67,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $composerFile = $projectDir.'/composer.json'; $config += (is_file($composerFile) ? json_decode(file_get_contents($composerFile), true) : [])['extra']['runtime'] ?? []; - $dotenvPath = $projectDir.'/'.($config['dotenv_path'] ?? '.env'); + $dotenvPath = $projectDir.'/'.($input->getOption('dotenv-path') ?? $config['dotenv_path'] ?? '.env'); $env = $input->getArgument('env') ?? $this->defaultEnv; $envKey = $config['env_var_name'] ?? 'APP_ENV'; diff --git a/src/Symfony/Component/Dotenv/Tests/Command/DotenvDumpCommandTest.php b/src/Symfony/Component/Dotenv/Tests/Command/DotenvDumpCommandTest.php index 44fc304c5ef8f..1b829f0cfb763 100644 --- a/src/Symfony/Component/Dotenv/Tests/Command/DotenvDumpCommandTest.php +++ b/src/Symfony/Component/Dotenv/Tests/Command/DotenvDumpCommandTest.php @@ -92,6 +92,41 @@ public function testExecuteTestEnvs() ], $vars); } + public function testExecuteWithDotenvPathOption() + { + @mkdir(__DIR__.'/some-path'); + file_put_contents(__DIR__.'/some-path/some-name.env', <<createCommand(); + $command->execute([ + 'env' => 'some-env', + '--dotenv-path' => 'some-path/some-name.env', + ]); + + $this->assertFileExists(__DIR__.'/some-path/some-name.env.local.php'); + + $vars = require __DIR__.'/some-path/some-name.env.local.php'; + $this->assertSame([ + 'APP_ENV' => 'some-env', + 'APP_SECRET' => 'abc123', + 'APP_LOCAL' => 'yes', + ], $vars); + + @unlink(__DIR__.'/some-path/some-name.env'); + @unlink(__DIR__.'/some-path/some-name.env.local'); + @unlink(__DIR__.'/some-path/some-name.env.local.php'); + @rmdir(__DIR__.'/some-path'); + } + private function createCommand(): CommandTester { $application = new Application();