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
+ 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
+
20
64
class Phar extends BuiltinPhar
21
65
{
22
66
public $ files = [];
@@ -35,6 +79,7 @@ public function addFileContents(string $filename, string $localName = null, bool
35
79
$ this ->files [] = $ key ;
36
80
37
81
$ contents = $ minify ? php_strip_whitespace ($ filename ) : file_get_contents ($ filename );
82
+
38
83
$ this [$ key ] = $ contents ;
39
84
}
40
85
0 commit comments