Skip to content

Commit 917a032

Browse files
authored
Allow dataset to defer loading instead of passing all the data (php-debugbar#723)
* Allow dataset to defer loading instead of passing all the data * Check data when loading * Tweak loading * Make configurable
1 parent c683e78 commit 917a032

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

src/DebugBar/JavascriptRenderer.php

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class JavascriptRenderer
8484

8585
protected $ajaxHandlerEnableTab = false;
8686

87+
protected $deferDatasets = false;
88+
8789
protected $openHandlerClass = 'PhpDebugBar.OpenHandler';
8890

8991
protected $openHandlerUrl;
@@ -194,6 +196,9 @@ public function setOptions(array $options)
194196
if (array_key_exists('ajax_handler_enable_tab', $options)) {
195197
$this->setAjaxHandlerEnableTab($options['ajax_handler_enable_tab']);
196198
}
199+
if (array_key_exists('defer_datasets', $options)) {
200+
$this->setDeferDatasets($options['defer_datasets']);
201+
}
197202
if (array_key_exists('open_handler_classname', $options)) {
198203
$this->setOpenHandlerClass($options['open_handler_classname']);
199204
}
@@ -642,6 +647,28 @@ public function isAjaxHandlerTabEnabled()
642647
}
643648

644649

650+
/**
651+
* Sets whether datasets are directly loaded or deferred
652+
*
653+
* @param boolean $enabled
654+
*/
655+
public function setDeferDatasets($defer = true)
656+
{
657+
$this->deferDatasets = $defer;
658+
return $this;
659+
}
660+
661+
/**
662+
* Check if the datasets are deffered
663+
*
664+
* @return boolean
665+
*/
666+
public function areDatasetsDeferred()
667+
{
668+
return $this->deferDatasets;
669+
}
670+
671+
645672
/**
646673
* Sets the class name of the js open handler
647674
*
@@ -1110,12 +1137,21 @@ public function render($initialize = true, $renderStackedData = true)
11101137

11111138
if ($renderStackedData && $this->debugBar->hasStackedData()) {
11121139
foreach ($this->debugBar->getStackedData() as $id => $data) {
1113-
$js .= $this->getAddDatasetCode($id, $data, '(stacked)');
1140+
if ($this->areDatasetsDeferred()) {
1141+
$js .= $this->getLoadDatasetCode($id, '(stacked)');
1142+
} else {
1143+
$js .= $this->getAddDatasetCode($id, $data, '(stacked)');
1144+
1145+
}
11141146
}
11151147
}
11161148

11171149
$suffix = !$initialize ? '(ajax)' : null;
1118-
$js .= $this->getAddDatasetCode($this->debugBar->getCurrentRequestId(), $this->debugBar->getData(), $suffix);
1150+
if ($this->areDatasetsDeferred()) {
1151+
$js .= $this->getLoadDatasetCode($this->debugBar->getCurrentRequestId(), $suffix);
1152+
} else {
1153+
$js .= $this->getAddDatasetCode($this->debugBar->getCurrentRequestId(), $this->debugBar->getData(), $suffix);
1154+
}
11191155

11201156
$nonce = $this->getNonceAttribute();
11211157

@@ -1279,6 +1315,23 @@ protected function getAddDatasetCode($requestId, $data, $suffix = null)
12791315
return $js;
12801316
}
12811317

1318+
/**
1319+
* Returns the js code needed to load a dataset with the OpenHandler
1320+
*
1321+
* @param string $requestId
1322+
* @param mixed $suffix
1323+
* @return string
1324+
*/
1325+
protected function getLoadDatasetCode($requestId, $suffix = null)
1326+
{
1327+
$js = sprintf("%s.loadDataSet(\"%s\"%s);\n",
1328+
$this->variableName,
1329+
$requestId,
1330+
$suffix ? ", " . json_encode($suffix) : ''
1331+
);
1332+
return $js;
1333+
}
1334+
12821335
/**
12831336
* If a nonce it set, create the correct attribute
12841337
* @return string

0 commit comments

Comments
 (0)