-
Notifications
You must be signed in to change notification settings - Fork 3
/
LoaderHelper.php
598 lines (506 loc) · 19.6 KB
/
LoaderHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
<?php
namespace plugins\riCjLoader;
/**
* Required functions for the CSS/JS Loader
*
* @author yellow1912 (rubikin.com)
* @author John William Robeson, Jr <[email protected]>
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License V2.0
*
* NOTES:
* All .php files can be manipulated by PHP when they're called, and are copied in-full to the browser page
*/
use Symfony\Component\Templating\Helper\Helper;
class LoaderHelper extends Helper
{
protected $loaders = array();
protected $files = array();
protected $processed_files = array();
protected $handlers = array();
protected $options = array(
'dirs' => array(),
'loaders' => '*',
'load_print' => true
);
protected $inline = array();
protected $location = 1;
protected $libs;
protected $loaded_libs = array();
/**
* the browser handler
*
* @var
*/
protected $browser;
/**
* @var
*/
protected $fileUtility;
/**
* @var
*/
protected $finder;
/**
* @var
*/
protected $filters;
public function __construct($settings, $browser, $finder)
{
$this->options = array_merge($this->options, $settings->get('plugins.ricjloader'));
$this->browser = $browser;
$this->finder = $finder;
}
/**
* @return mixed
*/
public function getFilters()
{
return $this->filters;
}
/**
* @param $id
* @param $handler
*/
public function setHandler($id, $handler)
{
if (isset($this->options['handlers']) && in_array($id, $this->options['handlers'])) {
$this->handlers[$id] = $handler;
}
}
/**
* @param $id
* @param $filter
*/
public function setFilter($id, $filter)
{
if (isset($this->options['filters'][$id])) {
$this->filters[$id] = array('filter' => $filter, 'options' => $this->options['filters'][$id]);
}
}
/**
* returns the name of this helper
*
* @return string
*/
public function getName()
{
return 'loader';
}
/**
* @param $options
*/
function set($options)
{
$this->options = array_merge($this->options, $options);
}
/**
* @param string $key
* @param bool $default
* @return array|bool
*/
public function getOption($key = '', $default = false)
{
if (!empty($key)) {
return isset($this->options[$key]) ? $this->options[$key] : $default;
} else {
return $this->options;
}
}
/**
* Load the file or set of files or libs
*
* @param array $file array(array('path' => 'path/to/file', 'type' => 'css'))
* @param string $location allows loading the file at header/footer or current location
*/
public function load($files, $location = '', $silent = false)
{
$files = (array)$files;
// rather costly operation here but we need to determine the location
if (empty($location)) {
$location = ++$this->location;
if (!$silent) {
echo '<!-- ricjloader: ' . $location . ' -->';
}
} // now we will have to echo out the string to be replaced here
elseif ($location !== 'header' && $location !== 'footer' && $location != $this->location) {
if (!$silent) {
echo '<!-- ricjloader: ' . $location . ' -->';
}
}
foreach ($files as $file => $options) {
if (!is_array($options)) {
$file = $options;
$options = array();
}
// only add this file if it has not been requested for the same position
if (!isset($this->files[$location]) || !in_array($file, $this->files[$location])) {
$options['ext'] = pathinfo($file, PATHINFO_EXTENSION);
if (isset($options['inline']) && !empty($options['inline'])) {
$file = md5($options['inline']) . '.' . $options['ext'];
}
$this->files[$location][$file] = $options;
}
}
return $location;
}
/**
* @param string $type
* @param string $location
*/
public function startInline($type = 'js', $location = '')
{
if ($location !== 'header' && $location !== 'footer') {
if (empty($location)) {
$location = $this->location;
}
}
$this->inline = array('type' => $type,
'location' => $location);
ob_start();
}
/**
*
*/
public function endInline()
{
$this->load(array('inline.' . $this->inline['type'] => array('inline' => ob_get_clean())), $this->inline['location']);
}
/**
* @param $type
* @return mixed
*/
private function getHandler($type)
{
return $this->handlers[$type];
}
/**
*
* Inject the assets into the content of the page
*
* @param string $content
*/
public function injectAssets($content)
{
// parse the loads
// sample: abc.css, template:current:file.php|type:js
preg_match_all("/(<!-- load:)(.*?)(-->)/", $content, $matches, PREG_SET_ORDER);
foreach ($matches as $val) {
$val[2] = str_replace(' ', '', $val[2]);
$temp = explode(',', $val[2]);
$load = array();
foreach ($temp as $v) {
// check if we have additional parameters
if(strpos($v, '|') !== false) {
$v = explode('|', $v);
$v[1] = explode(';', $v[1]);
foreach ($v[1] as $u) {
$u = explode(':', $u);
$load[$v[0]][$u[0]] = $u[1];
}
}
else{
$load[] = $v;
}
}
$location = $this->load($load, '', true);
$content = str_replace($val[0], '<!-- ricjloader: ' . $location . ' -->', $content);
}
// load the files
$ordered_files = array();
// scan the content to find out the real order of the loader
preg_match_all("/(<!-- ricjloader:)(.*?)(-->)/", $content, $matches, PREG_SET_ORDER);
$found_header = $found_footer = false;
foreach ($matches as $val) {
$val[2] = trim($val[2]);
if (!$found_header && $val[2] == 'header') {
$found_header = true;
} elseif (!$found_footer && $val[2] == 'footer') {
$found_footer = true;
}
if (isset($this->files[$val[2]])) {
$ordered_files[$val[2]] = $this->files[$val[2]];
}
}
if (!$found_header && isset($this->files['header'])) {
$ordered_files['header'] = $this->files['header'];
}
if (!$found_footer && isset($this->files['footer'])) {
$ordered_files['footer'] = $this->files['footer'];
}
$this->processFiles($ordered_files);
foreach ($this->processed_files as $type => $locations) {
foreach ($locations as $location => $files) {
$inject_content = $this->getHandler($type)->process($files, $this->getOption("cache"), $this->finder, $this->filters);
// inject
switch ($location) {
case 'header':
if (!$found_header) {
$content = str_replace('</head>', $inject_content . '</head>', $content);
} else {
$content = str_replace('<!-- ricjloader: header -->', $inject_content . '<!-- ricjloader: header -->', $content);
}
break;
case 'footer':
if (!$found_footer) {
$content = str_replace('</body>', $inject_content . '</body>', $content);
} else {
$content = str_replace('<!-- ricjloader: footer -->', $inject_content . '<!-- ricjloader: footer -->', $content);
}
break;
default:
$content = str_replace('<!-- ricjloader: ' . $location . ' -->', $inject_content . '<!-- ricjloader: ' . $location . ' -->', $content);
break;
}
}
}
return $content;
}
/**
* @return array
*/
public function getAssetsArray()
{
if ($this->getOption('load_global')) {
$this->loadGlobal();
}
if ($this->getOption('load_page')) {
$this->loadPage();
}
if ($this->getOption('load_loaders')) {
$this->loadLoaders();
}
$this->processFiles($this->files);
$result = array();
foreach ($this->processed_files as $type => $locations) {
foreach ($locations as $location => $files) {
// we may want to do some caching here
$result[$location][$type] = $this->getHandler($type)->processArray($files, $type, $this);
}
}
return $result;
}
/**
* @param $ordered_files
* @return array
*/
public function processFiles($ordered_files)
{
// now we loop thru the $ordered_files to make sure each file is loaded only once
$loaded_files = $to_load = array();
foreach ($ordered_files as $location => $files) {
$location_loaded_files = array();
foreach ($files as $file => $options) {
if (!array_key_exists($file, $loaded_files)) {
$loaded_files[$file] = $location;
$to_load[$location][$file] = $options;
$location_loaded_files[$file] = $options;
} // if we encounter this file in the loaded list, it means that we will have to take all the loaded
// files in this same location and put it IN FRONT OF this file location which is $loaded_files[$file]
elseif (!empty($location_loaded_files)) {
$to_load[$location] = array_diff($to_load[$location], $location_loaded_files);
array_KSplice2($to_load[$loaded_files[$file]], $file, 0, $location_loaded_files);
$location_loaded_files = array();
}
}
}
// now we will have to process the list of files to put them in their real type to process later
foreach ($to_load as $location => $files) {
foreach ($files as $file => $options) {
switch ($options['ext']) {
// lib? load the library
case 'lib':
// we need to try loading the config file
$lib = str_replace('.lib', '', $file);
if (!in_array($lib, $this->loaded_libs)) {
if (file_exists(__DIR__ . '/Resources/config/libs/' . $lib . '.php')) {
require (__DIR__ . '/Resources/config/libs/' . $lib . '.php');
$this->libs[$lib] = !isset($this->libs[$lib]) ? $libs[$lib] : array_merge($libs[$lib], $this->libs[$lib]);
}
$this->loaded_libs[] = $lib;
}
if (isset($this->libs[$lib])) {
$lib_versions = array_keys($this->libs[$lib]);
// if options are passed in
if (is_array($options)) {
if (isset($options['min']) && (($pos = array_search($options['min'], $lib_versions)) != 0)) {
$lib_versions = array_slice($lib_versions, $pos);
}
if (isset($options['max']) && (($pos = array_search($options['max'], $lib_versions)) < count($lib_versions) - 1)) {
array_splice($lib_versions, $pos + 1);
}
}
if (empty($lib_versions)) {
// houston we have a problem
// TODO: we need to somehow print out the error in this case
} else {
// we prefer the latest version
$lib_version = end($lib_versions);
// add the files
if (isset($this->libs[$lib][$lib_version]['css_files'])) {
$options['type'] = 'css';
foreach ($this->libs[$lib][$lib_version]['css_files'] as $css_file => $css_file_options) {
if ($this->getOption('cdn') && isset($css_file_options['cdn'])) {
$file = $this->request_type == 'NONSSL' ? $css_file_options['cdn']['http'] : $css_file_options['cdn']['https'];
$this->_load($this->processed_files, $file, $location, array('type' => 'css', 'external' => true));
} else {
if (strpos($css_file_options['local'], ":") !== false) {
$local = explode(":", $css_file_options['local']);
if (empty($local[2])) {
$local[2] = $css_file;
}
$file = $local[0] . ':' . $local[1] . ':' . $lib . '/' . $lib_version . '/' . $local[2];
} else {
$file = 'plugins:riCjLoader:libs/' . $lib . '/' . $lib_version . '/' . (!empty($css_file_options['local']) ? $css_file_options['local'] : $css_file);
}
$this->_load($this->processed_files, $file, $location, $options);
}
}
}
if (isset($this->libs[$lib][$lib_version]['jscript_files'])) {
$options['type'] = 'js';
foreach ($this->libs[$lib][$lib_version]['jscript_files'] as $jscript_file => $jscript_file_options) {
if ($this->getOption('cdn') && isset($jscript_file_options['cdn'])) {
$file = $this->request_type == 'NONSSL' ? $jscript_file_options['cdn']['http'] : $jscript_file_options['cdn']['https'];
$this->_load($this->processed_files, $file, $location, array('type' => 'js', 'external' => true));
} else {
if (strpos($jscript_file_options['local'], ":") !== false) {
$local = explode(":", $jscript_file_options['local']);
if (empty($local[2])) {
$local[2] = $jscript_file;
}
$file = $local[0] . ':' . $local[1] . ':' . $lib . '/' . $lib_version . '/' . $local[2];
} else {
$file = 'plugins:riCjLoader:libs/' . $lib . '/' . $lib_version . '/' . (!empty($jscript_file_options['local']) ? $jscript_file_options['local'] : $jscript_file);
}
$this->_load($this->processed_files, $file, $location, $options);
}
}
}
}
}
break;
default:
$this->_load($this->processed_files, $file, $location, $options);
break;
}
}
}
return $this->processed_files;
}
/**
*
*/
public function loadGlobal()
{
$this->loadFiles($this->finder->findGlobalFiles($this->getOption('load_print')));
}
/**
*
*/
public function loadPage()
{
$this->loadFiles($this->finder->findPageFiles());
}
/**
*
*/
public function loadLoaders()
{
$loaders = $this->finder->findLoaders($this->getOption('loaders'));
if (count($loaders) > 0) {
$this->addLoaders($loaders, true);
}
$this->loadFiles($this->finder->findLoadersFiles($this->loaders));
}
/**
* for backward compatibility
*
* @param $libs
*/
public function addLibs($libs)
{
foreach ($libs as $lib => $versions) {
foreach ($versions as $version => $options) {
if (!isset($this->libs[$lib]))
$this->libs[$lib][$version] = $options;
}
}
}
/**
* @param $loaders
* @param bool $multi
*/
public function addLoaders($loaders, $multi = false)
{
if ($multi) {
$this->loaders = array_merge($this->loaders, $loaders);
} else {
$this->loaders[] = $loaders;
}
}
/**
* we put the browser methods here because we want to users to be able to easily access
* it within templates
*/
/**
* @param $browser_name
* @return boolean
*/
public function isBrowser($browser_name)
{
return $this->browser->isBrowser($browser_name);
}
/**
* @return mixed
*/
public function getBrowserVersion()
{
return $this->browser->getVersion();
}
/**
* @param $files
* @param $file
* @param $location
* @param $options
*/
private function _load(&$files, $file, $location, $options)
{
if (!isset($options['type'])) {
$options['type'] = $options['ext'] == 'css' ? 'css' : 'js';
}
// for css, they MUST be loaded at header
if ($options['type'] == 'css' && is_integer($location)) {
$location = 'header';
}
$this->getHandler($options['type'])->load($files, $file, $location, $options);
}
/**
*
*/
function setCurrentPage()
{
if ($this->kernel->getContainer()->get("environment")->getSubEnvironment() == "frontend") {
// set current page
if ($this->this_is_home_page) {
$this->current_page = 'index_home';
} elseif ($this->current_page == 'index') {
if (isset($_GET['cPath'])) {
$this->current_page = 'index_category';
} elseif (isset($_GET['manufacturers_id'])) {
$this->current_page = 'index_manufacturer';
}
}
} else {
$this->current_page = preg_replace('/\.php/', '', substr(strrchr($_SERVER['PHP_SELF'], '/'), 1), 1);
}
}
/**
* @param $files
*/
private function loadFiles($files)
{
foreach ($files as $file) {
$this->load($file["files"], $file["location"]);
}
}
}