add template variables collector as a replacement for the former Smarty collector

This commit is contained in:
Daniel Seifert 2025-03-24 08:12:06 +01:00
parent 1309aa5976
commit f54713acb8
2 changed files with 131 additions and 0 deletions

View File

@ -20,6 +20,7 @@ use D3\DebugBar\Application\Models\Collectors\OxidConfigCollector;
use D3\DebugBar\Application\Models\Collectors\OxidShopCollector;
use D3\DebugBar\Application\Models\Collectors\OxidVersionCollector;
use D3\DebugBar\Application\Models\Collectors\SmartyCollector;
use D3\DebugBar\Application\Models\Collectors\TemplateVariablesCollector;
use D3\DebugBar\Application\Models\Exceptions\UnavailableException;
use D3\DebugBar\Application\Models\TimeDataCollectorHandler;
use DebugBar\Bridge\DoctrineCollector;
@ -117,6 +118,27 @@ class DebugBarComponent extends BaseController
return new SmartyCollector($smarty);
}
/**
* @return TemplateVariablesCollector
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function getTemplateVariablesCollector(): TemplateVariablesCollector
{
/** @var TemplateRenderer $renderer */
$renderer = ContainerFactory::getInstance()->getContainer()
->get(TemplateRendererBridgeInterface::class)
->getTemplateRenderer();
$templateEngine = $renderer->getTemplateEngine();
return new TemplateVariablesCollector(
array_merge(
$templateEngine->getGlobals(),
Registry::getConfig()->getActiveView()->getViewData()
)
);
}
/**
* @return NamespacedTwigProfileCollector
* @throws ContainerExceptionInterface
@ -188,6 +210,7 @@ class DebugBarComponent extends BaseController
// add custom collectors
$debugbar->addCollector($this->getOxidShopCollector());
$debugbar->addCollector($this->getOxidConfigCollector());
$debugbar->addCollector($this->getTemplateVariablesCollector());
/** @var TemplateRendererBridge $templateRendererBridge */
$templateRendererBridge = ContainerFactory::getInstance()->getContainer()->get(TemplateRendererBridgeInterface::class);

View File

@ -0,0 +1,108 @@
<?php
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (c) 2016 Dmitry Kosenkov
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
namespace D3\DebugBar\Application\Models\Collectors;
use DebugBar\DataCollector\DataCollector;
use DebugBar\DataCollector\Renderable;
use OxidEsales\Eshop\Core\Registry;
class TemplateVariablesCollector extends DataCollector implements Renderable
{
/**
* @var bool
*/
protected $useHtmlVarDumper = true;
/**
* Sets a flag indicating whether the Symfony HtmlDumper will be used to dump variables for
* rich variable rendering.
*
* @param bool $value
* @return $this
*/
public function useHtmlVarDumper($value = true): TemplateVariablesCollector
{
$this->useHtmlVarDumper = $value;
return $this;
}
/**
* Indicates whether the Symfony HtmlDumper will be used to dump variables for rich variable
* rendering.
*
* @return bool
*/
public function isHtmlVarDumperUsed()
{
return $this->useHtmlVarDumper;
}
public function __construct(protected array $templateVariables)
{
}
/**
* @return array
*/
public function collect(): array
{
$data = ['current view template' => Registry::getConfig()->getTopActiveView()->getTemplateName()];
$vars = $this->templateVariables;
foreach ($vars as $idx => $var) {
if ($this->isHtmlVarDumperUsed()) {
$data[$idx] = $this->getVarDumper()->renderVar($var);
} else {
$data[$idx] = $this->getDataFormatter()->formatVar($var);
}
}
return ['vars' => $data, 'count' => count($data)];
}
/**
* @return string
*/
public function getName(): string
{
return 'template_variables';
}
/**
* @return array
*/
public function getWidgets(): array
{
$widget = $this->isHtmlVarDumperUsed()
? "PhpDebugBar.Widgets.HtmlVariableListWidget"
: "PhpDebugBar.Widgets.VariableListWidget";
return [
"template_variables" => [
"icon" => "file-text",
"widget" => $widget,
"map" => "template_variables.vars",
"default" => "{}",
],
"template_variables:badge" => [
"map" => "template_variables.count",
"default" => 0,
],
];
}
}