Skip to content

Commit 41e1408

Browse files
ostroluckynicolas-grekas
authored andcommitted
Make "env" argument in symfony:dump-env optional
1 parent e385202 commit 41e1408

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

src/Command/DumpEnvCommand.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,29 @@ protected function configure()
3939
->setAliases(['dump-env'])
4040
->setDescription('Compiles .env files to .env.local.php.')
4141
->setDefinition([
42-
new InputArgument('env', InputArgument::REQUIRED, 'The application environment to dump .env files for - e.g. "prod".'),
42+
new InputArgument('env', InputArgument::OPTIONAL, 'The application environment to dump .env files for - e.g. "prod".'),
4343
])
4444
->addOption('empty', null, InputOption::VALUE_NONE, 'Ignore the content of .env files')
4545
;
4646
}
4747

4848
protected function execute(InputInterface $input, OutputInterface $output): int
4949
{
50-
$_SERVER['APP_ENV'] = $env = $input->getArgument('env');
50+
if ($env = $input->getArgument('env')) {
51+
$_SERVER['APP_ENV'] = $env;
52+
}
53+
5154
$path = $this->options->get('root-dir').'/.env';
5255

53-
$vars = $input->getOption('empty') ? ['APP_ENV' => $env] : $this->loadEnv($path, $env);
56+
if (!$env || !$input->getOption('empty')) {
57+
$vars = $this->loadEnv($path, $env);
58+
$env = $vars['APP_ENV'];
59+
}
60+
61+
if ($input->getOption('empty')) {
62+
$vars = ['APP_ENV' => $env];
63+
}
64+
5465
$vars = var_export($vars, true);
5566
$vars = <<<EOF
5667
<?php
@@ -67,7 +78,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6778
return 0;
6879
}
6980

70-
private function loadEnv(string $path, string $env): array
81+
private function loadEnv(string $path, ?string $env): array
7182
{
7283
if (!file_exists($autoloadFile = $this->config->get('vendor-dir').'/autoload.php')) {
7384
throw new \RuntimeException(sprintf('Please run "composer install" before running this command: "%s" not found.', $autoloadFile));
@@ -90,6 +101,14 @@ private function loadEnv(string $path, string $env): array
90101
$dotenv = new Dotenv(false);
91102
}
92103

104+
if (!$env && file_exists($p = "$path.local")) {
105+
$env = $_ENV['APP_ENV'] = $dotenv->parse(file_get_contents($p), $p)['APP_ENV'] ?? null;
106+
}
107+
108+
if (!$env) {
109+
throw new \RuntimeException('Please provide the name of the environment either by using the "--env" command line argument or by defining the "APP_ENV" variable in the ".env.local" file.');
110+
}
111+
93112
if (method_exists($dotenv, 'loadEnv')) {
94113
$dotenv->loadEnv($path);
95114
} else {

tests/Command/DumpEnvCommandTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,50 @@ public function testEnvCanBeReferenced()
120120
unlink($envLocal);
121121
}
122122

123+
public function testRequiresToSpecifyEnvArgumentWhenLocalFileDoesNotSpecifyAppEnv()
124+
{
125+
@mkdir(FLEX_TEST_DIR);
126+
$env = FLEX_TEST_DIR.'/.env';
127+
$envLocal = FLEX_TEST_DIR.'/.env.local';
128+
129+
file_put_contents($env, 'APP_ENV=dev');
130+
file_put_contents($envLocal, '');
131+
132+
$command = $this->createCommandDumpEnv();
133+
$this->expectException(\RuntimeException::class);
134+
$this->expectExceptionMessage('Please provide the name of the environment either by using the "--env" command line argument or by defining the "APP_ENV" variable in the ".env.local" file.');
135+
136+
try {
137+
$command->execute([]);
138+
} finally {
139+
unlink($env);
140+
unlink($envLocal);
141+
}
142+
}
143+
144+
public function testDoesNotRequireToSpecifyEnvArgumentWhenLocalFileIsPresent()
145+
{
146+
@mkdir(FLEX_TEST_DIR);
147+
$env = FLEX_TEST_DIR.'/.env';
148+
$envLocal = FLEX_TEST_DIR.'/.env.local';
149+
$envLocalPhp = FLEX_TEST_DIR.'/.env.local.php';
150+
@unlink($envLocalPhp);
151+
152+
file_put_contents($env, 'APP_ENV=dev');
153+
file_put_contents($envLocal, 'APP_ENV=staging');
154+
155+
$command = $this->createCommandDumpEnv();
156+
$command->execute([]);
157+
158+
$this->assertFileExists($envLocalPhp);
159+
160+
$this->assertSame(['APP_ENV' => 'staging'], require $envLocalPhp);
161+
162+
unlink($env);
163+
unlink($envLocal);
164+
unlink($envLocalPhp);
165+
}
166+
123167
private function createCommandDumpEnv()
124168
{
125169
$command = new DumpEnvCommand(

0 commit comments

Comments
 (0)