1
1
<?php
2
2
3
3
namespace Agentur1601com \FileLazyLoader \EventListener \Loader ;
4
+
4
5
use Contao \LayoutModel ;
5
6
use MatthiasMullie \Minify ;
6
7
use Contao \Combiner ;
@@ -16,26 +17,22 @@ class JsLoader
16
17
*/
17
18
public function returnMultiColumnWizardArray ($ foundedFiles , $ savedFiles , bool $ removeExternFiles = false )
18
19
{
19
- if (!is_array ($ savedFiles )) {
20
+ if (!is_array ($ savedFiles )) {
20
21
$ savedFiles = [];
21
22
}
22
23
23
24
$ savedFiles = array_reverse ($ savedFiles );
24
25
25
26
$ returnArray = [];
26
27
27
- foreach ($ savedFiles as $ savedFile )
28
- {
29
- if ($ savedFile ['js_files_extFile ' ] === "1 " && !$ removeExternFiles )
30
- {
28
+ foreach ($ savedFiles as $ savedFile ) {
29
+ if ($ savedFile ['js_files_extFile ' ] === "1 " && !$ removeExternFiles ) {
31
30
array_unshift ($ returnArray , $ savedFile );
32
31
continue ;
33
32
}
34
33
35
- foreach ($ foundedFiles as $ pathLoadedKey => $ pathLoadedFile )
36
- {
37
- if (str_replace (TL_ROOT ,"" , $ pathLoadedFile ) === $ savedFile ['js_files_path ' ])
38
- {
34
+ foreach ($ foundedFiles as $ pathLoadedKey => $ pathLoadedFile ) {
35
+ if (str_replace (TL_ROOT , "" , $ pathLoadedFile ) === $ savedFile ['js_files_path ' ]) {
39
36
$ arrayValue = $ this ->setWizardArray (
40
37
$ savedFile ['select ' ],
41
38
$ savedFile ['js_files_path ' ],
@@ -44,8 +41,7 @@ public function returnMultiColumnWizardArray($foundedFiles, $savedFiles, bool $r
44
41
$ savedFile ['js_minimize ' ]
45
42
);
46
43
47
- if ($ savedFile ['select ' ])
48
- {
44
+ if ($ savedFile ['select ' ]) {
49
45
array_unshift ($ returnArray , $ arrayValue );
50
46
unset($ foundedFiles [$ pathLoadedKey ]);
51
47
}
@@ -55,9 +51,8 @@ public function returnMultiColumnWizardArray($foundedFiles, $savedFiles, bool $r
55
51
}
56
52
}
57
53
58
- foreach ($ foundedFiles as $ pathLoadedFile )
59
- {
60
- $ tmpPath = str_replace (TL_ROOT ,"" , $ pathLoadedFile );
54
+ foreach ($ foundedFiles as $ pathLoadedFile ) {
55
+ $ tmpPath = str_replace (TL_ROOT , "" , $ pathLoadedFile );
61
56
$ returnArray [] = $ this ->setWizardArray (
62
57
"" ,
63
58
$ tmpPath
@@ -74,10 +69,8 @@ public function returnMultiColumnWizardArray($foundedFiles, $savedFiles, bool $r
74
69
*/
75
70
public function removeExtFiles ($ filesArray )
76
71
{
77
- foreach ($ filesArray as &$ file )
78
- {
79
- if ($ file ['js_files_extFile ' ] === '1 ' )
80
- {
72
+ foreach ($ filesArray as &$ file ) {
73
+ if ($ file ['js_files_extFile ' ] === '1 ' ) {
81
74
unset($ file );
82
75
}
83
76
}
@@ -106,10 +99,8 @@ public function minimizeFile($sourceFile, $destinationFile)
106
99
*/
107
100
public function loadJs ($ objPage , $ objLayout , $ objPageRegular )
108
101
{
109
- if (isset ($ objLayout ->fileLazyLoaderJsFiles ) && is_array (($ jsFilesArray = unserialize ($ objLayout ->fileLazyLoaderJsFiles ))))
110
- {
111
- if ($ objLayout ->fileLazyLoaderModifyExtJs === '1 ' )
112
- {
102
+ if (isset ($ objLayout ->fileLazyLoaderJsFiles ) && is_array (($ jsFilesArray = unserialize ($ objLayout ->fileLazyLoaderJsFiles )))) {
103
+ if ($ objLayout ->fileLazyLoaderModifyExtJs === '1 ' ) {
113
104
#require(TL_ROOT . '/system/initialize.php');
114
105
115
106
$ jsFilesArray = $ this ->mergeWizardArrByGlobalScriptArr (
@@ -127,25 +118,21 @@ public function loadJs($objPage, $objLayout, $objPageRegular)
127
118
$ GLOBALS ['TL_JAVASCRIPT ' ] = [];
128
119
}
129
120
130
- foreach ($ jsFilesArray as $ jsFile )
131
- {
121
+ foreach ($ jsFilesArray as $ jsFile ) {
132
122
$ link = $ this ->removeTrailingSlash ($ jsFile ["js_files_path " ]);
133
123
134
- if (!$ jsFile ["select " ] || !file_exists (TL_ROOT . $ link ))
135
- {
124
+ if (!$ jsFile ["select " ] || !file_exists (TL_ROOT . $ link )) {
136
125
continue ;
137
126
}
138
127
139
- if ($ jsFile ['js_minimize ' ] && $ jsFile ['js_files_extFile ' ] !== '1 ' )
140
- {
128
+ if ($ jsFile ['js_minimize ' ] && $ jsFile ['js_files_extFile ' ] !== '1 ' ) {
141
129
$ Combinder = new Combiner ();
142
130
$ Combinder ->add ($ jsFile ["js_files_path " ]);
143
131
$ link = $ Combinder ->getCombinedFile ();
144
- $ this ->minimizeFile (TL_ROOT . $ jsFile ["js_files_path " ],TL_ROOT . "/ " . $ link );
132
+ $ this ->minimizeFile (TL_ROOT . $ jsFile ["js_files_path " ], TL_ROOT . "/ " . $ link );
145
133
}
146
134
147
- switch ($ jsFile ["js_param " ])
148
- {
135
+ switch ($ jsFile ["js_param " ]) {
149
136
case 'footer ' :
150
137
$ GLOBALS ['TL_BODY ' ][] = "<script src=' " . $ link . "'> " ;
151
138
break ;
@@ -154,15 +141,15 @@ public function loadJs($objPage, $objLayout, $objPageRegular)
154
141
$ GLOBALS ['TL_JAVASCRIPT ' ][] = $ link ;
155
142
break ;
156
143
case 'preload_push ' :
157
- header ("Link: < " . $ link . ">; rel=preload; as=script " ,false );
144
+ header ("Link: < " . $ link . ">; rel=preload; as=script " , false );
158
145
$ GLOBALS ['TL_JAVASCRIPT ' ][] = $ link ;
159
146
break ;
160
147
case 'defer ' :
161
- $ GLOBALS ['TL_HEAD ' ][] = "<script src=' " . $ link. "' defer></script> " ;
148
+ $ GLOBALS ['TL_HEAD ' ][] = "<script src=' " . $ link . "' defer></script> " ;
162
149
break ;
163
150
default :
164
151
$ jsFile ["js_param " ] = empty ($ jsFile ["js_param " ]) ? 'static ' : $ jsFile ["js_param " ];
165
- $ GLOBALS ['TL_JAVASCRIPT ' ][] = $ link. "| " . $ jsFile ["js_param " ];
152
+ $ GLOBALS ['TL_JAVASCRIPT ' ][] = $ link . "| " . $ jsFile ["js_param " ];
166
153
}
167
154
}
168
155
}
@@ -174,21 +161,16 @@ public function loadJs($objPage, $objLayout, $objPageRegular)
174
161
*/
175
162
private function parseGlobalJsPaths (array $ globalJsPaths = [])
176
163
{
177
- if (!is_array ($ globalJsPaths ))
178
- {
164
+ if (!is_array ($ globalJsPaths )) {
179
165
return [];
180
166
}
181
167
182
168
$ returnArr = [];
183
169
184
- foreach ($ globalJsPaths as $ key => $ pathValue )
185
- {
186
- if (is_string ($ key ))
187
- {
170
+ foreach ($ globalJsPaths as $ key => $ pathValue ) {
171
+ if (is_string ($ key )) {
188
172
$ returnArr [$ key ] = explode ('| ' , $ pathValue );
189
- }
190
- else
191
- {
173
+ } else {
192
174
$ returnArr [] = explode ('| ' , $ pathValue );
193
175
}
194
176
}
@@ -215,12 +197,12 @@ private function setWizardArray(
215
197
)
216
198
{
217
199
return [
218
- "select " => $ select ,
219
- "js_files_path " => $ js_files_path ,
200
+ "select " => $ select ,
201
+ "js_files_path " => $ js_files_path ,
220
202
"js_files_path_min " => $ js_files_path_min ,
221
- "js_param " => $ js_param ,
222
- "js_minimize " => $ js_minimize ,
223
- "js_files_extFile " => $ js_files_extFile ,
203
+ "js_param " => $ js_param ,
204
+ "js_minimize " => $ js_minimize ,
205
+ "js_files_extFile " => $ js_files_extFile ,
224
206
];
225
207
}
226
208
@@ -231,28 +213,22 @@ private function setWizardArray(
231
213
*/
232
214
private function mergeWizardArrByGlobalScriptArr (array $ wizardArray , array $ mergeArray )
233
215
{
234
- foreach ($ wizardArray as &$ wizardElem )
235
- {
236
- foreach ($ mergeArray as $ key => &$ mergeElem )
237
- {
238
- if ($ wizardElem ['js_files_path ' ] === $ mergeElem [0 ])
239
- {
216
+ foreach ($ wizardArray as &$ wizardElem ) {
217
+ foreach ($ mergeArray as $ key => &$ mergeElem ) {
218
+ if ($ wizardElem ['js_files_path ' ] === $ mergeElem [0 ]) {
240
219
$ wizardElem ['verified ' ] = 1 ;
241
220
unset($ mergeArray [$ key ]);
242
221
}
243
222
}
244
223
245
- if ($ wizardElem ['js_files_extFile ' ] === '1 ' && $ wizardElem ['verified ' ] !== '1 ' )
246
- {
224
+ if ($ wizardElem ['js_files_extFile ' ] === '1 ' && $ wizardElem ['verified ' ] !== '1 ' ) {
247
225
unset($ wizardElem );
248
- }
249
- else {
226
+ } else {
250
227
unset($ wizardElem ['verified ' ]);
251
228
}
252
229
}
253
230
254
- foreach ($ mergeArray as $ key => $ mergeElem )
255
- {
231
+ foreach ($ mergeArray as $ key => $ mergeElem ) {
256
232
$ wizardArrayElem = $ this ->setWizardArray (
257
233
'1 ' ,
258
234
$ mergeElem [0 ],
@@ -262,12 +238,9 @@ private function mergeWizardArrByGlobalScriptArr(array $wizardArray, array $merg
262
238
'1 '
263
239
);
264
240
265
- if (is_string ($ key ) && !empty ($ key ))
266
- {
241
+ if (is_string ($ key ) && !empty ($ key )) {
267
242
$ wizardArray [$ key ] = $ wizardArrayElem ;
268
- }
269
- else
270
- {
243
+ } else {
271
244
array_unshift ($ wizardArray , $ wizardArrayElem );
272
245
}
273
246
}
@@ -281,7 +254,7 @@ private function mergeWizardArrByGlobalScriptArr(array $wizardArray, array $merg
281
254
*/
282
255
private function removeTrailingSlash (string $ path )
283
256
{
284
- return '/ ' . ltrim (rtrim ($ path ,'/ ' ), '/ ' );
257
+ return '/ ' . ltrim (rtrim ($ path , '/ ' ), '/ ' );
285
258
}
286
259
287
260
}
0 commit comments