diff --git a/src/Phar.php b/src/Phar.php index cec050a..5c05fa4 100644 --- a/src/Phar.php +++ b/src/Phar.php @@ -19,9 +19,10 @@ function php_strip_whitespace($file): string { + $lines = file($file, FILE_IGNORE_NEW_LINES); + // First pass, process inline comments // We need to rely on the LF chars here - $lines = file($file, FILE_IGNORE_NEW_LINES); $lines = array_map(function ($line) { if (preg_match('!^//.*$!', $line)) { return null; @@ -55,10 +56,19 @@ function php_strip_whitespace($file): string return true; }); - $lines = implode(" ", $tokens); + $text = implode(" ", $tokens); + $text = preg_replace('/\s\s+/', ' ', $text); + // Restore compatibility for heredoc blocks + // NOTE: Line-breaks inside heredoc are not preserved + preg_match("/.*<<<" . "'([A-Z]+)'/", $text, $m); + for ($i = 1; $i < count($m); $i++) { + $boundary = $m[$i]; + $text = str_replace("<<<'$boundary'", "<<<'$boundary'\n", $text); + $text = str_replace("$boundary;", "\n$boundary;\n", $text); + } - return preg_replace('/\s\s+/', ' ', $lines); + return $text; } class Phar extends BuiltinPhar