@@ -60,6 +60,8 @@ class JavascriptRenderer
60
60
61
61
protected $ enableJqueryNoConflict = true ;
62
62
63
+ protected $ useRequireJs = false ;
64
+
63
65
protected $ initialization ;
64
66
65
67
protected $ controls = array ();
@@ -143,6 +145,9 @@ public function setOptions(array $options)
143
145
if (array_key_exists ('enable_jquery_noconflict ' , $ options )) {
144
146
$ this ->setEnableJqueryNoConflict ($ options ['enable_jquery_noconflict ' ]);
145
147
}
148
+ if (array_key_exists ('use_requirejs ' , $ options )) {
149
+ $ this ->setUseRequireJs ($ options ['use_requirejs ' ]);
150
+ }
146
151
if (array_key_exists ('controls ' , $ options )) {
147
152
foreach ($ options ['controls ' ] as $ name => $ control ) {
148
153
$ this ->addControl ($ name , $ control );
@@ -352,6 +357,28 @@ public function isJqueryNoConflictEnabled()
352
357
return $ this ->enableJqueryNoConflict ;
353
358
}
354
359
360
+ /**
361
+ * Sets whether to use RequireJS or not
362
+ *
363
+ * @param boolean $enabled
364
+ * @return $this
365
+ */
366
+ public function setUseRequireJs ($ enabled = true )
367
+ {
368
+ $ this ->useRequireJs = $ enabled ;
369
+ return $ this ;
370
+ }
371
+
372
+ /**
373
+ * Checks if RequireJS is used
374
+ *
375
+ * @return boolean
376
+ */
377
+ public function isRequireJsUsed ()
378
+ {
379
+ return $ this ->useRequireJs ;
380
+ }
381
+
355
382
/**
356
383
* Adds a control to initialize
357
384
*
@@ -710,21 +737,25 @@ public function dumpCssAssets($targetFilename = null)
710
737
*/
711
738
public function dumpJsAssets ($ targetFilename = null )
712
739
{
713
- $ this ->dumpAssets ($ this ->getAssets ('js ' ), $ targetFilename );
740
+ $ this ->dumpAssets ($ this ->getAssets ('js ' ), $ targetFilename, $ this -> useRequireJs );
714
741
}
715
742
716
743
/**
717
744
* Write assets to standard output or in a file
718
745
*
719
746
* @param array $files
720
747
* @param string $targetFilename
748
+ * @param bool $useRequireJs
721
749
*/
722
- protected function dumpAssets ($ files , $ targetFilename = null )
750
+ protected function dumpAssets ($ files , $ targetFilename = null , $ useRequireJs = false )
723
751
{
724
752
$ content = '' ;
725
753
foreach ($ files as $ file ) {
726
754
$ content .= file_get_contents ($ file ) . "\n" ;
727
755
}
756
+ if ($ useRequireJs ) {
757
+ $ content = "define('debugbar', ['jquery'], function($){ \r\n" . $ content . "\r\n return PhpDebugBar; \r\n}); " ;
758
+ }
728
759
if ($ targetFilename !== null ) {
729
760
file_put_contents ($ targetFilename , $ content );
730
761
} else {
@@ -752,7 +783,7 @@ public function renderHead()
752
783
$ html .= sprintf ('<script type="text/javascript" src="%s"></script> ' . "\n" , $ file );
753
784
}
754
785
755
- if ($ this ->enableJqueryNoConflict ) {
786
+ if ($ this ->enableJqueryNoConflict && ! $ this -> useRequireJs ) {
756
787
$ html .= '<script type="text/javascript">jQuery.noConflict(true);</script> ' . "\n" ;
757
788
}
758
789
@@ -820,7 +851,7 @@ public function replaceTagInBuffer($here = true, $initialize = true, $renderStac
820
851
* AJAX request should not render the initialization code.
821
852
*
822
853
* @param boolean $initialize Whether or not to render the debug bar initialization code
823
- * @param bool $renderStackedData Whether or not to render the stacked data
854
+ * @param boolean $renderStackedData Whether or not to render the stacked data
824
855
* @return string
825
856
*/
826
857
public function render ($ initialize = true , $ renderStackedData = true )
@@ -840,7 +871,12 @@ public function render($initialize = true, $renderStackedData = true)
840
871
$ suffix = !$ initialize ? '(ajax) ' : null ;
841
872
$ js .= $ this ->getAddDatasetCode ($ this ->debugBar ->getCurrentRequestId (), $ this ->debugBar ->getData (), $ suffix );
842
873
843
- return "<script type= \"text/javascript \"> \n$ js \n</script> \n" ;
874
+ if ($ this ->useRequireJs ){
875
+ return "<script type= \"text/javascript \"> \nrequire(['debugbar'], function(PhpDebugBar){ $ js }); \n</script> \n" ;
876
+ } else {
877
+ return "<script type= \"text/javascript \"> \n$ js \n</script> \n" ;
878
+ }
879
+
844
880
}
845
881
846
882
/**
@@ -950,7 +986,7 @@ protected function getJsControlsDefinitionCode($varname)
950
986
*
951
987
* @param string $requestId
952
988
* @param array $data
953
- * @param null $suffix
989
+ * @param mixed $suffix
954
990
* @return string
955
991
*/
956
992
protected function getAddDatasetCode ($ requestId , $ data , $ suffix = null )
0 commit comments