Skip to content

Commit

Permalink
add --empty option
Browse files Browse the repository at this point in the history
  • Loading branch information
renanbr authored and nicolas-grekas committed Jan 21, 2019
1 parent 31b12b1 commit 4d92f7b
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 18 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml.dist
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
<phpunit backupGlobals="true"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
Expand Down
44 changes: 27 additions & 17 deletions src/Command/DumpEnvCommand.php
Expand Up @@ -15,6 +15,7 @@
use Composer\Config;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Flex\Options;
Expand All @@ -40,19 +41,43 @@ protected function configure()
->setDefinition([
new InputArgument('env', InputArgument::REQUIRED, 'The application environment to dump .env files for - e.g. "prod".'),
])
->addOption('empty', null, InputOption::VALUE_NONE, 'Ignore the content of .env files')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$_SERVER['APP_ENV'] = $env = $input->getArgument('env');
$path = $this->options->get('root-dir').'/.env';

if (!$input->getOption('empty')) {
$this->loadEnv($path, $env);
}

$vars = array_flip(explode(',', ($_SERVER['SYMFONY_DOTENV_VARS'] ?? '').',APP_ENV'));
$vars = array_intersect_key($_SERVER, $vars);
$vars = var_export($vars, true);
$vars = <<<EOF
<?php
// This file was generated by running "composer dump-env $env"
return $vars;
EOF;
file_put_contents($path.'.local.php', $vars, LOCK_EX);

$this->getIO()->writeError('Successfully dumped .env files in <info>.env.local.php</>');
}

private function loadEnv(string $path, string $env)
{
require $this->config->get('vendor-dir').'/autoload.php';

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

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

if (method_exists($dotenv, 'loadEnv')) {
Expand All @@ -73,20 +98,5 @@ protected function execute(InputInterface $input, OutputInterface $output)
$dotenv->load($p);
}
}

$vars = array_flip(explode(',', ($_SERVER['SYMFONY_DOTENV_VARS'] ?? '').',APP_ENV'));
$vars = array_intersect_key($_SERVER, $vars);
$vars = var_export($vars, true);
$vars = <<<EOF
<?php
// This file was generated by running "composer dump-env $env"
return $vars;
EOF;
file_put_contents($path.'.local.php', $vars, LOCK_EX);

$this->getIO()->writeError('Successfully dumped .env files in <info>.env.local.php</>');
}
}
98 changes: 98 additions & 0 deletions tests/Command/DumpEnvCommandTest.php
@@ -0,0 +1,98 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Flex\Tests\Command;

use Composer\Config;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Flex\Command\DumpEnvCommand;
use Symfony\Flex\Options;

class DumpEnvCommandTest extends TestCase
{
public function testFileIsCreated()
{
@mkdir(FLEX_TEST_DIR);
$env = FLEX_TEST_DIR.'/.env';
$envLocal = FLEX_TEST_DIR.'/.env.local.php';
@unlink($env);
@unlink($envLocal);

$envContent = <<<EOF
APP_ENV=dev
APP_SECRET=abcdefgh123456789
EOF;
file_put_contents($env, $envContent);

$command = $this->createCommandDumpEnv();
$command->execute([
'env' => 'prod',
]);

$this->assertFileExists($envLocal);

$vars = require $envLocal;
$this->assertSame([
'APP_ENV' => 'prod',
'APP_SECRET' => 'abcdefgh123456789',
], $vars);

unlink($env);
unlink($envLocal);
}

public function testEmptyOptionMustIgnoreContent()
{
@mkdir(FLEX_TEST_DIR);
$env = FLEX_TEST_DIR.'/.env';
$envLocal = FLEX_TEST_DIR.'/.env.local.php';
@unlink($env);
@unlink($envLocal);

$envContent = <<<EOF
APP_ENV=dev
APP_SECRET=abcdefgh123456789
EOF;
file_put_contents($env, $envContent);

$command = $this->createCommandDumpEnv();
$command->execute([
'env' => 'prod',
'--empty' => true,
]);

$this->assertFileExists($envLocal);

$vars = require $envLocal;
$this->assertSame([
'APP_ENV' => 'prod',
], $vars);

unlink($env);
unlink($envLocal);
}

private function createCommandDumpEnv()
{
$command = new DumpEnvCommand(
new Config(false, __DIR__.'/../..'),
new Options(['root-dir' => FLEX_TEST_DIR])
);

$application = new Application();
$application->add($command);
$command = $application->find('dump-env');

return new CommandTester($command);
}
}

0 comments on commit 4d92f7b

Please sign in to comment.