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 dd4ca46
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/Phar.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,50 @@

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) {
if (preg_match('!^//.*$!', $line)) {
return null;
}
// Prevent processing non-coment lines (eg: containing php:// or http:// uris)
return preg_replace('!\s//.*$!', '', $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 +79,7 @@ public function addFileContents(string $filename, string $localName = null, bool
$this->files[] = $key;

$contents = $minify ? php_strip_whitespace($filename) : file_get_contents($filename);

$this[$key] = $contents;
}

Expand Down

0 comments on commit dd4ca46

Please sign in to comment.