Skip to content

Commit 7e561d5

Browse files
renanbrnicolas-grekas
authored andcommitted
add --empty option
1 parent 31b12b1 commit 7e561d5

File tree

4 files changed

+127
-18
lines changed

4 files changed

+127
-18
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
},
1616
"require-dev": {
1717
"composer/composer": "^1.0.2",
18+
"symfony/dotenv": "^3.4|^4.0",
1819
"symfony/phpunit-bridge": "^3.4.19|^4.1.8",
1920
"symfony/process": "^2.7|^3.0|^4.0"
2021
},

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3-
<phpunit backupGlobals="false"
3+
<phpunit backupGlobals="true"
44
backupStaticAttributes="false"
55
colors="true"
66
convertErrorsToExceptions="true"

src/Command/DumpEnvCommand.php

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Composer\Config;
1616
use Symfony\Component\Console\Input\InputArgument;
1717
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Input\InputOption;
1819
use Symfony\Component\Console\Output\OutputInterface;
1920
use Symfony\Component\Dotenv\Dotenv;
2021
use Symfony\Flex\Options;
@@ -40,19 +41,43 @@ protected function configure()
4041
->setDefinition([
4142
new InputArgument('env', InputArgument::REQUIRED, 'The application environment to dump .env files for - e.g. "prod".'),
4243
])
44+
->addOption('empty', null, InputOption::VALUE_NONE, 'Ignore the content of .env files')
4345
;
4446
}
4547

4648
protected function execute(InputInterface $input, OutputInterface $output)
49+
{
50+
$_SERVER['APP_ENV'] = $env = $input->getArgument('env');
51+
$path = $this->options->get('root-dir').'/.env';
52+
53+
if (!$input->getOption('empty')) {
54+
$this->loadEnv($path, $env);
55+
}
56+
57+
$vars = array_flip(explode(',', ($_SERVER['SYMFONY_DOTENV_VARS'] ?? '').',APP_ENV'));
58+
$vars = array_intersect_key($_SERVER, $vars);
59+
$vars = var_export($vars, true);
60+
$vars = <<<EOF
61+
<?php
62+
63+
// This file was generated by running "composer dump-env $env"
64+
65+
return $vars;
66+
67+
EOF;
68+
file_put_contents($path.'.local.php', $vars, LOCK_EX);
69+
70+
$this->getIO()->writeError('Successfully dumped .env files in <info>.env.local.php</>');
71+
}
72+
73+
private function loadEnv(string $path, string $env)
4774
{
4875
require $this->config->get('vendor-dir').'/autoload.php';
4976

5077
if (!class_exists(Dotenv::class)) {
5178
throw new \RuntimeException('Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.');
5279
}
5380

54-
$_SERVER['APP_ENV'] = $env = $input->getArgument('env');
55-
$path = $this->options->get('root-dir').'/.env';
5681
$dotenv = new Dotenv();
5782

5883
if (method_exists($dotenv, 'loadEnv')) {
@@ -73,20 +98,5 @@ protected function execute(InputInterface $input, OutputInterface $output)
7398
$dotenv->load($p);
7499
}
75100
}
76-
77-
$vars = array_flip(explode(',', ($_SERVER['SYMFONY_DOTENV_VARS'] ?? '').',APP_ENV'));
78-
$vars = array_intersect_key($_SERVER, $vars);
79-
$vars = var_export($vars, true);
80-
$vars = <<<EOF
81-
<?php
82-
83-
// This file was generated by running "composer dump-env $env"
84-
85-
return $vars;
86-
87-
EOF;
88-
file_put_contents($path.'.local.php', $vars, LOCK_EX);
89-
90-
$this->getIO()->writeError('Successfully dumped .env files in <info>.env.local.php</>');
91101
}
92102
}

tests/Command/DumpEnvCommandTest.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Flex\Tests\Command;
13+
14+
use Composer\Config;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\Console\Application;
17+
use Symfony\Component\Console\Tester\CommandTester;
18+
use Symfony\Flex\Command\DumpEnvCommand;
19+
use Symfony\Flex\Options;
20+
21+
class DumpEnvCommandTest extends TestCase
22+
{
23+
public function testFileIsCreated()
24+
{
25+
@mkdir(FLEX_TEST_DIR);
26+
$env = FLEX_TEST_DIR.'/.env';
27+
$envLocal = FLEX_TEST_DIR.'/.env.local.php';
28+
@unlink($env);
29+
@unlink($envLocal);
30+
31+
$envContent = <<<EOF
32+
APP_ENV=dev
33+
APP_SECRET=abcdefgh123456789
34+
EOF;
35+
file_put_contents($env, $envContent);
36+
37+
$command = $this->createCommandDumpEnv();
38+
$command->execute([
39+
'env' => 'prod',
40+
]);
41+
42+
$this->assertFileExists($envLocal);
43+
44+
$vars = require $envLocal;
45+
$this->assertSame([
46+
'APP_ENV' => 'prod',
47+
'APP_SECRET' => 'abcdefgh123456789',
48+
], $vars);
49+
50+
unlink($env);
51+
unlink($envLocal);
52+
}
53+
54+
public function testEmptyOptionMustIgnoreContent()
55+
{
56+
@mkdir(FLEX_TEST_DIR);
57+
$env = FLEX_TEST_DIR.'/.env';
58+
$envLocal = FLEX_TEST_DIR.'/.env.local.php';
59+
@unlink($env);
60+
@unlink($envLocal);
61+
62+
$envContent = <<<EOF
63+
APP_ENV=dev
64+
APP_SECRET=abcdefgh123456789
65+
EOF;
66+
file_put_contents($env, $envContent);
67+
68+
$command = $this->createCommandDumpEnv();
69+
$command->execute([
70+
'env' => 'prod',
71+
'--empty' => true,
72+
]);
73+
74+
$this->assertFileExists($envLocal);
75+
76+
$vars = require $envLocal;
77+
$this->assertSame([
78+
'APP_ENV' => 'prod',
79+
], $vars);
80+
81+
unlink($env);
82+
unlink($envLocal);
83+
}
84+
85+
private function createCommandDumpEnv()
86+
{
87+
$command = new DumpEnvCommand(
88+
new Config(false, __DIR__.'/../..'),
89+
new Options(['root-dir' => FLEX_TEST_DIR])
90+
);
91+
92+
$application = new Application();
93+
$application->add($command);
94+
$command = $application->find('dump-env');
95+
96+
return new CommandTester($command);
97+
}
98+
}

0 commit comments

Comments
 (0)