Skip to content
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

Added a feature to optionally override existing environment variables set by the system #1043

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
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
7 changes: 4 additions & 3 deletions src/Command/DumpEnvCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ protected function configure()
new InputArgument('env', 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('override', null, InputOption::VALUE_NONE, 'Whether existing environment variables set by the system should be overridden')
;
}

Expand All @@ -57,7 +58,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$path = $this->options->get('root-dir').'/'.($runtime['dotenv_path'] ?? '.env');

if (!$env || !$input->getOption('empty')) {
$vars = $this->loadEnv($path, $env, $runtime);
$vars = $this->loadEnv($path, $env, $runtime, $input->getOption('override'));
$env = $vars[$envKey];
}

Expand All @@ -81,7 +82,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

private function loadEnv(string $path, ?string $env, array $runtime): array
private function loadEnv(string $path, ?string $env, array $runtime, bool $override): array
{
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));
Expand Down Expand Up @@ -118,7 +119,7 @@ private function loadEnv(string $path, ?string $env, array $runtime): array
$testEnvs = $runtime['test_envs'] ?? ['test'];

if (method_exists($dotenv, 'loadEnv')) {
$dotenv->loadEnv($path, $envKey, 'dev', $testEnvs);
$dotenv->loadEnv($path, $envKey, 'dev', $testEnvs, $override);
} 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);
Expand Down