Fix .env with number symbols (#10497)

* Fix .env with number symbols

* Handle tabs and trim when quoting so we don't include spaces
This commit is contained in:
Tony Murray
2019-08-06 22:03:13 -05:00
committed by GitHub
parent 0a6ded9ba3
commit 2d2a55e47d

View File

@@ -146,6 +146,8 @@ class ComposerHelper
}
}
$content = self::fixComments($content);
// only write if the content has changed
if ($content !== $original_content) {
file_put_contents($file, $content);
@@ -172,4 +174,22 @@ class ComposerHelper
$cmd = "set -v\n" . implode(PHP_EOL, (array)$cmds);
passthru($cmd);
}
/**
* Fix .env with # in them without a space before it
*/
private static function fixComments($dotenv)
{
return implode(PHP_EOL, array_map(function ($line) {
$parts = explode('=', $line, 2);
if (isset($parts[1])
&& preg_match('/(?<!\s)#/', $parts[1]) // number symbol without a space before it
&& !preg_match('/^(".*"|\'.*\')$/', $parts[1]) // not already quoted
) {
return trim($parts[0]) . '="' . trim($parts[1]) . '"';
}
return $line;
}, explode(PHP_EOL, $dotenv)));
}
}