DebugBar/Application/Component/DebugBarComponent.php
2025-03-24 10:40:55 +01:00

256 lines
8.4 KiB
PHP

<?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) 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\Component;
use D3\DebugBar\Application\Models\AvailabilityCheck;
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;
use DebugBar\Bridge\MonologCollector;
use DebugBar\Bridge\NamespacedTwigProfileCollector;
use DebugBar\DataCollector\ExceptionsCollector;
use DebugBar\DataCollector\MemoryCollector;
use DebugBar\DataCollector\MessagesCollector;
use DebugBar\DataCollector\PhpInfoCollector;
use DebugBar\DataCollector\RequestDataCollector;
use DebugBar\DataCollector\TimeDataCollector;
use DebugBar\DebugBar;
use DebugBar\DebugBarException;
use DebugBar\JavascriptRenderer;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Logging\DebugStack;
use Monolog\Logger;
use OxidEsales\Eshop\Core\Controller\BaseController;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProviderInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateRenderer;
use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateRendererBridge;
use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateRendererBridgeInterface;
use OxidEsales\Twig\TwigEngine;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionClass;
use ReflectionException;
use Twig\Profiler\Profile;
class DebugBarComponent extends BaseController
{
protected DebugBar|null $debugBar = null;
protected null|JavascriptRenderer $debugBarRenderer = null;
/**
* Marking object as component
* @var bool
*/
protected $_blIsComponent = true;
/**
* @throws ContainerExceptionInterface
* @throws DebugBarException
* @throws NotFoundExceptionInterface
*/
public function __construct()
{
parent::__construct();
if (AvailabilityCheck::isAvailable()) {
$debugbar = new DebugBar();
$this->addCollectors($debugbar);
$debugbarRenderer = $debugbar->getJavascriptRenderer();
$debugbarRenderer->setBaseUrl(Registry::getConfig()->getOutUrl() . 'debugbar');
$this->debugBar = $debugbar;
$this->debugBarRenderer = $debugbarRenderer;
}
}
/**
* @return MonologCollector
*/
public function getMonologCollector(): MonologCollector
{
/** @var Logger $monolog */
$monolog = Registry::getLogger();
return new MonologCollector($monolog);
}
/**
* @return DoctrineCollector
* @throws ContainerExceptionInterface
* @throws DebugBarException
* @throws NotFoundExceptionInterface
*/
public function getDoctrineCollector(): DoctrineCollector
{
/** @var Connection $connection */
$connection = ContainerFactory::getInstance()->getContainer()->get(ConnectionProviderInterface::class)->get();
$debugStack = new DebugStack();
$connection->getConfiguration()->setSQLLogger($debugStack);
return new DoctrineCollector($debugStack);
}
/**
* @return SmartyCollector
*/
public function getSmartyCollector(): SmartyCollector
{
$smarty = Registry::getUtilsView()->getSmarty();
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(
Registry::getConfig()->getActiveView()->getViewData(),
$templateEngine->getGlobals()
)
);
}
/**
* @return NamespacedTwigProfileCollector
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function getTwigCollector(): NamespacedTwigProfileCollector
{
/** @var Profile $profile */
$profile = ContainerFactory::getInstance()->getContainer()->get(Profile::class);
return new NamespacedTwigProfileCollector($profile);
}
/**
* @return OxidShopCollector
*/
public function getOxidShopCollector(): OxidShopCollector
{
return oxNew(OxidShopCollector::class);
}
/**
* @return OxidConfigCollector
*/
public function getOxidConfigCollector(): OxidConfigCollector
{
return oxNew(OxidConfigCollector::class, Registry::getConfig());
}
/**
* @return OxidVersionCollector
*/
public function getOxidVersionCollector(): OxidVersionCollector
{
return oxNew(OxidVersionCollector::class);
}
/**
* @param DebugBar $debugbar
* @return void
* @throws ContainerExceptionInterface
* @throws DebugBarException
* @throws NotFoundExceptionInterface
*/
public function addCollectors(DebugBar $debugbar): void
{
// add all default collectors except the useless ExceptionCollector
$debugbar->addCollector(new PhpInfoCollector());
$debugbar->addCollector(new MessagesCollector());
$debugbar->addCollector(new RequestDataCollector());
$debugbar->addCollector(new TimeDataCollector());
$debugbar->addCollector(new MemoryCollector());
$debugbar->addCollector(new ExceptionsCollector());
// 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);
switch (get_class($templateRendererBridge->getTemplateRenderer()->getTemplateEngine())) {
case TwigEngine::class:
$debugbar->addCollector($this->getTwigCollector());
break;
default:
dumpvar(get_class($templateRendererBridge->getTemplateRenderer()->getTemplateEngine()));
}
$debugbar->addCollector($this->getMonologCollector());
$debugbar->addCollector($this->getDoctrineCollector());
$debugbar->addCollector($this->getOxidVersionCollector());
}
/**
* @return void
* @throws UnavailableException
* @throws ReflectionException
*/
public function addTimelineMeasures(): void
{
if (false === $this->debugBar instanceof DebugBar) {
throw new UnavailableException();
}
$collectors = $this->debugBar->getCollectors();
$collectors['time'] = TimeDataCollectorHandler::getInstance();
$reflection = new ReflectionClass($this->debugBar);
$property = $reflection->getProperty('collectors');
$property->setAccessible(true);
$property->setValue($this->debugBar, $collectors);
}
/**
* @return DebugBar
* @throws UnavailableException
*/
public function getDebugBar(): DebugBar
{
if (false === $this->debugBar instanceof DebugBar) {
throw new UnavailableException();
}
return $this->debugBar;
}
/**
* @return JavascriptRenderer
*/
public function getRenderer(): JavascriptRenderer
{
return $this->debugBarRenderer;
}
}