1
0
mirror of https://github.com/maximebf/php-debugbar.git synced 2025-03-15 11:50:01 +01:00

Add initial RequireJS support

See https://github.com/barryvdh/laravel-debugbar/pull/423
This commit is contained in:
Barry vd. Heuvel 2016-05-15 15:11:14 +02:00
parent e6109df274
commit c3268ca122

View File

@ -60,6 +60,8 @@ class JavascriptRenderer
protected $enableJqueryNoConflict = true;
protected $useRequireJs = false;
protected $initialization;
protected $controls = array();
@ -143,6 +145,9 @@ class JavascriptRenderer
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('controls', $options)) {
foreach ($options['controls'] as $name => $control) {
$this->addControl($name, $control);
@ -352,6 +357,28 @@ class JavascriptRenderer
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;
}
/**
* Adds a control to initialize
*
@ -710,7 +737,7 @@ class JavascriptRenderer
*/
public function dumpJsAssets($targetFilename = null)
{
$this->dumpAssets($this->getAssets('js'), $targetFilename);
$this->dumpAssets($this->getAssets('js'), $targetFilename, $this->useRequireJs);
}
/**
@ -718,13 +745,17 @@ class JavascriptRenderer
*
* @param array $files
* @param string $targetFilename
* @param bool $useRequireJs
*/
protected function dumpAssets($files, $targetFilename = null)
protected function dumpAssets($files, $targetFilename = null, $useRequireJs = false)
{
$content = '';
foreach ($files as $file) {
$content .= file_get_contents($file) . "\n";
}
if ($useRequireJs) {
$content = "define('debugbar', ['jquery'], function($){\r\n" . $content . "\r\n return PhpDebugBar; \r\n});";
}
if ($targetFilename !== null) {
file_put_contents($targetFilename, $content);
} else {
@ -752,7 +783,7 @@ class JavascriptRenderer
$html .= sprintf('<script type="text/javascript" src="%s"></script>' . "\n", $file);
}
if ($this->enableJqueryNoConflict) {
if ($this->enableJqueryNoConflict && !$this->useRequireJs) {
$html .= '<script type="text/javascript">jQuery.noConflict(true);</script>' . "\n";
}
@ -820,7 +851,7 @@ class JavascriptRenderer
* AJAX request should not render the initialization code.
*
* @param boolean $initialize Whether or not to render the debug bar initialization code
* @param bool $renderStackedData Whether or not to render the stacked data
* @param boolean $renderStackedData Whether or not to render the stacked data
* @return string
*/
public function render($initialize = true, $renderStackedData = true)
@ -840,7 +871,12 @@ class JavascriptRenderer
$suffix = !$initialize ? '(ajax)' : null;
$js .= $this->getAddDatasetCode($this->debugBar->getCurrentRequestId(), $this->debugBar->getData(), $suffix);
return "<script type=\"text/javascript\">\n$js\n</script>\n";
if ($this->useRequireJs){
return "<script type=\"text/javascript\">\nrequire(['debugbar'], function(PhpDebugBar){ $js });\n</script>\n";
} else {
return "<script type=\"text/javascript\">\n$js\n</script>\n";
}
}
/**
@ -950,7 +986,7 @@ class JavascriptRenderer
*
* @param string $requestId
* @param array $data
* @param null $suffix
* @param mixed $suffix
* @return string
*/
protected function getAddDatasetCode($requestId, $data, $suffix = null)