3
3
* @Author: nguyen
4
4
* @Date: 2020-02-12 14:01:01
5
5
* @Last Modified by: Alex Dong
6
- * @Last Modified time: 2020-05-14 10:00:26
6
+ * @Last Modified time: 2020-05-21 15:12:08
7
7
*/
8
8
9
9
namespace Magepow \Lazyload \Plugin ;
14
14
15
15
class LazyResponse
16
16
{
17
- public $ request ;
17
+ protected $ request ;
18
18
19
- public $ helper ;
19
+ protected $ helper ;
20
20
21
- public $ content ;
21
+ protected $ content ;
22
22
23
- public $ isJson ;
23
+ protected $ isJson ;
24
24
25
- public $ exclude = [];
25
+ protected $ exclude = [];
26
26
27
- public $ placeholder = 'data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22 ' . '$width ' . '%22%20height%3D%22 ' . '$height ' . '%22%20viewBox%3D%220%200%20225%20265%22%3E%3C%2Fsvg%3E ' ;
27
+ protected $ scripts = [];
28
+
29
+ protected $ placeholder = 'data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22 ' . '$width ' . '%22%20height%3D%22 ' . '$height ' . '%22%20viewBox%3D%220%200%20225%20265%22%3E%3C%2Fsvg%3E ' ;
28
30
29
31
public function __construct (
30
32
RequestInterface $ request ,
@@ -65,6 +67,8 @@ public function beforeSendResponse(Http $response)
65
67
66
68
if (!$ this ->helper ->getConfigModule ('general/loading_img ' )) return ;
67
69
70
+ $ body = $ this ->processExcludeJs ($ body );
71
+
68
72
$ exclude = $ this ->helper ->getConfigModule ('general/exclude_img ' );
69
73
// $exclude = 'product-image-photo';
70
74
if ($ exclude ){
@@ -78,6 +82,19 @@ public function beforeSendResponse(Http $response)
78
82
// $regex_block = '';
79
83
$ body = $ this ->addLazyload ($ body , $ placeholder , $ regex_block );
80
84
85
+ $ body = preg_replace_callback (
86
+ '~<\s*\bscript\b[^>]*>(.*?)<\s*\/\s*script\s*>~is ' ,
87
+ function ($ match ){
88
+ $ scriptId = trim ($ match [1 ], ' ' );
89
+ if ($ scriptId && isset ($ this ->scripts [$ scriptId ])){
90
+ return $ this ->scripts [$ scriptId ];
91
+ }else {
92
+ return $ match [0 ];
93
+ }
94
+ },
95
+ $ body
96
+ );
97
+
81
98
$ body_includes = $ this ->helper ->getConfigModule ('general/body_includes ' );
82
99
if ($ body_includes ) $ body = $ this ->addToBottomBody ($ body , $ body_includes );
83
100
@@ -157,6 +174,42 @@ public function isExclude($class)
157
174
return !empty ($ excludeExist );
158
175
}
159
176
177
+ public function processExcludeJs ($ content , $ minify =false , $ deferJs =false )
178
+ {
179
+ $ content = preg_replace_callback (
180
+ '~<\s*\bscript\b[^>]*>(.*?)<\s*\/\s*script\s*>~is ' ,
181
+ function ($ match ) use ($ minify , $ deferJs ){
182
+ // if(stripos($match[0], 'type="text/x-magento') !== false) return $match[0];
183
+ $ scriptId = 'script_ ' . uniqid ();
184
+ if ($ minify && trim ($ match [1 ], ' ' )){
185
+ $ this ->scripts [$ scriptId ] = $ this ->minifyJs ( $ match [0 ] );
186
+ }else {
187
+ $ this ->scripts [$ scriptId ] = $ match [0 ];
188
+ }
189
+ if (!$ deferJs ) return '<script> ' . $ scriptId . '</script> ' ;
190
+ return '' ;
191
+ },
192
+ $ content
193
+ );
194
+
195
+ return $ content ;
196
+ }
197
+
198
+ public function minifyJs ($ script )
199
+ {
200
+ $ regex = '~//?\s*\*[\s\S]*?\*\s*//?~ ' ; // RegEx to remove /** */ and // ** **// php comments
201
+ $ search = array (
202
+ '/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:| \\\| \')\/\/.*))/ ' ,
203
+ '/(\s)+/s ' , // shorten multiple whitespace sequences
204
+ );
205
+
206
+ $ replace = array (
207
+ '' ,
208
+ '\\1 ' ,
209
+ );
210
+ return preg_replace ($ search , $ replace , $ script );
211
+ }
212
+
160
213
public function addLazyloadImage ($ content , $ placeholder )
161
214
{
162
215
if ($ this ->isJson ) return $ this ->addLazyloadImageJson ($ content , $ placeholder );
0 commit comments