17
17
18
18
use Phar as BuiltinPhar ;
19
19
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
+
20
60
class Phar extends BuiltinPhar
21
61
{
22
62
public $ files = [];
@@ -35,6 +75,7 @@ public function addFileContents(string $filename, string $localName = null, bool
35
75
$ this ->files [] = $ key ;
36
76
37
77
$ contents = $ minify ? php_strip_whitespace ($ filename ) : file_get_contents ($ filename );
78
+ echo $ contents . "\n" ;
38
79
$ this [$ key ] = $ contents ;
39
80
}
40
81
0 commit comments