-
Notifications
You must be signed in to change notification settings - Fork 401
/
JavascriptRenderer.php
1267 lines (1117 loc) · 37.3 KB
/
JavascriptRenderer.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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* This file is part of the DebugBar package.
*
* (c) 2013 Maxime Bouroumeau-Fuseau
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace DebugBar;
use DebugBar\DataCollector\AssetProvider;
use DebugBar\DataCollector\Renderable;
/**
* Renders the debug bar using the client side javascript implementation
*
* Generates all the needed initialization code of controls
*/
class JavascriptRenderer
{
const INITIALIZE_CONSTRUCTOR = 2;
const INITIALIZE_CONTROLS = 4;
const REPLACEABLE_TAG = "{--DEBUGBAR_OB_START_REPLACE_ME--}";
const RELATIVE_PATH = 'path';
const RELATIVE_URL = 'url';
protected $debugBar;
protected $baseUrl;
protected $basePath;
protected $cssVendors = array(
'fontawesome' => 'vendor/font-awesome/css/font-awesome.min.css',
'highlightjs' => 'vendor/highlightjs/styles/github.css'
);
protected $jsVendors = array(
'jquery' => 'vendor/jquery/dist/jquery.min.js',
'highlightjs' => 'vendor/highlightjs/highlight.pack.js'
);
protected $includeVendors = true;
protected $cssFiles = array('debugbar.css', 'widgets.css', 'openhandler.css');
protected $jsFiles = array('debugbar.js', 'widgets.js', 'openhandler.js');
protected $additionalAssets = array();
protected $javascriptClass = 'PhpDebugBar.DebugBar';
protected $variableName = 'phpdebugbar';
protected $enableJqueryNoConflict = true;
protected $useRequireJs = false;
protected $hideEmptyTabs = null;
protected $initialization;
protected $controls = array();
protected $ignoredCollectors = array();
protected $ajaxHandlerClass = 'PhpDebugBar.AjaxHandler';
protected $ajaxHandlerBindToFetch = false;
protected $ajaxHandlerBindToJquery = false;
protected $ajaxHandlerBindToXHR = true;
protected $ajaxHandlerAutoShow = true;
protected $ajaxHandlerEnableTab = false;
protected $openHandlerClass = 'PhpDebugBar.OpenHandler';
protected $openHandlerUrl;
protected $cspNonce;
/**
* @param \DebugBar\DebugBar $debugBar
* @param string $baseUrl
* @param string $basePath
*/
public function __construct(DebugBar $debugBar, $baseUrl = null, $basePath = null)
{
$this->debugBar = $debugBar;
if ($baseUrl === null) {
$baseUrl = '/vendor/maximebf/debugbar/src/DebugBar/Resources';
}
$this->baseUrl = $baseUrl;
if ($basePath === null) {
$basePath = __DIR__ . DIRECTORY_SEPARATOR . 'Resources';
}
$this->basePath = $basePath;
// bitwise operations cannot be done in class definition :(
$this->initialization = self::INITIALIZE_CONSTRUCTOR | self::INITIALIZE_CONTROLS;
}
/**
* Sets options from an array
*
* Options:
* - base_path
* - base_url
* - include_vendors
* - javascript_class
* - variable_name
* - initialization
* - enable_jquery_noconflict
* - controls
* - disable_controls
* - ignore_collectors
* - ajax_handler_classname
* - ajax_handler_bind_to_jquery
* - ajax_handler_auto_show
* - open_handler_classname
* - open_handler_url
*
* @param array $options [description]
*/
public function setOptions(array $options)
{
if (array_key_exists('base_path', $options)) {
$this->setBasePath($options['base_path']);
}
if (array_key_exists('base_url', $options)) {
$this->setBaseUrl($options['base_url']);
}
if (array_key_exists('include_vendors', $options)) {
$this->setIncludeVendors($options['include_vendors']);
}
if (array_key_exists('javascript_class', $options)) {
$this->setJavascriptClass($options['javascript_class']);
}
if (array_key_exists('variable_name', $options)) {
$this->setVariableName($options['variable_name']);
}
if (array_key_exists('initialization', $options)) {
$this->setInitialization($options['initialization']);
}
if (array_key_exists('enable_jquery_noconflict', $options)) {
$this->setEnableJqueryNoConflict($options['enable_jquery_noconflict']);
}
if (array_key_exists('use_requirejs', $options)) {
$this->setUseRequireJs($options['use_requirejs']);
}
if (array_key_exists('hide_empty_tabs', $options)) {
$this->setHideEmptyTabs($options['hide_empty_tabs']);
}
if (array_key_exists('controls', $options)) {
foreach ($options['controls'] as $name => $control) {
$this->addControl($name, $control);
}
}
if (array_key_exists('disable_controls', $options)) {
foreach ((array) $options['disable_controls'] as $name) {
$this->disableControl($name);
}
}
if (array_key_exists('ignore_collectors', $options)) {
foreach ((array) $options['ignore_collectors'] as $name) {
$this->ignoreCollector($name);
}
}
if (array_key_exists('ajax_handler_classname', $options)) {
$this->setAjaxHandlerClass($options['ajax_handler_classname']);
}
if (array_key_exists('ajax_handler_bind_to_jquery', $options)) {
$this->setBindAjaxHandlerToJquery($options['ajax_handler_bind_to_jquery']);
}
if (array_key_exists('ajax_handler_auto_show', $options)) {
$this->setAjaxHandlerAutoShow($options['ajax_handler_auto_show']);
}
if (array_key_exists('ajax_handler_enable_tab', $options)) {
$this->setAjaxHandlerEnableTab($options['ajax_handler_enable_tab']);
}
if (array_key_exists('open_handler_classname', $options)) {
$this->setOpenHandlerClass($options['open_handler_classname']);
}
if (array_key_exists('open_handler_url', $options)) {
$this->setOpenHandlerUrl($options['open_handler_url']);
}
if (array_key_exists('csp_nonce', $options)) {
$this->setCspNonce($options['csp_nonce']);
}
}
/**
* Sets the path which assets are relative to
*
* @param string $path
*/
public function setBasePath($path)
{
$this->basePath = $path;
return $this;
}
/**
* Returns the path which assets are relative to
*
* @return string
*/
public function getBasePath()
{
return $this->basePath;
}
/**
* Sets the base URL from which assets will be served
*
* @param string $url
*/
public function setBaseUrl($url)
{
$this->baseUrl = $url;
return $this;
}
/**
* Returns the base URL from which assets will be served
*
* @return string
*/
public function getBaseUrl()
{
return $this->baseUrl;
}
/**
* Whether to include vendor assets
*
* You can only include js or css vendors using
* setIncludeVendors('css') or setIncludeVendors('js')
*
* @param boolean $enabled
*/
public function setIncludeVendors($enabled = true)
{
if (is_string($enabled)) {
$enabled = array($enabled);
}
$this->includeVendors = $enabled;
if (!$enabled || (is_array($enabled) && !in_array('js', $enabled))) {
// no need to call jQuery.noConflict() if we do not include our own version
$this->enableJqueryNoConflict = false;
}
return $this;
}
/**
* Checks if vendors assets are included
*
* @return boolean
*/
public function areVendorsIncluded()
{
return $this->includeVendors !== false;
}
/**
* Disable a specific vendor's assets.
*
* @param string $name "jquery", "fontawesome", "highlightjs"
*
* @return void
*/
public function disableVendor($name)
{
if (array_key_exists($name, $this->cssVendors)) {
unset($this->cssVendors[$name]);
}
if (array_key_exists($name, $this->jsVendors)) {
unset($this->jsVendors[$name]);
}
}
/**
* Sets the javascript class name
*
* @param string $className
*/
public function setJavascriptClass($className)
{
$this->javascriptClass = $className;
return $this;
}
/**
* Returns the javascript class name
*
* @return string
*/
public function getJavascriptClass()
{
return $this->javascriptClass;
}
/**
* Sets the variable name of the class instance
*
* @param string $name
*/
public function setVariableName($name)
{
$this->variableName = $name;
return $this;
}
/**
* Returns the variable name of the class instance
*
* @return string
*/
public function getVariableName()
{
return $this->variableName;
}
/**
* Sets what should be initialized
*
* - INITIALIZE_CONSTRUCTOR: only initializes the instance
* - INITIALIZE_CONTROLS: initializes the controls and data mapping
* - INITIALIZE_CONSTRUCTOR | INITIALIZE_CONTROLS: initialize everything (default)
*
* @param integer $init
*/
public function setInitialization($init)
{
$this->initialization = $init;
return $this;
}
/**
* Returns what should be initialized
*
* @return integer
*/
public function getInitialization()
{
return $this->initialization;
}
/**
* Sets whether to call jQuery.noConflict()
*
* @param boolean $enabled
*/
public function setEnableJqueryNoConflict($enabled = true)
{
$this->enableJqueryNoConflict = $enabled;
return $this;
}
/**
* Checks if jQuery.noConflict() will be called
*
* @return boolean
*/
public function isJqueryNoConflictEnabled()
{
return $this->enableJqueryNoConflict;
}
/**
* Sets whether to use RequireJS or not
*
* @param boolean $enabled
* @return $this
*/
public function setUseRequireJs($enabled = true)
{
$this->useRequireJs = $enabled;
return $this;
}
/**
* Checks if RequireJS is used
*
* @return boolean
*/
public function isRequireJsUsed()
{
return $this->useRequireJs;
}
/**
* Sets whether to hide empty tabs or not
*
* @param boolean $hide
* @return $this
*/
public function setHideEmptyTabs($hide = true)
{
$this->hideEmptyTabs = $hide;
return $this;
}
/**
* Checks if empty tabs are hidden or not
*
* @return boolean
*/
public function areEmptyTabsHidden()
{
return $this->hideEmptyTabs;
}
/**
* Adds a control to initialize
*
* Possible options:
* - icon: icon name
* - tooltip: string
* - widget: widget class name
* - title: tab title
* - map: a property name from the data to map the control to
* - default: a js string, default value of the data map
*
* "icon" or "widget" are at least needed
*
* @param string $name
* @param array $options
*/
public function addControl($name, array $options)
{
if (count(array_intersect(array_keys($options), array('icon', 'widget', 'tab', 'indicator'))) === 0) {
throw new DebugBarException("Not enough options for control '$name'");
}
$this->controls[$name] = $options;
return $this;
}
/**
* Disables a control
*
* @param string $name
*/
public function disableControl($name)
{
$this->controls[$name] = null;
return $this;
}
/**
* Returns the list of controls
*
* This does not include controls provided by collectors
*
* @return array
*/
public function getControls()
{
return $this->controls;
}
/**
* Ignores widgets provided by a collector
*
* @param string $name
*/
public function ignoreCollector($name)
{
$this->ignoredCollectors[] = $name;
return $this;
}
/**
* Returns the list of ignored collectors
*
* @return array
*/
public function getIgnoredCollectors()
{
return $this->ignoredCollectors;
}
/**
* Sets the class name of the ajax handler
*
* Set to false to disable
*
* @param string $className
*/
public function setAjaxHandlerClass($className)
{
$this->ajaxHandlerClass = $className;
return $this;
}
/**
* Returns the class name of the ajax handler
*
* @return string
*/
public function getAjaxHandlerClass()
{
return $this->ajaxHandlerClass;
}
/**
* Sets whether to call bindToFetch() on the ajax handler
*
* @param boolean $bind
*/
public function setBindAjaxHandlerToFetch($bind = true)
{
$this->ajaxHandlerBindToFetch = $bind;
return $this;
}
/**
* Checks whether bindToFetch() will be called on the ajax handler
*
* @return boolean
*/
public function isAjaxHandlerBoundToFetch()
{
return $this->ajaxHandlerBindToFetch;
}
/**
* Sets whether to call bindToJquery() on the ajax handler
*
* @param boolean $bind
* @deprecated use setBindAjaxHandlerToXHR
*/
public function setBindAjaxHandlerToJquery($bind = true)
{
$this->ajaxHandlerBindToJquery = $bind;
return $this;
}
/**
* Checks whether bindToJquery() will be called on the ajax handler
*
* @return boolean
* @deprecated use isAjaxHandlerBoundToXHR
*/
public function isAjaxHandlerBoundToJquery()
{
return $this->ajaxHandlerBindToJquery;
}
/**
* Sets whether to call bindToXHR() on the ajax handler
*
* @param boolean $bind
*/
public function setBindAjaxHandlerToXHR($bind = true)
{
$this->ajaxHandlerBindToXHR = $bind;
return $this;
}
/**
* Checks whether bindToXHR() will be called on the ajax handler
*
* @return boolean
*/
public function isAjaxHandlerBoundToXHR()
{
return $this->ajaxHandlerBindToXHR;
}
/**
* Sets whether new ajax debug data will be immediately shown. Setting to false could be useful
* if there are a lot of tracking events cluttering things.
*
* @param boolean $autoShow
*/
public function setAjaxHandlerAutoShow($autoShow = true)
{
$this->ajaxHandlerAutoShow = $autoShow;
return $this;
}
/**
* Checks whether the ajax handler will immediately show new ajax requests.
*
* @return boolean
*/
public function isAjaxHandlerAutoShow()
{
return $this->ajaxHandlerAutoShow;
}
/**
* Sets whether new ajax debug data will be shown in a separate tab instead of dropdown.
*
* @param boolean $enabled
*/
public function setAjaxHandlerEnableTab($enabled = true)
{
$this->ajaxHandlerEnableTab = $enabled;
return $this;
}
/**
* Check if the Ajax Handler History tab is enabled
*
* @return boolean
*/
public function isAjaxHandlerTabEnabled()
{
return $this->ajaxHandlerEnableTab;
}
/**
* Sets the class name of the js open handler
*
* @param string $className
*/
public function setOpenHandlerClass($className)
{
$this->openHandlerClass = $className;
return $this;
}
/**
* Returns the class name of the js open handler
*
* @return string
*/
public function getOpenHandlerClass()
{
return $this->openHandlerClass;
}
/**
* Sets the url of the open handler
*
* @param string $url
*/
public function setOpenHandlerUrl($url)
{
$this->openHandlerUrl = $url;
return $this;
}
/**
* Returns the url for the open handler
*
* @return string
*/
public function getOpenHandlerUrl()
{
return $this->openHandlerUrl;
}
/**
* Sets the CSP Nonce (or remove it by setting to null)
*
* @param string|null $nonce
* @return $this
*/
public function setCspNonce($nonce = null)
{
$this->cspNonce = $nonce;
return $this;
}
/**
* Get the CSP Nonce
*
* @return string|null
*/
public function getCspNonce()
{
return $this->cspNonce;
}
/**
* Add assets stored in files to render in the head
*
* @param array $cssFiles An array of filenames
* @param array $jsFiles An array of filenames
* @param string $basePath Base path of those files
* @param string $baseUrl Base url of those files
* @return $this
*/
public function addAssets($cssFiles, $jsFiles, $basePath = null, $baseUrl = null)
{
$this->additionalAssets[] = array(
'base_path' => $basePath,
'base_url' => $baseUrl,
'css' => (array) $cssFiles,
'js' => (array) $jsFiles
);
return $this;
}
/**
* Add inline assets to render inline in the head. Ideally, you should store static assets in
* files that you add with the addAssets function. However, adding inline assets is useful when
* integrating with 3rd-party libraries that require static assets that are only available in an
* inline format.
*
* The inline content arrays require special string array keys: they are used to deduplicate
* content. This is particularly useful if multiple instances of the same asset end up being
* added. Inline assets from all collectors are merged together into the same array, so these
* content IDs effectively deduplicate the inline assets.
*
* @param array $inlineCss An array map of content ID to inline CSS content (not including <style> tag)
* @param array $inlineJs An array map of content ID to inline JS content (not including <script> tag)
* @param array $inlineHead An array map of content ID to arbitrary inline HTML content (typically
* <style>/<script> tags); it must be embedded within the <head> element
* @return $this
*/
public function addInlineAssets($inlineCss, $inlineJs, $inlineHead)
{
$this->additionalAssets[] = array(
'inline_css' => (array) $inlineCss,
'inline_js' => (array) $inlineJs,
'inline_head' => (array) $inlineHead
);
return $this;
}
/**
* Returns the list of asset files
*
* @param string $type 'css', 'js', 'inline_css', 'inline_js', 'inline_head', or null for all
* @param string $relativeTo The type of path to which filenames must be relative (path, url or null)
* @return array
*/
public function getAssets($type = null, $relativeTo = self::RELATIVE_PATH)
{
$cssFiles = $this->cssFiles;
$jsFiles = $this->jsFiles;
$inlineCss = array();
$inlineJs = array();
$inlineHead = array();
if ($this->includeVendors !== false) {
if ($this->includeVendors === true || in_array('css', $this->includeVendors)) {
$cssFiles = array_merge($this->cssVendors, $cssFiles);
}
if ($this->includeVendors === true || in_array('js', $this->includeVendors)) {
$jsFiles = array_merge($this->jsVendors, $jsFiles);
}
}
if ($relativeTo) {
$root = $this->getRelativeRoot($relativeTo, $this->basePath, $this->baseUrl);
$cssFiles = $this->makeUriRelativeTo($cssFiles, $root);
$jsFiles = $this->makeUriRelativeTo($jsFiles, $root);
}
$additionalAssets = $this->additionalAssets;
// finds assets provided by collectors
foreach ($this->debugBar->getCollectors() as $collector) {
if (($collector instanceof AssetProvider) && !in_array($collector->getName(), $this->ignoredCollectors)) {
$additionalAssets[] = $collector->getAssets();
}
}
foreach ($additionalAssets as $assets) {
$basePath = isset($assets['base_path']) ? $assets['base_path'] : '';
$baseUrl = isset($assets['base_url']) ? $assets['base_url'] : '';
$root = $this->getRelativeRoot($relativeTo,
$this->makeUriRelativeTo($basePath, $this->basePath),
$this->makeUriRelativeTo($baseUrl, $this->baseUrl));
if (isset($assets['css'])) {
$cssFiles = array_merge($cssFiles, $this->makeUriRelativeTo((array) $assets['css'], $root));
}
if (isset($assets['js'])) {
$jsFiles = array_merge($jsFiles, $this->makeUriRelativeTo((array) $assets['js'], $root));
}
if (isset($assets['inline_css'])) {
$inlineCss = array_merge($inlineCss, (array) $assets['inline_css']);
}
if (isset($assets['inline_js'])) {
$inlineJs = array_merge($inlineJs, (array) $assets['inline_js']);
}
if (isset($assets['inline_head'])) {
$inlineHead = array_merge($inlineHead, (array) $assets['inline_head']);
}
}
// Deduplicate files
$cssFiles = array_unique($cssFiles);
$jsFiles = array_unique($jsFiles);
return $this->filterAssetArray(array($cssFiles, $jsFiles, $inlineCss, $inlineJs, $inlineHead), $type ?? '');
}
/**
* Returns the correct base according to the type
*
* @param string $relativeTo
* @param string $basePath
* @param string $baseUrl
* @return string
*/
protected function getRelativeRoot($relativeTo, $basePath, $baseUrl)
{
if ($relativeTo === self::RELATIVE_PATH) {
return $basePath;
}
if ($relativeTo === self::RELATIVE_URL) {
return $baseUrl;
}
return null;
}
/**
* Makes a URI relative to another
*
* @param string|array $uri
* @param string $root
* @return string
*/
protected function makeUriRelativeTo($uri, $root)
{
if (!$root) {
return $uri;
}
if (is_array($uri)) {
$uris = array();
foreach ($uri as $u) {
$uris[] = $this->makeUriRelativeTo($u, $root);
}
return $uris;
}
$uri = $uri ?? '';
if (substr($uri, 0, 1) === '/' || preg_match('/^([a-zA-Z]+:\/\/|[a-zA-Z]:\/|[a-zA-Z]:\\\)/', $uri)) {
return $uri;
}
return rtrim($root, '/') . "/$uri";
}
/**
* Filters a tuple of (css, js, inline_css, inline_js, inline_head) assets according to $type
*
* @param array $array
* @param string $type 'css', 'js', 'inline_css', 'inline_js', 'inline_head', or null for all
* @return array
*/
protected function filterAssetArray($array, $type = '')
{
$types = array('css', 'js', 'inline_css', 'inline_js', 'inline_head');
$typeIndex = array_search(strtolower($type ?? ''), $types);
return $typeIndex !== false ? $array[$typeIndex] : $array;
}
/**
* Returns an array where all items are Assetic AssetCollection:
* - The first one contains the CSS files
* - The second one contains the JS files
* - The third one contains arbitrary inline HTML (typically composed of <script>/<style>
* elements); it must be embedded within the <head> element
*
* @param string $type Optionally return only 'css', 'js', or 'inline_head' collection
* @return array|\Assetic\Asset\AssetCollection
*/
public function getAsseticCollection($type = null)
{
$types = array('css', 'js', 'inline_head');
$typeIndex = array_search(strtolower($type), $types);
list($cssFiles, $jsFiles, $inlineCss, $inlineJs, $inlineHead) = $this->getAssets();
$collections = array(
$this->createAsseticCollection($cssFiles, $inlineCss),
$this->createAsseticCollection($jsFiles, $inlineJs),
$this->createAsseticCollection(null, $inlineHead)
);
return $typeIndex !== false ? $collections[$typeIndex] : $collections;
}
/**
* Create an Assetic AssetCollection with the given content.
* Filenames will be converted to absolute path using
* the base path.
*
* @param array|null $files Array of asset filenames.
* @param array|null $content Array of inline asset content.
* @return \Assetic\Asset\AssetCollection
*/
protected function createAsseticCollection($files = null, $content = null)
{
$assets = array();
if ($files) {
foreach ($files as $file) {
$assets[] = new \Assetic\Asset\FileAsset($file);
}
}
if ($content) {
foreach ($content as $item) {
$assets[] = new \Assetic\Asset\StringAsset($item);
}
}
return new \Assetic\Asset\AssetCollection($assets);
}
/**
* Write all CSS assets to standard output or in a file
*
* @param string $targetFilename
*/
public function dumpCssAssets($targetFilename = null)
{
$this->dumpAssets($this->getAssets('css'), $this->getAssets('inline_css'), $targetFilename);
}
/**
* Write all JS assets to standard output or in a file
*
* @param string $targetFilename
*/
public function dumpJsAssets($targetFilename = null)
{
$this->dumpAssets($this->getAssets('js'), $this->getAssets('inline_js'), $targetFilename, $this->useRequireJs);
}
/**
* Write all inline HTML header assets to standard output or in a file (only returns assets not
* already returned by dumpCssAssets or dumpJsAssets)
*
* @param string $targetFilename
*/
public function dumpHeadAssets($targetFilename = null)
{
$this->dumpAssets(null, $this->getAssets('inline_head'), $targetFilename);
}
/**
* Write assets to standard output or in a file
*
* @param array|null $files Filenames containing assets
* @param array|null $content Inline content to dump
* @param string $targetFilename
* @param bool $useRequireJs
*/
protected function dumpAssets($files = null, $content = null, $targetFilename = null, $useRequireJs = false)
{
$dumpedContent = '';
if ($files) {
foreach ($files as $file) {
$dumpedContent .= file_get_contents($file) . "\n";
}
}
if ($content) {
foreach ($content as $item) {
$dumpedContent .= $item . "\n";
}
}
if ($useRequireJs) {
$dumpedContent = "define('debugbar', ['jquery'], function($){\r\n" . $dumpedContent . "\r\n return PhpDebugBar; \r\n});";
}
if ($targetFilename !== null) {
file_put_contents($targetFilename, $dumpedContent);
} else {
echo $dumpedContent;
}
}
/**
* Renders the html to include needed assets
*
* Only useful if Assetic is not used
*
* @return string
*/
public function renderHead()
{
list($cssFiles, $jsFiles, $inlineCss, $inlineJs, $inlineHead) = $this->getAssets(null, self::RELATIVE_URL);
$html = '';
$nonce = $this->getNonceAttribute();
foreach ($cssFiles as $file) {
$html .= sprintf('<link rel="stylesheet" type="text/css" href="%s">' . "\n", $file);
}
foreach ($inlineCss as $content) {