From fed7cede3f4cac73c211a89d45e146a5271097c6 Mon Sep 17 00:00:00 2001 From: Yannoff Date: Thu, 2 May 2024 22:25:56 +0200 Subject: [PATCH] (WIP) Fix php minify feature - Implement home-made strip method --- src/Phar.php | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/Phar.php b/src/Phar.php index 216b02d..faad2ac 100644 --- a/src/Phar.php +++ b/src/Phar.php @@ -17,6 +17,46 @@ use Phar as BuiltinPhar; +function php_strip_whitespace($file): string +{ + // 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) { + return preg_replace('!//.*$!', '', $line); + }, $lines); + + // Second pass: multi-line comments + // At this point we can use a token approach + $contents = implode(" ", $lines); + $tokens = explode(" ", $contents); + + $tokens = array_filter($tokens, static function ($token) { + static $isMultiLineComment = false; + + if ($token == '*/' && $isMultiLineComment) { + $isMultiLineComment = false; + return false; + } + + if ($isMultiLineComment) + return false; + + if (trim($token) == '/**' || trim($token) == '/*') { + $isMultiLineComment = true; + return false; + } + + return true; + + }); + + $lines = implode(" ", $tokens); + + + return preg_replace('/\s\s+/', ' ', $lines); +} + class Phar extends BuiltinPhar { public $files = []; @@ -35,6 +75,7 @@ public function addFileContents(string $filename, string $localName = null, bool $this->files[] = $key; $contents = $minify ? php_strip_whitespace($filename) : file_get_contents($filename); + echo $contents . "\n"; $this[$key] = $contents; }