Skip to content

Commit a319588

Browse files
committed
(WIP) Fix php minify feature
- Implement home-made strip method
1 parent 39e35aa commit a319588

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/Phar.php

+45
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-comment lines (eg: containing php:// or http:// uris)
30+
// to be mistakenly treated as such
31+
return preg_replace('!\s//.*$!', '', $line);
32+
}, $lines);
33+
34+
// Second pass: multi-line comments
35+
// At this point we can use a token approach
36+
$contents = implode(" ", $lines);
37+
$tokens = explode(" ", $contents);
38+
39+
$tokens = array_filter($tokens, static function ($token) {
40+
static $isMultiLineComment = false;
41+
42+
if ($token == '*/' && $isMultiLineComment) {
43+
$isMultiLineComment = false;
44+
return false;
45+
}
46+
47+
if ($isMultiLineComment)
48+
return false;
49+
50+
if (trim($token) == '/**' || trim($token) == '/*') {
51+
$isMultiLineComment = true;
52+
return false;
53+
}
54+
55+
return true;
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)