Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Dotenv] Add --dotenv-path option to DotenvDumpCommand #52553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Symfony/Component/Dotenv/Command/DotenvDumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <info>%command.name%</info> command compiles .env files into a PHP-optimized file called .env.local.php.

Expand All @@ -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'] ?? [];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line looks like you can set dotenv_path in your composer.json file:

"extra": {
  "runtime": {
    "dotenv_path": "some/path/.env"
  }
}

In that case this PR would not be needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed for cases when the .env file name contains an env name, like a local.env, dev.env, prod.env.

$dotenvPath = $projectDir.'/'.($config['dotenv_path'] ?? '.env');
Copy link

@ghost ghost Nov 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have second thoughts on this. Maybe we should set the $config['dotenv_path'] if this option was passed to the command and then leave this line as it was before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it doesn't matter.

$dotenvPath = $projectDir.'/'.($input->getOption('dotenv-path') ?? $config['dotenv_path'] ?? '.env');
$env = $input->getArgument('env') ?? $this->defaultEnv;
$envKey = $config['env_var_name'] ?? 'APP_ENV';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,41 @@ public function testExecuteTestEnvs()
], $vars);
}

public function testExecuteWithDotenvPathOption()
{
@mkdir(__DIR__.'/some-path');
file_put_contents(__DIR__.'/some-path/some-name.env', <<<EOF
APP_ENV=dev
APP_SECRET=abc123
EOF
);

file_put_contents(__DIR__.'/some-path/some-name.env.local', <<<EOF
APP_LOCAL=yes
EOF
);

$command = $this->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();
Expand Down