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 webpack_current_theme_as_set_in_db() |
180
|
|
|
{ |
181
|
|
|
$v = null; |
182
|
|
|
if(Security::database_is_ready()) { |
183
|
|
|
try{ |
184
|
|
|
$v = SiteConfig::current_site_config()->Theme; |
185
|
|
|
} |
186
|
|
|
catch (Exception $e) { |
187
|
|
|
//dont worry! |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
if(! $v) { |
191
|
|
|
$v = Config::inst()->get('SSViewer', 'current_theme'); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $v; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return string |
200
|
|
|
*/ |
201
|
|
|
protected static function webpack_theme_folder_for_customisation() |
202
|
|
|
{ |
203
|
|
|
return '/themes/'.self::webpack_current_theme_as_set_in_db().'_'.self::$working_theme_folder_extension.'/'; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Update the given HTML content with the appropriate include tags for the registered |
209
|
|
|
* requirements. Needs to receive a valid HTML/XHTML template in the $content parameter, |
210
|
|
|
* including a head and body tag. |
211
|
|
|
* |
212
|
|
|
* @param string $templateFile No longer used, only retained for compatibility |
213
|
|
|
* @param string $content HTML content that has already been parsed from the $templateFile |
214
|
|
|
* through {@link SSViewer} |
215
|
|
|
* @return string HTML content augmented with the requirements tags |
216
|
|
|
*/ |
217
|
|
|
public function includeInHTML($templateFile, $content) |
218
|
|
|
{ |
219
|
|
|
if ($this->themedRequest()) { |
220
|
|
|
|
221
|
|
|
//===================================================================== |
222
|
|
|
// start copy-ish from parent class |
223
|
|
|
|
224
|
|
|
$hasHead = (strpos($content, '</head>') !== false || strpos($content, '</head ') !== false) ? true : false; |
225
|
|
|
$hasRequirements = ($this->css || $this->javascript || $this->customCSS || $this->customScript || $this->customHeadTags) ? true: false; |
|
|
|
|
226
|
|
|
if ($hasHead && $hasRequirements) { |
227
|
|
|
$requirements = ''; |
228
|
|
|
$jsRequirements = ''; |
229
|
|
|
$requirementsCSSFiles = array(); |
230
|
|
|
$requirementsJSFiles = array(); |
231
|
|
|
|
232
|
|
|
// Combine files - updates $this->javascript and $this->css |
233
|
|
|
$this->process_combined_files(); |
234
|
|
|
$isDev = Director::isDev(); |
235
|
|
|
foreach (array_diff_key($this->javascript, $this->blocked) as $file => $dummy) { |
236
|
|
|
$ignore = in_array($file, self::$files_to_ignore) ? true : false; |
237
|
|
|
if($isDev || $ignore) { |
238
|
|
|
$path = Convert::raw2xml($this->path_for_file($file)); |
|
|
|
|
239
|
|
|
if ($path) { |
240
|
|
|
if ($isDev) { |
241
|
|
|
$requirementsJSFiles[$path] = $path; |
242
|
|
|
} |
243
|
|
|
if(in_array($file, self::$files_to_ignore)) { |
244
|
|
|
$jsRequirements .= "<script type=\"text/javascript\" src=\"$path\"></script>\n"; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
// Add all inline JavaScript *after* including external files they might rely on |
251
|
|
|
if ($this->customScript) { |
|
|
|
|
252
|
|
|
foreach (array_diff_key($this->customScript, $this->blocked) as $script) { |
253
|
|
|
$jsRequirements .= "<script type=\"text/javascript\">\n//<![CDATA[\n"; |
254
|
|
|
$jsRequirements .= "$script\n"; |
255
|
|
|
$jsRequirements .= "\n//]]>\n</script>\n"; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
foreach (array_diff_key($this->css, $this->blocked) as $file => $params) { |
260
|
|
|
$ignore = in_array($file, self::$files_to_ignore) ? true : false; |
261
|
|
|
if($isDev || $ignore) { |
262
|
|
|
$path = Convert::raw2xml($this->path_for_file($file)); |
|
|
|
|
263
|
|
|
if ($path) { |
264
|
|
|
$media = (isset($params['media']) && !empty($params['media'])) ? $params['media'] : ""; |
265
|
|
|
if ($isDev) { |
266
|
|
|
$requirementsCSSFiles[$path."_".$media] = $path; |
267
|
|
|
} |
268
|
|
|
if($ignore) { |
269
|
|
|
if($media !== '') { |
270
|
|
|
$media = " media=\"{$media}\""; |
271
|
|
|
} |
272
|
|
|
$requirements .= "<link rel=\"stylesheet\" type=\"text/css\"{$media} href=\"$path\" />\n"; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
foreach (array_diff_key($this->customCSS, $this->blocked) as $css) { |
279
|
|
|
$requirements .= "<style type=\"text/css\">\n$css\n</style>\n"; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
foreach (array_diff_key($this->customHeadTags, $this->blocked) as $customHeadTag) { |
283
|
|
|
$requirements .= "$customHeadTag\n"; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
// Remove all newlines from code to preserve layout |
287
|
|
|
$jsRequirements = preg_replace('/>\n*/', '>', $jsRequirements); |
288
|
|
|
|
289
|
|
|
// Forcefully put the scripts at the bottom of the body instead of before the first |
290
|
|
|
// script tag. |
291
|
|
|
$content = preg_replace("/(<\/body[^>]*>)/i", $jsRequirements . "\\1", $content); |
292
|
|
|
|
293
|
|
|
// Put CSS at the bottom of the head |
294
|
|
|
$content = preg_replace("/(<\/head>)/i", $requirements . "\\1", $content); |
295
|
|
|
|
296
|
|
|
//end copy-ish from parent class |
297
|
|
|
//===================================================================== |
298
|
|
|
|
299
|
|
|
//copy files ... |
300
|
|
|
if ($this->canSaveRequirements()) { |
301
|
|
|
$themeFolderForSavingFiles = self::webpack_theme_folder_for_customisation(); |
302
|
|
|
//css |
303
|
|
|
$cssFolder = $themeFolderForSavingFiles.self::$copy_css_to_folder; |
304
|
|
|
|
305
|
|
|
foreach ($requirementsCSSFiles as $cssFile) { |
306
|
|
|
$this->moveFileToRequirementsFolder($cssFile, $cssFolder); |
307
|
|
|
} |
308
|
|
|
//js |
309
|
|
|
$jsFolder = $themeFolderForSavingFiles.self::$copy_js_to_folder; |
310
|
|
|
foreach ($requirementsJSFiles as $jsFile) { |
311
|
|
|
$this->moveFileToRequirementsFolder($jsFile, $jsFolder); |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
return $content; |
316
|
|
|
} else { |
317
|
|
|
return parent::includeInHTML($templateFile, $content); |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Attach requirements inclusion to X-Include-JS and X-Include-CSS headers on the given |
323
|
|
|
* HTTP Response |
324
|
|
|
* |
325
|
|
|
* @param SS_HTTPResponse $response |
326
|
|
|
*/ |
327
|
|
|
public function include_in_response(SS_HTTPResponse $response) |
328
|
|
|
{ |
329
|
|
|
if ($this->themedRequest()) { |
|
|
|
|
330
|
|
|
//do nothing |
331
|
|
|
} else { |
332
|
|
|
return parent::include_in_response($response); |
333
|
|
|
} |
334
|
|
|
//$this->process_combined_files(); |
|
|
|
|
335
|
|
|
//do nothing ... |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* |
340
|
|
|
* |
341
|
|
|
* |
342
|
|
|
* @return bool |
|
|
|
|
343
|
|
|
*/ |
344
|
|
|
protected function canSaveRequirements() |
345
|
|
|
{ |
346
|
|
|
if (Director::isDev()) { |
347
|
|
|
if ($this->themedRequest()) { |
348
|
|
|
$socket = @fsockopen('localhost', 3000, $errno, $errstr, 1); |
349
|
|
|
if ($socket) { |
350
|
|
|
return true; |
351
|
|
|
} |
352
|
|
|
} |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* |
358
|
|
|
* |
359
|
|
|
* @return bool |
360
|
|
|
*/ |
361
|
|
|
protected function themedRequest() |
362
|
|
|
{ |
363
|
|
|
return Config::inst()->get('SSViewer', 'theme') && Config::inst()->get('SSViewer', 'theme_enabled') ? true : false; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
protected function moveFileToRequirementsFolder($fileLocation, $folderLocation) |
|
|
|
|
367
|
|
|
{ |
368
|
|
|
$base = Director::baseFolder(); |
369
|
|
|
$folderLocationWithBase = $base . $folderLocation; |
370
|
|
|
Filesystem::makeFolder($folderLocationWithBase); |
371
|
|
|
if (!file_exists($folderLocationWithBase)) { |
372
|
|
|
user_error('Please update Requirements_Backend_For_Webpack for the right folder or create '.$folderLocationWithBase); |
373
|
|
|
} |
374
|
|
|
if (strpos($fileLocation, "//") !== false) { |
375
|
|
|
$logFile = $folderLocationWithBase."/TO.INCLUDE.FROM.PAGE.SS.FILE.log"; |
376
|
|
|
$line = $_SERVER['REQUEST_URI']." | ".$fileLocation; |
|
|
|
|
377
|
|
|
$this->addLinesToFile($logFile, $fileLocation); |
378
|
|
|
} else { |
379
|
|
|
$from = $fileLocation; |
380
|
|
|
$to = basename($fileLocation); |
|
|
|
|
381
|
|
|
$line = '@import \''.$from.'\''; |
382
|
|
|
$logFile = $folderLocationWithBase."/TO.INCLUDE.USING.WEBPACK.METHODS.log"; |
383
|
|
|
$this->addLinesToFile($logFile, $line); |
384
|
|
|
if (in_array($fileLocation, self::$files_to_ignore)) { |
|
|
|
|
385
|
|
|
//to be completed ... |
386
|
|
|
} else { |
|
|
|
|
387
|
|
|
// if (! file_exists($to) || self::$force_update) { |
|
|
|
|
388
|
|
|
// copy($from, $to); |
|
|
|
|
389
|
|
|
// } |
390
|
|
|
} |
391
|
|
|
} |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
protected function copyIfYouCan($from, $to, $count = 0) |
395
|
|
|
{ |
396
|
|
|
try { |
397
|
|
|
copy($from, $to); |
398
|
|
|
} |
399
|
|
|
catch(Exception $e) { |
400
|
|
|
$count++; |
401
|
|
|
$this->makeFolderWritable(); |
|
|
|
|
402
|
|
|
if($count < 3) { |
403
|
|
|
$this->copyIfYouCan($from, $to, $count); |
404
|
|
|
} |
405
|
|
|
} |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
protected function addLinesToFile($fileLocation, $line, $count = 0) |
409
|
|
|
{ |
410
|
|
|
$line .= "\n"; |
411
|
|
|
try { |
412
|
|
|
$lines = []; |
413
|
|
|
if (file_exists($fileLocation)) { |
414
|
|
|
$lines = file($fileLocation); |
415
|
|
|
} |
416
|
|
|
if (! in_array($line, $lines)) { |
417
|
|
|
$handle = fopen($fileLocation, 'a'); |
418
|
|
|
fwrite($handle, $line); |
419
|
|
|
} |
420
|
|
|
} |
421
|
|
|
catch(Exception $e) { |
422
|
|
|
$this->makeFolderWritable(); |
|
|
|
|
423
|
|
|
$count++; |
424
|
|
|
if($count < 3) { |
425
|
|
|
$this->addLinesToFile($fileLocation, $lines, $count); |
426
|
|
|
} |
427
|
|
|
} |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
protected function makeFolderWritable($fileLocation) |
431
|
|
|
{ |
432
|
|
|
if(file_exists($fileLocation)) { |
433
|
|
|
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(dirname($fileLocation))); |
434
|
|
|
foreach($iterator as $item) { |
435
|
|
|
chmod($item, '0664'); |
436
|
|
|
} |
437
|
|
|
} |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
|
441
|
|
|
public static function flush() |
|
|
|
|
442
|
|
|
{ |
443
|
|
|
$theme = self::webpack_current_theme_as_set_in_db(); |
444
|
|
|
if($theme) { |
445
|
|
|
//make raw requirements writeable |
446
|
|
|
$base = Director::baseFolder(); |
447
|
|
|
$themeFolderForCustomisation = self::webpack_theme_folder_for_customisation(); |
448
|
|
|
$rawFolders = [ |
449
|
|
|
$base.'/'.$themeFolderForCustomisation.'/src/sass', |
450
|
|
|
$base.'/'.$themeFolderForCustomisation.'/'.self::$copy_css_to_folder, |
451
|
|
|
$base.'/'.$themeFolderForCustomisation.'/'.self::$copy_js_to_folder |
452
|
|
|
]; |
453
|
|
|
foreach($rawFolders as $folder) { |
454
|
|
|
Filesystem::makeFolder($folder); |
455
|
|
|
} |
456
|
|
|
$files = [ |
457
|
|
|
$base.$themeFolderForCustomisation.'src/main.js', |
458
|
|
|
$base.$themeFolderForCustomisation.'src/sass/style.sass' |
459
|
|
|
]; |
460
|
|
|
foreach($files as $file) { |
461
|
|
|
if(!file_exists($file)){ |
462
|
|
|
file_put_contents($file, '//add your customisations in this file'); |
463
|
|
|
} |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
$varArray = [ |
467
|
|
|
'themeName' => self::webpack_current_theme_as_set_in_db(), |
468
|
|
|
'devWebAddress' => $_SERVER['HTTP_HOST'], |
469
|
|
|
'distributionFolder' => self::webpack_current_theme_as_set_in_db().'_'.Config::inst()->get('WebpackPageControllerExtension', 'webpack_distribution_folder_extension') |
470
|
|
|
]; |
471
|
|
|
$str = 'module.exports = '.json_encode($varArray).''; |
472
|
|
|
file_put_contents($base.'/'.self::$webpack_variables_file_location, $str); |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
|
478
|
|
|
} |
479
|
|
|
|
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.