Skip to content

Commit fed7ced

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

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/Phar.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,46 @@
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+
return preg_replace('!//.*$!', '', $line);
27+
}, $lines);
28+
29+
// Second pass: multi-line comments
30+
// At this point we can use a token approach
31+
$contents = implode(" ", $lines);
32+
$tokens = explode(" ", $contents);
33+
34+
$tokens = array_filter($tokens, static function ($token) {
35+
static $isMultiLineComment = false;
36+
37+
if ($token == '*/' && $isMultiLineComment) {
38+
$isMultiLineComment = false;
39+
return false;
40+
}
41+
42+
if ($isMultiLineComment)
43+
return false;
44+
45+
if (trim($token) == '/**' || trim($token) == '/*') {
46+
$isMultiLineComment = true;
47+
return false;
48+
}
49+
50+
return true;
51+
52+
});
53+
54+
$lines = implode(" ", $tokens);
55+
56+
57+
return preg_replace('/\s\s+/', ' ', $lines);
58+
}
59+
2060
class Phar extends BuiltinPhar
2161
{
2262
public $files = [];
@@ -35,6 +75,7 @@ public function addFileContents(string $filename, string $localName = null, bool
3575
$this->files[] = $key;
3676

3777
$contents = $minify ? php_strip_whitespace($filename) : file_get_contents($filename);
78+
echo $contents . "\n";
3879
$this[$key] = $contents;
3980
}
4081

0 commit comments

Comments
 (0)