mc-cms-namelessmc/core/classes/Misc/DebugBarHelper.php

96 lines
2.8 KiB
PHP
Raw Permalink Normal View History

2021-12-25 22:26:25 -08:00
<?php
use DebugBar\Bridge\NamespacedTwigProfileCollector;
use DebugBar\DataCollector\ConfigCollector;
use DebugBar\DataCollector\DataCollector;
2022-01-07 14:48:48 -08:00
use DebugBar\DataCollector\MemoryCollector;
use DebugBar\DataCollector\PDO\PDOCollector;
2022-01-07 14:48:48 -08:00
use DebugBar\DataCollector\PhpInfoCollector;
use DebugBar\DataCollector\RequestDataCollector;
2022-01-07 14:48:48 -08:00
use DebugBar\DataCollector\TimeDataCollector;
2021-12-25 22:26:25 -08:00
use DebugBar\DebugBar;
2021-12-25 22:31:15 -08:00
use Junker\DebugBar\Bridge\SmartyCollector;
use Twig\Environment;
use Twig\Extension\ProfilerExtension;
use Twig\Profiler\Profile;
2021-12-25 22:26:25 -08:00
/**
* Class to help integrate the PHPDebugBar with NamelessMC.
*
* @package NamelessMC\Misc
* @author Aberdeener
* @version 2.2.0
* @license MIT
*/
class DebugBarHelper extends Instanceable
{
private ?DebugBar $_debugBar = null;
2021-12-25 22:26:25 -08:00
/**
* Enable the PHPDebugBar.
2021-12-25 22:26:25 -08:00
*/
public function enable(): void
{
$debugbar = new DebugBar();
2022-01-07 14:48:48 -08:00
$debugbar->addCollector(new TimeDataCollector());
$requestCollector = new RequestDataCollector();
$requestCollector->useHtmlVarDumper();
$debugbar->addCollector($requestCollector);
$debugbar->addCollector(EventCollector::getInstance());
$debugbar->addCollector(CacheCollector::getInstance());
$configCollector = new ConfigCollector();
$configCollector->useHtmlVarDumper();
$configCollector->setData(array_filter(Config::all(), static function ($key) {
return $key !== 'mysql' && $key !== 'email' && $key !== 'authme';
}, ARRAY_FILTER_USE_KEY));
$debugbar->addCollector($configCollector);
2021-12-25 22:26:25 -08:00
$pdoCollector = new PDOCollector(DB::getInstance()->getPDO());
$pdoCollector->setRenderSqlWithParams(true, "'");
2021-12-25 22:26:25 -08:00
$debugbar->addCollector($pdoCollector);
2022-01-07 14:48:48 -08:00
$debugbar->addCollector(new PhpInfoCollector());
$debugbar->addCollector(new MemoryCollector());
2021-12-25 22:31:15 -08:00
2021-12-25 23:02:05 -08:00
$this->_debugBar = $debugbar;
2021-12-25 22:26:25 -08:00
}
public function addCollector(DataCollector $collector): void
{
$this->_debugBar->addCollector($collector);
}
public function addSmartyCollector(Smarty $smarty): void
{
$smartyCollector = new SmartyCollector($smarty);
if ($this->getDebugBar()->hasCollector($smartyCollector->getName())) {
return;
}
$smartyCollector->useHtmlVarDumper();
$this->addCollector($smartyCollector);
}
public function addTwigCollector(Environment $twig, Profile $profile): void
{
$twig->addExtension(new ProfilerExtension($profile));
$collector = new NamespacedTwigProfileCollector($profile);
if ($this->getDebugBar()->hasCollector($collector->getName())) {
return;
}
$this->addCollector($collector);
}
public function getDebugBar(): ?DebugBar
{
2021-12-25 23:02:05 -08:00
return $this->_debugBar;
2021-12-25 22:26:25 -08:00
}
}