Skip to content

Change the EnvConfigurator to only handle the .env file #423

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
wants to merge 1 commit into from
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
97 changes: 15 additions & 82 deletions src/Configurator/EnvConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,18 @@ public function configure(Recipe $recipe, $vars)
{
$this->write('Added environment variable defaults');

$this->configureEnvDist($recipe, $vars);
$this->configurePhpUnit($recipe, $vars);
$this->configureEnv($recipe, $vars);
}

public function unconfigure(Recipe $recipe, $vars)
{
$this->unconfigureEnvFiles($recipe, $vars);
$this->unconfigurePhpUnit($recipe, $vars);
$this->unconfigureEnvFiles($recipe);
}

private function configureEnvDist(Recipe $recipe, $vars)
private function configureEnv(Recipe $recipe, $vars)
{
$distenv = getcwd().'/.env.dist';
if (!is_file($distenv) || $this->isFileMarked($recipe, $distenv)) {
$envfile = getcwd().'/.env';
if (!is_file($envfile) || $this->isFileMarked($recipe, $envfile)) {
return;
}

Expand All @@ -54,89 +52,24 @@ private function configureEnvDist(Recipe $recipe, $vars)
}
$data .= "$key=$value\n";
}
if (!file_exists(getcwd().'/.env')) {
copy($distenv, getcwd().'/.env');
}
$data = $this->markData($recipe, $data);
file_put_contents($distenv, $data, FILE_APPEND);
file_put_contents(getcwd().'/.env', $data, FILE_APPEND);
file_put_contents($envfile, $data, FILE_APPEND);
}

private function configurePhpUnit(Recipe $recipe, $vars)
private function unconfigureEnvFiles(Recipe $recipe)
{
foreach (['phpunit.xml.dist', 'phpunit.xml'] as $file) {
$phpunit = getcwd().'/'.$file;
if (!is_file($phpunit)) {
continue;
}

if ($this->isFileXmlMarked($recipe, $phpunit)) {
continue;
}

$data = '';
foreach ($vars as $key => $value) {
$value = $this->evaluateValue($value);
if ('#' === $key[0]) {
if (is_numeric(substr($key, 1))) {
$doc = new \DOMDocument();
$data .= ' '.$doc->saveXML($doc->createComment(' '.$value.' '))."\n";
} else {
$value = $this->options->expandTargetDir($value);
$doc = new \DOMDocument();
$fragment = $doc->createElement('env');
$fragment->setAttribute('name', substr($key, 1));
$fragment->setAttribute('value', $value);
$data .= ' '.str_replace(['<', '/>'], ['<!-- ', ' -->'], $doc->saveXML($fragment))."\n";
}
} else {
$value = $this->options->expandTargetDir($value);
$doc = new \DOMDocument();
$fragment = $doc->createElement('env');
$fragment->setAttribute('name', $key);
$fragment->setAttribute('value', $value);
$data .= ' '.$doc->saveXML($fragment)."\n";
}
}
$data = $this->markXmlData($recipe, $data);
file_put_contents($phpunit, preg_replace('{^(\s+</php>)}m', $data.'$1', file_get_contents($phpunit)));
$envfile = getcwd().'/.env';
if (!file_exists($envfile)) {
return;
}
}

private function unconfigureEnvFiles(Recipe $recipe, $vars)
{
foreach (['.env', '.env.dist'] as $file) {
$env = getcwd().'/'.$file;
if (!file_exists($env)) {
continue;
}

$contents = preg_replace(sprintf('{%s*###> %s ###.*###< %s ###%s+}s', "\n", $recipe->getName(), $recipe->getName(), "\n"), "\n", file_get_contents($env), -1, $count);
if (!$count) {
continue;
}

$this->write(sprintf('Removing environment variables from %s', $file));
file_put_contents($env, $contents);
$contents = preg_replace(sprintf('{%s*###> %s ###.*###< %s ###%s+}s', "\n", $recipe->getName(), $recipe->getName(), "\n"), "\n", file_get_contents($envfile), -1, $count);
if (!$count) {
return;
}
}

private function unconfigurePhpUnit(Recipe $recipe, $vars)
{
foreach (['phpunit.xml.dist', 'phpunit.xml'] as $file) {
$phpunit = getcwd().'/'.$file;
if (!is_file($phpunit)) {
continue;
}

$contents = preg_replace(sprintf('{%s*\s+<!-- ###\+ %s ### -->.*<!-- ###- %s ### -->%s+}s', "\n", $recipe->getName(), $recipe->getName(), "\n"), "\n", file_get_contents($phpunit), -1, $count);
if (!$count) {
continue;
}

$this->write(sprintf('Removed environment variables from %s', $file));
file_put_contents($phpunit, $contents);
}
$this->write(sprintf('Removing environment variables from %s', $envfile));
file_put_contents($envfile, $contents);
}

private function evaluateValue($value)
Expand Down
72 changes: 3 additions & 69 deletions tests/Configurator/EnvConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,10 @@ public function testConfigure()
$recipe = $this->getMockBuilder(Recipe::class)->disableOriginalConstructor()->getMock();
$recipe->expects($this->any())->method('getName')->will($this->returnValue('FooBundle'));

$env = sys_get_temp_dir().'/.env.dist';
$env = sys_get_temp_dir().'/.env';
@unlink($env);
touch($env);

$phpunit = sys_get_temp_dir().'/phpunit.xml';
$phpunitDist = $phpunit.'.dist';
@unlink($phpunit);
@unlink($phpunitDist);
copy(__DIR__.'/../Fixtures/phpunit.xml.dist', $phpunitDist);
copy(__DIR__.'/../Fixtures/phpunit.xml.dist', $phpunit);
$configurator->configure($recipe, [
'APP_ENV' => 'test bar',
'APP_DEBUG' => '0',
Expand Down Expand Up @@ -71,47 +65,9 @@ public function testConfigure()
APP_SECRET="s3cretf0rt3st\"<>"
###< FooBundle ###

EOF;
$xmlContents = <<<EOF
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
>
<php>
<ini name="error_reporting" value="-1" />
<env name="KERNEL_CLASS" value="App\Kernel" />

<!-- ###+ FooBundle ### -->
<env name="APP_ENV" value="test bar"/>
<env name="APP_DEBUG" value="0"/>
<env name="APP_PARAGRAPH" value="foo&#10;&quot;bar&quot;\\t"/>
<env name="DATABASE_URL" value="mysql://[email protected]:3306/symfony?charset=utf8mb4&amp;serverVersion=5.7"/>
<env name="MAILER_URL" value="null://localhost"/>
<env name="MAILER_USER" value="fabien"/>
<!-- Comment 1 -->
<!-- Comment 3 -->
<!-- env name="TRUSTED_SECRET" value="s3cretf0rt3st&quot;&lt;&gt;" -->
<env name="APP_SECRET" value="s3cretf0rt3st&quot;&lt;&gt;"/>
<!-- ###- FooBundle ### -->
</php>

<testsuites>
<testsuite name="Project Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>
</phpunit>

EOF;

$this->assertStringEqualsFile($env, $envContents);
$this->assertStringEqualsFile($phpunitDist, $xmlContents);
$this->assertStringEqualsFile($phpunit, $xmlContents);

$configurator->configure($recipe, [
'APP_ENV' => 'test',
Expand All @@ -123,8 +79,6 @@ public function testConfigure()
]);

$this->assertStringEqualsFile($env, $envContents);
$this->assertStringEqualsFile($phpunitDist, $xmlContents);
$this->assertStringEqualsFile($phpunit, $xmlContents);

$configurator->unconfigure($recipe, [
'APP_ENV' => 'test',
Expand All @@ -142,11 +96,7 @@ public function testConfigure()

EOF
);

$this->assertFileEquals(__DIR__.'/../Fixtures/phpunit.xml.dist', $phpunitDist);
$this->assertFileEquals(__DIR__.'/../Fixtures/phpunit.xml.dist', $phpunit);

@unlink($phpunit, $env);
@unlink($env);
}

public function testConfigureGeneratedSecret()
Expand All @@ -160,16 +110,9 @@ public function testConfigureGeneratedSecret()
$recipe = $this->getMockBuilder(Recipe::class)->disableOriginalConstructor()->getMock();
$recipe->expects($this->any())->method('getName')->will($this->returnValue('FooBundle'));

$env = sys_get_temp_dir().'/.env.dist';
$env = sys_get_temp_dir().'/.env';
@unlink($env);
touch($env);
$phpunit = sys_get_temp_dir().'/phpunit.xml';
$phpunitDist = $phpunit.'.dist';
@unlink($phpunit);
@unlink($phpunitDist);

copy(__DIR__.'/../Fixtures/phpunit.xml.dist', $phpunitDist);
copy(__DIR__.'/../Fixtures/phpunit.xml.dist', $phpunit);

$configurator->configure($recipe, [
'#TRUSTED_SECRET_1' => '%generate(secret,32)%',
Expand All @@ -184,14 +127,5 @@ public function testConfigureGeneratedSecret()
$this->assertRegExp('/#TRUSTED_SECRET_3=[a-z0-9]{64}/', $envContents);
$this->assertRegExp('/APP_SECRET=[a-z0-9]{32}/', $envContents);
@unlink($env);

foreach ([$phpunitDist, $phpunit] as $file) {
$fileContents = file_get_contents($file);

$this->assertRegExp('/<!-- env name="TRUSTED_SECRET_1" value="[a-z0-9]{64}" -->/', $fileContents);
$this->assertRegExp('/<!-- env name="TRUSTED_SECRET_2" value="[a-z0-9]{64}" -->/', $fileContents);
$this->assertRegExp('/<!-- env name="TRUSTED_SECRET_3" value="[a-z0-9]{64}" -->/', $fileContents);
$this->assertRegExp('/<env name="APP_SECRET" value="[a-z0-9]{32}"\/>/', $fileContents);
}
}
}