DebugBar/Application/Component/DebugBarComponent.php

228 lines
6.9 KiB
PHP
Raw Permalink Normal View History

2022-07-30 20:58:10 +02:00
<?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;
2022-08-03 09:57:50 +02:00
use D3\DebugBar\Application\Models\Collectors\OxidConfigCollector;
2022-08-04 16:03:32 +02:00
use D3\DebugBar\Application\Models\Collectors\OxidShopCollector;
use D3\DebugBar\Application\Models\Collectors\OxidVersionCollector;
2022-07-31 21:58:12 +02:00
use D3\DebugBar\Application\Models\Collectors\SmartyCollector;
use D3\DebugBar\Application\Models\TimeDataCollectorHandler;
2022-07-30 20:58:10 +02:00
use DebugBar\Bridge\DoctrineCollector;
use DebugBar\Bridge\MonologCollector;
2022-08-16 22:59:44 +02:00
use DebugBar\Bridge\NamespacedTwigProfileCollector;
use DebugBar\Bridge\Twig\TwigCollector;
use DebugBar\Bridge\TwigProfileCollector;
use DebugBar\DataCollector\ExceptionsCollector;
2022-08-05 09:23:48 +02:00
use DebugBar\DataCollector\MemoryCollector;
use DebugBar\DataCollector\MessagesCollector;
use DebugBar\DataCollector\PhpInfoCollector;
use DebugBar\DataCollector\RequestDataCollector;
use DebugBar\DataCollector\TimeDataCollector;
use DebugBar\DebugBar;
2022-07-30 20:58:10 +02:00
use DebugBar\DebugBarException;
use DebugBar\JavascriptRenderer;
2022-08-03 09:12:45 +02:00
use Doctrine\DBAL\Connection;
2022-07-30 20:58:10 +02:00
use Doctrine\DBAL\Logging\DebugStack;
use Monolog\Logger;
2022-07-30 20:58:10 +02:00
use OxidEsales\Eshop\Core\Controller\BaseController;
use OxidEsales\Eshop\Core\DatabaseProvider;
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
use OxidEsales\Eshop\Core\Registry;
2022-08-16 22:59:44 +02:00
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
use OxidEsales\Twig\Loader\ContentTemplateLoader;
2022-07-30 20:58:10 +02:00
use ReflectionClass;
use ReflectionException;
2022-08-16 22:59:44 +02:00
use Twig\Environment;
use Twig\Extension\ProfilerExtension;
use Twig\Profiler\Profile;
2022-07-30 20:58:10 +02:00
class DebugBarComponent extends BaseController
{
2022-08-05 09:23:48 +02:00
/** @var DebugBar */
protected $debugBar;
2022-07-30 20:58:10 +02:00
/** @var JavascriptRenderer */
protected $debugBarRenderer;
2022-07-30 20:58:10 +02:00
/**
* Marking object as component
* @var bool
*/
protected $_blIsComponent = true;
/**
* @throws DebugBarException
* @throws DatabaseConnectionException
* @throws ReflectionException
*/
public function __construct()
{
parent::__construct();
if (false === isAdmin()) {
2022-08-05 09:23:48 +02:00
$debugbar = new DebugBar();
2022-07-30 20:58:10 +02:00
2022-07-31 21:58:12 +02:00
$this->addCollectors($debugbar);
2022-07-30 20:58:10 +02:00
$debugbarRenderer = $debugbar->getJavascriptRenderer();
$debugbarRenderer->setBaseUrl(Registry::getConfig()->getOutUrl() . 'debugbar');
$this->debugBar = $debugbar;
2022-07-30 20:58:10 +02:00
$this->debugBarRenderer = $debugbarRenderer;
}
}
/**
* @return MonologCollector
*/
public function getMonologCollector(): MonologCollector
{
/** @var Logger $monolog */
$monolog = Registry::getLogger();
2022-07-30 20:58:10 +02:00
return new MonologCollector($monolog);
}
/**
* @return DoctrineCollector
* @throws ReflectionException
* @throws DebugBarException
* @throws DatabaseConnectionException
*/
public function getDoctrineCollector(): DoctrineCollector
{
$db = DatabaseProvider::getDb();
$debugStack = new DebugStack();
2022-08-03 09:12:45 +02:00
/** @var Connection $connection */
2022-07-30 20:58:10 +02:00
$connection = $this->getNonPublicProperty($db, 'connection');
$connection->getConfiguration()->setSQLLogger($debugStack);
return new DoctrineCollector($debugStack);
}
2022-07-31 21:58:12 +02:00
/**
* @return SmartyCollector
*/
public function getSmartyCollector(): SmartyCollector
{
$smarty = Registry::getUtilsView()->getSmarty();
return new SmartyCollector($smarty);
2022-07-31 21:58:12 +02:00
}
2022-08-04 16:03:32 +02:00
/**
* @return OxidShopCollector
*/
public function getOxidShopCollector(): OxidShopCollector
{
return oxNew(OxidShopCollector::class);
}
2022-08-03 09:57:50 +02:00
/**
* @return OxidConfigCollector
*/
public function getOxidConfigCollector(): OxidConfigCollector
{
return oxNew(OxidConfigCollector::class, Registry::getConfig());
}
2022-08-04 16:03:32 +02:00
/**
* @return OxidVersionCollector
*/
public function getOxidVersionCollector(): OxidVersionCollector
{
return oxNew(OxidVersionCollector::class);
}
2022-07-30 20:58:10 +02:00
/**
2022-08-03 09:12:45 +02:00
* @param object $object
* @param string $propName
*
2022-07-30 20:58:10 +02:00
* @return mixed
* @throws ReflectionException
*/
2022-08-03 09:12:45 +02:00
protected function getNonPublicProperty(object $object, string $propName)
2022-07-30 20:58:10 +02:00
{
$reflection = new ReflectionClass($object);
$property = $reflection->getProperty($propName);
$property->setAccessible(true);
return $property->getValue($object);
}
2022-07-31 21:58:12 +02:00
/**
2022-08-05 09:23:48 +02:00
* @param DebugBar $debugbar
2022-07-31 21:58:12 +02:00
* @return void
* @throws DatabaseConnectionException
* @throws DebugBarException
* @throws ReflectionException
*/
2022-08-05 09:23:48 +02:00
public function addCollectors(DebugBar $debugbar): void
2022-07-31 21:58:12 +02:00
{
2022-08-05 09:23:48 +02:00
// 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());
2022-08-05 09:23:48 +02:00
2022-08-16 22:59:44 +02:00
/*
$container = ContainerFactory::getInstance()->getContainer();
/** @var ContentTemplateLoader $contentTemplateLoader */
/*
$contentTemplateLoader = $container->get(ContentTemplateLoader::class);
$twigEnv = new Environment($contentTemplateLoader);
$twigProfile = new Profile();
$twigEnv->addExtension(new ProfilerExtension($twigProfile));
*/
2022-08-05 09:23:48 +02:00
// add custom collectors
2022-08-04 16:03:32 +02:00
$debugbar->addCollector($this->getOxidShopCollector());
$debugbar->addCollector($this->getOxidConfigCollector());
$debugbar->addCollector($this->getSmartyCollector());
2022-08-16 22:59:44 +02:00
//$debugbar->addCollector(new NamespacedTwigProfileCollector($twigProfile, $contentTemplateLoader));
2022-07-31 21:58:12 +02:00
$debugbar->addCollector($this->getMonologCollector());
$debugbar->addCollector($this->getDoctrineCollector());
2022-08-04 16:03:32 +02:00
$debugbar->addCollector($this->getOxidVersionCollector());
2022-07-31 21:58:12 +02:00
}
/**
* @return void
*/
public function addTimelineMessures(): void
{
$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);
}
2022-08-03 00:32:09 +02:00
/**
2022-08-05 09:23:48 +02:00
* @return DebugBar
2022-08-03 00:32:09 +02:00
*/
2022-08-05 09:23:48 +02:00
public function getDebugBar(): DebugBar
2022-08-03 00:32:09 +02:00
{
return $this->debugBar;
}
/**
* @return JavascriptRenderer
*/
public function getRenderer(): JavascriptRenderer
{
return $this->debugBarRenderer;
}
}