diff --git a/Application/Component/DebugBarComponent.php b/Application/Component/DebugBarComponent.php index e5c1173..26c8768 100644 --- a/Application/Component/DebugBarComponent.php +++ b/Application/Component/DebugBarComponent.php @@ -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); diff --git a/Application/Models/Collectors/TemplateVariablesCollector.php b/Application/Models/Collectors/TemplateVariablesCollector.php new file mode 100644 index 0000000..9260817 --- /dev/null +++ b/Application/Models/Collectors/TemplateVariablesCollector.php @@ -0,0 +1,108 @@ + + * @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, + ], + ]; + } +}