|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Requirements_Backend_For_Webpack::set_files_to_ignore( |
|
5
|
|
|
* 'mysite/javascript/myfile.js'; |
|
6
|
|
|
* ); |
|
7
|
|
|
* |
|
8
|
|
|
* |
|
9
|
|
|
*/ |
|
10
|
|
|
class Requirements_Backend_For_Webpack extends Requirements_Backend implements flushable |
|
|
|
|
|
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
private static $webpack_variables_file_location = 'themes/webpack-variables.js'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* we need this method because Requirements_Backend does not extend Object! |
|
21
|
|
|
* @param string |
|
22
|
|
|
*/ |
|
23
|
|
|
public static function set_webpack_variables_file_location($str) |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
self::$webpack_variables_file_location = $tr; |
|
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* e.g. /mysite/javascript/test.js |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
private static $files_to_ignore = array(); |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* we need this method because Requirements_Backend does not extend Object! |
|
36
|
|
|
* @var array $array |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function set_files_to_ignore($array) |
|
39
|
|
|
{ |
|
40
|
|
|
self::$files_to_ignore = $array; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function get_files_to_ignore() |
|
47
|
|
|
{ |
|
48
|
|
|
return self::$files_to_ignore = $array; |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
private static $working_theme_folder_extension = "_mysite"; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* we need this method because Requirements_Backend does not extend Object! |
|
58
|
|
|
* @var string $string |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function set_working_theme_folder_extension($string) |
|
61
|
|
|
{ |
|
62
|
|
|
self::$working_theme_folder_extension = $string; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* we need this method because Requirements_Backend does not extend Object! |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function get_working_theme_folder_extension() |
|
71
|
|
|
{ |
|
72
|
|
|
return self::$working_theme_folder_extension = $string; |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @var string |
|
77
|
|
|
*/ |
|
78
|
|
|
private static $copy_css_to_folder = "src/raw_requirements/css"; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* we need this method because Requirements_Backend does not extend Object! |
|
82
|
|
|
* @var string $string |
|
83
|
|
|
*/ |
|
84
|
|
|
public static function set_copy_css_to_folder($string) |
|
85
|
|
|
{ |
|
86
|
|
|
self::$copy_css_to_folder = $string; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @var string |
|
91
|
|
|
*/ |
|
92
|
|
|
private static $copy_js_to_folder = "src/raw_requirements/js"; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* we need this method because Requirements_Backend does not extend Object! |
|
96
|
|
|
* @param string $string |
|
97
|
|
|
*/ |
|
98
|
|
|
public static function set_copy_js_to_folder($string) |
|
99
|
|
|
{ |
|
100
|
|
|
self::$copy_js_to_folder = $string; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @var array |
|
105
|
|
|
*/ |
|
106
|
|
|
private static $urls_to_exclude = array(); |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* we need this method because Requirements_Backend does not extend Object! |
|
110
|
|
|
* @param array $array |
|
|
|
|
|
|
111
|
|
|
*/ |
|
112
|
|
|
public static function set_urls_to_exclude($a) |
|
113
|
|
|
{ |
|
114
|
|
|
self::$urls_to_exclude = $a; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* |
|
119
|
|
|
* @return array |
|
120
|
|
|
*/ |
|
121
|
|
|
public static function get_urls_to_exclude() |
|
122
|
|
|
{ |
|
123
|
|
|
return self::$urls_to_exclude; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @var bool |
|
128
|
|
|
*/ |
|
129
|
|
|
private static $force_update = true; |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* |
|
133
|
|
|
* @param bool |
|
134
|
|
|
*/ |
|
135
|
|
|
public static function set_force_update($bool) |
|
136
|
|
|
{ |
|
137
|
|
|
self::$force_update = $bool; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* |
|
143
|
|
|
* @return bool |
|
144
|
|
|
*/ |
|
145
|
|
|
public static function get_force_update($bool) |
|
|
|
|
|
|
146
|
|
|
{ |
|
147
|
|
|
return self::$force_update; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Whether to add caching query params to the requests for file-based requirements. |
|
152
|
|
|
* Eg: themes/myTheme/js/main.js?m=123456789. The parameter is a timestamp generated by |
|
153
|
|
|
* filemtime. This has the benefit of allowing the browser to cache the URL infinitely, |
|
154
|
|
|
* while automatically busting this cache every time the file is changed. |
|
155
|
|
|
* |
|
156
|
|
|
* @var bool |
|
157
|
|
|
*/ |
|
158
|
|
|
protected $suffix_requirements = false; |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Whether to combine CSS and JavaScript files |
|
162
|
|
|
* |
|
163
|
|
|
* @var bool |
|
164
|
|
|
*/ |
|
165
|
|
|
protected $combined_files_enabled = false; |
|
166
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Force the JavaScript to the bottom of the page, even if there's a script tag in the body already |
|
170
|
|
|
* |
|
171
|
|
|
* @var boolean |
|
172
|
|
|
*/ |
|
173
|
|
|
protected $force_js_to_bottom = true; |
|
174
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @return string |
|
178
|
|
|
*/ |
|
179
|
|
|
protected static function current_theme_as_set_in_db() |
|
180
|
|
|
{ |
|
181
|
|
|
$v = SiteConfig::current_site_config()->Theme; |
|
182
|
|
|
if(! $v) { |
|
183
|
|
|
$v = Config::inst()->get('SSViewer', 'current_theme'); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return $v; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Update the given HTML content with the appropriate include tags for the registered |
|
192
|
|
|
* requirements. Needs to receive a valid HTML/XHTML template in the $content parameter, |
|
193
|
|
|
* including a head and body tag. |
|
194
|
|
|
* |
|
195
|
|
|
* @param string $templateFile No longer used, only retained for compatibility |
|
196
|
|
|
* @param string $content HTML content that has already been parsed from the $templateFile |
|
197
|
|
|
* through {@link SSViewer} |
|
198
|
|
|
* @return string HTML content augmented with the requirements tags |
|
199
|
|
|
*/ |
|
200
|
|
|
public function includeInHTML($templateFile, $content) |
|
201
|
|
|
{ |
|
202
|
|
|
if ($this->themedRequest()) { |
|
203
|
|
|
|
|
204
|
|
|
//===================================================================== |
|
205
|
|
|
// start copy-ish from parent class |
|
206
|
|
|
|
|
207
|
|
|
$hasHead = (strpos($content, '</head>') !== false || strpos($content, '</head ') !== false) ? true : false; |
|
208
|
|
|
$hasRequirements = ($this->css || $this->javascript || $this->customCSS || $this->customScript || $this->customHeadTags) ? true: false; |
|
|
|
|
|
|
209
|
|
|
if ($hasHead && $hasRequirements) { |
|
210
|
|
|
$requirements = ''; |
|
211
|
|
|
$jsRequirements = ''; |
|
212
|
|
|
$requirementsCSSFiles = array(); |
|
213
|
|
|
$requirementsJSFiles = array(); |
|
214
|
|
|
|
|
215
|
|
|
// Combine files - updates $this->javascript and $this->css |
|
216
|
|
|
$this->process_combined_files(); |
|
217
|
|
|
$isDev = Director::isDev(); |
|
218
|
|
|
foreach (array_diff_key($this->javascript, $this->blocked) as $file => $dummy) { |
|
219
|
|
|
$ignore = in_array($file, self::$files_to_ignore) ? true : false; |
|
220
|
|
|
if($isDev || $ignore) { |
|
221
|
|
|
$path = Convert::raw2xml($this->path_for_file($file)); |
|
|
|
|
|
|
222
|
|
|
if ($path) { |
|
223
|
|
|
if ($isDev) { |
|
224
|
|
|
$requirementsJSFiles[$path] = $path; |
|
225
|
|
|
} |
|
226
|
|
|
if(in_array($file, self::$files_to_ignore)) { |
|
227
|
|
|
$jsRequirements .= "<script type=\"text/javascript\" src=\"$path\"></script>\n"; |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
// Add all inline JavaScript *after* including external files they might rely on |
|
234
|
|
|
if ($this->customScript) { |
|
|
|
|
|
|
235
|
|
|
foreach (array_diff_key($this->customScript, $this->blocked) as $script) { |
|
236
|
|
|
$jsRequirements .= "<script type=\"text/javascript\">\n//<![CDATA[\n"; |
|
237
|
|
|
$jsRequirements .= "$script\n"; |
|
238
|
|
|
$jsRequirements .= "\n//]]>\n</script>\n"; |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
foreach (array_diff_key($this->css, $this->blocked) as $file => $params) { |
|
243
|
|
|
$ignore = in_array($file, self::$files_to_ignore) ? true : false; |
|
244
|
|
|
if($isDev || $ignore) { |
|
245
|
|
|
$path = Convert::raw2xml($this->path_for_file($file)); |
|
|
|
|
|
|
246
|
|
|
if ($path) { |
|
247
|
|
|
$media = (isset($params['media']) && !empty($params['media'])) ? $params['media'] : ""; |
|
248
|
|
|
if ($isDev) { |
|
249
|
|
|
$requirementsCSSFiles[$path."_".$media] = $path; |
|
250
|
|
|
} |
|
251
|
|
|
if($ignore) { |
|
252
|
|
|
if($media !== '') { |
|
253
|
|
|
$media = " media=\"{$media}\""; |
|
254
|
|
|
} |
|
255
|
|
|
$requirements .= "<link rel=\"stylesheet\" type=\"text/css\"{$media} href=\"$path\" />\n"; |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
foreach (array_diff_key($this->customCSS, $this->blocked) as $css) { |
|
262
|
|
|
$requirements .= "<style type=\"text/css\">\n$css\n</style>\n"; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
foreach (array_diff_key($this->customHeadTags, $this->blocked) as $customHeadTag) { |
|
266
|
|
|
$requirements .= "$customHeadTag\n"; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
// Remove all newlines from code to preserve layout |
|
270
|
|
|
$jsRequirements = preg_replace('/>\n*/', '>', $jsRequirements); |
|
271
|
|
|
|
|
272
|
|
|
// Forcefully put the scripts at the bottom of the body instead of before the first |
|
273
|
|
|
// script tag. |
|
274
|
|
|
$content = preg_replace("/(<\/body[^>]*>)/i", $jsRequirements . "\\1", $content); |
|
275
|
|
|
|
|
276
|
|
|
// Put CSS at the bottom of the head |
|
277
|
|
|
$content = preg_replace("/(<\/head>)/i", $requirements . "\\1", $content); |
|
278
|
|
|
|
|
279
|
|
|
//end copy-ish from parent class |
|
280
|
|
|
//===================================================================== |
|
281
|
|
|
|
|
282
|
|
|
//copy files ... |
|
283
|
|
|
if ($this->canSaveRequirements()) { |
|
284
|
|
|
|
|
285
|
|
|
//css |
|
286
|
|
|
$cssFolder = '/themes/'.self::current_theme_as_set_in_db().self::$working_theme_folder_extension.'/'.self::$copy_css_to_folder; |
|
287
|
|
|
|
|
288
|
|
|
foreach ($requirementsCSSFiles as $cssFile) { |
|
289
|
|
|
$this->moveFileToRequirementsFolder($cssFile, $cssFolder); |
|
290
|
|
|
} |
|
291
|
|
|
//js |
|
292
|
|
|
$jsFolder = '/themes/'.self::current_theme_as_set_in_db().self::$working_theme_folder_extension.'/'.self::$copy_js_to_folder; |
|
293
|
|
|
foreach ($requirementsJSFiles as $jsFile) { |
|
294
|
|
|
$this->moveFileToRequirementsFolder($jsFile, $jsFolder); |
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
} |
|
298
|
|
|
return $content; |
|
299
|
|
|
} else { |
|
300
|
|
|
return parent::includeInHTML($templateFile, $content); |
|
301
|
|
|
} |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* Attach requirements inclusion to X-Include-JS and X-Include-CSS headers on the given |
|
306
|
|
|
* HTTP Response |
|
307
|
|
|
* |
|
308
|
|
|
* @param SS_HTTPResponse $response |
|
309
|
|
|
*/ |
|
310
|
|
|
public function include_in_response(SS_HTTPResponse $response) |
|
311
|
|
|
{ |
|
312
|
|
|
if ($this->themedRequest()) { |
|
|
|
|
|
|
313
|
|
|
//do nothing |
|
314
|
|
|
} else { |
|
315
|
|
|
return parent::include_in_response($response); |
|
316
|
|
|
} |
|
317
|
|
|
//$this->process_combined_files(); |
|
|
|
|
|
|
318
|
|
|
//do nothing ... |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* |
|
323
|
|
|
* |
|
324
|
|
|
* |
|
325
|
|
|
* @return bool |
|
|
|
|
|
|
326
|
|
|
*/ |
|
327
|
|
|
protected function canSaveRequirements() |
|
328
|
|
|
{ |
|
329
|
|
|
if (Director::isDev()) { |
|
330
|
|
|
if ($this->themedRequest()) { |
|
331
|
|
|
$socket = @fsockopen('localhost', 3000, $errno, $errstr, 1); |
|
332
|
|
|
if ($socket) { |
|
333
|
|
|
return true; |
|
334
|
|
|
} |
|
335
|
|
|
} |
|
336
|
|
|
} |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
/** |
|
340
|
|
|
* |
|
341
|
|
|
* |
|
342
|
|
|
* @return bool |
|
343
|
|
|
*/ |
|
344
|
|
|
protected function themedRequest() |
|
345
|
|
|
{ |
|
346
|
|
|
return Config::inst()->get('SSViewer', 'theme') && Config::inst()->get('SSViewer', 'theme_enabled') ? true : false; |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
protected function moveFileToRequirementsFolder($fileLocation, $folderLocation) |
|
|
|
|
|
|
350
|
|
|
{ |
|
351
|
|
|
$base = Director::baseFolder(); |
|
352
|
|
|
$folderLocationWithBase = $base . $folderLocation; |
|
353
|
|
|
Filesystem::makeFolder($folderLocationWithBase); |
|
354
|
|
|
if (!file_exists($folderLocationWithBase)) { |
|
355
|
|
|
user_error('Please update Requirements_Backend_For_Webpack for the right folder or create '.$folderLocationWithBase); |
|
356
|
|
|
} |
|
357
|
|
|
if (strpos($fileLocation, "//") !== false) { |
|
358
|
|
|
$logFile = $folderLocationWithBase."/EXTERNALS.log"; |
|
359
|
|
|
$lines = array(); |
|
360
|
|
|
$line = $_SERVER['REQUEST_URI']." | ".$fileLocation."\n"; |
|
361
|
|
|
if (file_exists($logFile)) { |
|
362
|
|
|
$lines = file($logFile); |
|
363
|
|
|
} |
|
364
|
|
|
if (! in_array($line, $lines)) { |
|
365
|
|
|
$this->addLinesToFile($logFile, $fileLocation); |
|
366
|
|
|
} |
|
367
|
|
|
} else { |
|
368
|
|
|
$from = $fileLocation; |
|
369
|
|
|
$to = basename($fileLocation); |
|
370
|
|
|
$line = '"cp .'.$from.' .'.$folderLocation.$to.'",'."\n"; |
|
371
|
|
|
$from = $base.$from; |
|
372
|
|
|
$to = $folderLocationWithBase . '/' . $to; |
|
373
|
|
|
$logFile = $folderLocationWithBase."/TO.INCLUDE.IN.COMPOSER.log"; |
|
374
|
|
|
$lines = array(); |
|
375
|
|
|
if (file_exists($logFile)) { |
|
376
|
|
|
$lines = file($logFile); |
|
377
|
|
|
} |
|
378
|
|
|
if (! in_array($line, $lines)) { |
|
379
|
|
|
$this->addLinesToFile($logFile, $line); |
|
380
|
|
|
} |
|
381
|
|
|
if (in_array($fileLocation, self::$files_to_ignore)) { |
|
|
|
|
|
|
382
|
|
|
//to be completed ... |
|
383
|
|
|
} else { |
|
384
|
|
|
if (! file_exists($to) || self::$force_update) { |
|
385
|
|
|
copy($from, $to); |
|
386
|
|
|
} |
|
387
|
|
|
} |
|
388
|
|
|
} |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
protected $addLinesToFileCount = 0; |
|
392
|
|
|
|
|
393
|
|
|
protected function copyIfYouCan($from, $to) |
|
394
|
|
|
{ |
|
395
|
|
|
try { |
|
396
|
|
|
copy($from, $to); |
|
397
|
|
|
} |
|
398
|
|
|
catch(Exception $e) { |
|
399
|
|
|
$this->makeFolderWritable(); |
|
|
|
|
|
|
400
|
|
|
$this->copyIfYouCan($from, $to); |
|
401
|
|
|
} |
|
402
|
|
|
} |
|
403
|
|
|
|
|
404
|
|
|
protected function addLinesToFile($fileLocation, $line) |
|
405
|
|
|
{ |
|
406
|
|
|
try { |
|
407
|
|
|
$handle = fopen($fileLocation, 'a'); |
|
408
|
|
|
fwrite($handle, $line); |
|
409
|
|
|
} |
|
410
|
|
|
catch(Exception $e) { |
|
411
|
|
|
$this->makeFolderWritable(); |
|
|
|
|
|
|
412
|
|
|
$this->addLinesToFile($fileLocation, $lines); |
|
|
|
|
|
|
413
|
|
|
} |
|
414
|
|
|
} |
|
415
|
|
|
|
|
416
|
|
|
protected function makeFolderWritable($fileLocation) |
|
417
|
|
|
{ |
|
418
|
|
|
if(file_exists($fileLocation)) { |
|
419
|
|
|
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(dirname($fileLocation))); |
|
420
|
|
|
foreach($iterator as $item) { |
|
421
|
|
|
chmod($item, '0664'); |
|
422
|
|
|
} |
|
423
|
|
|
if($this->addLinesToFileCount < 3) { |
|
424
|
|
|
$this->addLinesToFileCount++; |
|
425
|
|
|
} |
|
426
|
|
|
} |
|
427
|
|
|
} |
|
428
|
|
|
|
|
429
|
|
|
|
|
430
|
|
|
public static function flush() |
|
|
|
|
|
|
431
|
|
|
{ |
|
432
|
|
|
//make raw requirements writeable |
|
433
|
|
|
$base = Director::baseFolder(); |
|
434
|
|
|
$rawFolders = [ |
|
435
|
|
|
$base.'/'.self::$copy_css_to_folder, |
|
436
|
|
|
$base.'/'.self::$copy_js_to_folder |
|
437
|
|
|
]; |
|
438
|
|
|
$obj = singleton('Requirements_Backend_For_Webpack'); |
|
439
|
|
|
$obj->makeFolderWritable($rawFolders[0]); |
|
440
|
|
|
$obj->makeFolderWritable($rawFolders[1]); |
|
441
|
|
|
|
|
442
|
|
|
|
|
443
|
|
|
// |
|
444
|
|
|
$varArray = [ |
|
445
|
|
|
'themeName' => self::current_theme_as_set_in_db(), |
|
446
|
|
|
'devWebAddress' => $_SERVER['HTTP_HOST'], |
|
447
|
|
|
'distributionFolder' => self::current_theme_as_set_in_db().'_'.Config::inst()->get('WebpackPageControllerExtension', 'webpack_distribution_folder_extension') |
|
448
|
|
|
]; |
|
449
|
|
|
$str = 'module.exports = '.json_encode($varArray).''; |
|
450
|
|
|
file_put_contents($base.'/'.self::$webpack_variables_file_location, $str); |
|
451
|
|
|
} |
|
452
|
|
|
|
|
453
|
|
|
|
|
454
|
|
|
} |
|
455
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.