Skip to content

Commit dd4ca46

Browse files
committed
(WIP) Fix php minify feature
- Implement home-made strip method
1 parent 5294346 commit dd4ca46

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/Phar.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,50 @@
1717

1818
use Phar as BuiltinPhar;
1919

20+
function php_strip_whitespace($file): string
21+
{
22+
// First pass, process inline comments
23+
// We need to rely on the LF chars here
24+
$lines = file($file, FILE_IGNORE_NEW_LINES);
25+
$lines = array_map(function ($line) {
26+
if (preg_match('!^//.*$!', $line)) {
27+
return null;
28+
}
29+
// Prevent processing non-coment lines (eg: containing php:// or http:// uris)
30+
return preg_replace('!\s//.*$!', '', $line);
31+
}, $lines);
32+
33+
// Second pass: multi-line comments
34+
// At this point we can use a token approach
35+
$contents = implode(" ", $lines);
36+
$tokens = explode(" ", $contents);
37+
38+
$tokens = array_filter($tokens, static function ($token) {
39+
static $isMultiLineComment = false;
40+
41+
if ($token == '*/' && $isMultiLineComment) {
42+
$isMultiLineComment = false;
43+
return false;
44+
}
45+
46+
if ($isMultiLineComment)
47+
return false;
48+
49+
if (trim($token) == '/**' || trim($token) == '/*') {
50+
$isMultiLineComment = true;
51+
return false;
52+
}
53+
54+
return true;
55+
56+
});
57+
58+
$lines = implode(" ", $tokens);
59+
60+
61+
return preg_replace('/\s\s+/', ' ', $lines);
62+
}
63+
2064
class Phar extends BuiltinPhar
2165
{
2266
public $files = [];
@@ -35,6 +79,7 @@ public function addFileContents(string $filename, string $localName = null, bool
3579
$this->files[] = $key;
3680

3781
$contents = $minify ? php_strip_whitespace($filename) : file_get_contents($filename);
82+
3883
$this[$key] = $contents;
3984
}
4085

0 commit comments

Comments
 (0)