Skip to content

Commit

Permalink
(WIP) Fix php minify feature
Browse files Browse the repository at this point in the history
- Implement home-made strip method
  • Loading branch information
yannoff committed May 2, 2024
1 parent 5294346 commit fed7ced
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/Phar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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;
}

Expand Down

0 comments on commit fed7ced

Please sign in to comment.