From 2e31a1bd2ff6bbb8047d1f79b706a15f16407ecd Mon Sep 17 00:00:00 2001 From: Aleksey Polyvanyi Date: Sat, 11 Nov 2023 15:02:42 +0100 Subject: [PATCH 1/3] feature/dotenv-add-dotenv-path-option-to-DotenvDumpCommand --- .../Dotenv/Command/DotenvDumpCommand.php | 10 +++++- .../Tests/Command/DotenvDumpCommandTest.php | 35 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Dotenv/Command/DotenvDumpCommand.php b/src/Symfony/Component/Dotenv/Command/DotenvDumpCommand.php index 051f4b05a04b3..ff3d42731c843 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 dot-env files') ->setHelp(<<<'EOT' The %command.name% command compiles .env files into a PHP-optimized file called .env.local.php. @@ -66,7 +67,14 @@ 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'); + + if ($input->getOption('dotenv-path')) { + $dotenvPath = $input->getOption('dotenv-path'); + } else { + $dotenvPath = $config['dotenv_path'] ?? '.env'; + } + $dotenvPath = $projectDir.'/'.$dotenvPath; + $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(); From a7ce1dc5552e2bb45609d5d699e32166134cb8fc Mon Sep 17 00:00:00 2001 From: Aleksey Polyvanyi Date: Sun, 12 Nov 2023 13:37:39 +0100 Subject: [PATCH 2/3] -refactoring --- .../Component/Dotenv/Command/DotenvDumpCommand.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/Dotenv/Command/DotenvDumpCommand.php b/src/Symfony/Component/Dotenv/Command/DotenvDumpCommand.php index ff3d42731c843..7ca342808b234 100644 --- a/src/Symfony/Component/Dotenv/Command/DotenvDumpCommand.php +++ b/src/Symfony/Component/Dotenv/Command/DotenvDumpCommand.php @@ -47,7 +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 dot-env files') + ->addOption('dotenv-path', null, InputOption::VALUE_OPTIONAL, 'Path to .env files') ->setHelp(<<<'EOT' The %command.name% command compiles .env files into a PHP-optimized file called .env.local.php. @@ -67,14 +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'] ?? []; - - if ($input->getOption('dotenv-path')) { - $dotenvPath = $input->getOption('dotenv-path'); - } else { - $dotenvPath = $config['dotenv_path'] ?? '.env'; - } - $dotenvPath = $projectDir.'/'.$dotenvPath; - + $dotenvPath = $projectDir.'/'.($input->getOption('dotenv-path') ?? $config['dotenv_path'] ?? '.env'); $env = $input->getArgument('env') ?? $this->defaultEnv; $envKey = $config['env_var_name'] ?? 'APP_ENV'; From c0218d66a60e3b9078f49c53b5ae1fc568402353 Mon Sep 17 00:00:00 2001 From: Aleksey Polyvanyi Date: Mon, 13 Nov 2023 07:58:10 +0100 Subject: [PATCH 3/3] -refactoring --- src/Symfony/Component/Dotenv/Command/DotenvDumpCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Dotenv/Command/DotenvDumpCommand.php b/src/Symfony/Component/Dotenv/Command/DotenvDumpCommand.php index 7ca342808b234..3505508c96028 100644 --- a/src/Symfony/Component/Dotenv/Command/DotenvDumpCommand.php +++ b/src/Symfony/Component/Dotenv/Command/DotenvDumpCommand.php @@ -47,7 +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 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.