Skip to content

Commit

Permalink
(WIP) Experimental/Working Support heredocs
Browse files Browse the repository at this point in the history
  • Loading branch information
yannoff committed May 3, 2024
1 parent a319588 commit 3f80efd
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/Phar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3f80efd

Please sign in to comment.