Skip to content

Commit

Permalink
bug symfony#235 Fix commented env vars support (fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.0-dev branch.

Discussion
----------

Fix commented env vars support

closes symfony#234

Commits
-------

8c93a2a fixed commented env vars support
  • Loading branch information
fabpot committed Dec 7, 2017
2 parents 5c6192e + 8c93a2a commit 73436bc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/Configurator/EnvConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@ private function configureEnvDist(Recipe $recipe, $vars)
if ('%generate(secret)%' === $value) {
$value = bin2hex(random_bytes(16));
}
if ('#' === $key[0]) {
if ('#' === $key[0] && ctype_digit(substr($key, 1))) {
$data .= '# '.$value."\n";
} else {
$value = $this->options->expandTargetDir($value);
if (false !== strpbrk($value, " \t\n&!\"")) {
$value = '"'.str_replace(['\\', '"', "\t", "\n"], ['\\\\', '\\"', '\t', '\n'], $value).'"';
}
$data .= "$key=$value\n";

continue;
}

$value = $this->options->expandTargetDir($value);
if (false !== strpbrk($value, " \t\n&!\"")) {
$value = '"'.str_replace(['\\', '"', "\t", "\n"], ['\\\\', '\\"', '\t', '\n'], $value).'"';
}
$data .= "$key=$value\n";
}
if (!file_exists(getcwd().'/.env')) {
copy($distenv, getcwd().'/.env');
Expand Down Expand Up @@ -80,8 +82,17 @@ private function configurePhpUnit(Recipe $recipe, $vars)
$value = bin2hex(random_bytes(16));
}
if ('#' === $key[0]) {
$doc = new \DOMDocument();
$data .= ' '.$doc->saveXML($doc->createComment(' '.$value.' '))."\n";
if (ctype_digit(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();
Expand Down
5 changes: 5 additions & 0 deletions tests/Configurator/EnvConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function testConfigure()
'MAILER_USER' => 'fabien',
'#1' => 'Comment 1',
'#2' => 'Comment 3',
'#TRUSTED_SECRET' => 's3cretf0rt3st"<>',
'APP_SECRET' => 's3cretf0rt3st"<>',
]);

Expand All @@ -62,6 +63,7 @@ public function testConfigure()
MAILER_USER=fabien
# Comment 1
# Comment 3
#TRUSTED_SECRET="s3cretf0rt3st\"<>"
APP_SECRET="s3cretf0rt3st\"<>"
###< FooBundle ###
Expand Down Expand Up @@ -89,6 +91,7 @@ public function testConfigure()
<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>
Expand All @@ -111,6 +114,7 @@ public function testConfigure()
'APP_DEBUG' => '0',
'#1' => 'Comment 1',
'#2' => 'Comment 3',
'#TRUSTED_SECRET' => 's3cretf0rt3st',
'APP_SECRET' => 's3cretf0rt3st',
]);

Expand All @@ -123,6 +127,7 @@ public function testConfigure()
'APP_DEBUG' => '0',
'#1' => 'Comment 1',
'#2' => 'Comment 3',
'#TRUSTED_SECRET' => 's3cretf0rt3st',
'APP_SECRET' => 's3cretf0rt3st',
]);

Expand Down

0 comments on commit 73436bc

Please sign in to comment.