1717
1818use Phar as BuiltinPhar ;
1919
20+ /**
21+ * Replacement for the `php_strip_whitespace()` native method,
22+ * which mistakenly treat php attributes as bash-like comments
23+ *
24+ * @param string $file
25+ *
26+ * @return string
27+ */
28+ function php_strip_whitespace (string $ file ): string
29+ {
30+ $ lines = file ($ file , FILE_IGNORE_NEW_LINES );
31+
32+ // First pass, one-line comments
33+ // Processing is easier using a per-line approach
34+ $ lines = array_map (function ($ line ) {
35+ if (preg_match ('!^(//|#[^\[]).*$! ' , $ line )) {
36+ return null ;
37+ }
38+ // Prevent processing non-comment lines (eg: containing php:// or http:// uris)
39+ // to be mistakenly treated as such
40+ return preg_replace ('!\s(//|#[^\[]).*$! ' , '' , $ line );
41+ // Not working: breaks phpcc self-compiling
42+ //return preg_replace('!([^:])' . '(//|#[^\[])' . '.*$!', '$1', $line);
43+ }, $ lines );
44+
45+ // Second pass: multi-line comments
46+ // At this point we can use a token approach
47+ $ contents = implode (" " , $ lines );
48+ $ tokens = explode (" " , $ contents );
49+
50+ $ tokens = array_filter ($ tokens , static function ($ token ) {
51+ static $ isMultiLineComment = false ;
52+
53+ if ($ token == '*/ ' && $ isMultiLineComment ) {
54+ $ isMultiLineComment = false ;
55+ return false ;
56+ }
57+
58+ if ($ isMultiLineComment )
59+ return false ;
60+
61+ if (trim ($ token ) == '/** ' || trim ($ token ) == '/* ' ) {
62+ $ isMultiLineComment = true ;
63+ return false ;
64+ }
65+
66+ return true ;
67+ });
68+
69+ $ text = implode (" " , $ tokens );
70+ $ text = preg_replace ('/\s\s+/ ' , ' ' , $ text );
71+
72+ // Restore compatibility for heredoc blocks
73+ // NOTE: Line-breaks inside heredoc are not preserved
74+ preg_match ("/.*<<< " . "'([A-Z]+)'/ " , $ text , $ m );
75+ for ($ i = 1 ; $ i < count ($ m ); $ i ++) {
76+ $ boundary = $ m [$ i ];
77+ $ text = str_replace ("<<<' $ boundary' " , "<<<' $ boundary' \n" , $ text );
78+ $ text = str_replace ("$ boundary; " , "\n$ boundary; \n" , $ text );
79+ }
80+
81+ return $ text ;
82+ }
83+
2084class Phar extends BuiltinPhar
2185{
2286 public $ files = [];
@@ -35,6 +99,7 @@ public function addFileContents(string $filename, string $localName = null, bool
3599 $ this ->files [] = $ key ;
36100
37101 $ contents = $ minify ? php_strip_whitespace ($ filename ) : file_get_contents ($ filename );
102+
38103 $ this [$ key ] = $ contents ;
39104 }
40105
0 commit comments