. */ declare(strict_types=1); namespace Fisharebest\Webtrees; use DebugBar\DataCollector\MessagesCollector; use DebugBar\DataCollector\PDO\PDOCollector; use DebugBar\DataCollector\PDO\TraceablePDO; use DebugBar\DataCollector\TimeDataCollector; use DebugBar\JavascriptRenderer; use DebugBar\StandardDebugBar; use Fisharebest\Webtrees\DebugBar\ViewCollector; use PDO; /** * A static wrapper for maximebf/php-debugbar. * * @see https://github.com/maximebf/php-debugbar */ class DebugBar { /** @var StandardDebugbar|null */ private static $debugbar = null; /** @var JavascriptRenderer */ private static $renderer; /** * Initialize the Debugbar. * * @return void */ public static function enable(): void { self::$debugbar = new StandardDebugBar(); self::$debugbar->addCollector(new ViewCollector()); self::$renderer = self::$debugbar->getJavascriptRenderer('./vendor/maximebf/debugbar/src/DebugBar/Resources/'); } /** * Initialize the PDO collector. * * @param PDO $pdo * * @return PDO */ public static function initPDO(PDO $pdo): PDO { if (self::$debugbar instanceof StandardDebugBar) { $traceable_pdo = new TraceablePDO($pdo); self::$debugbar->addCollector(new PDOCollector($traceable_pdo)); } return $pdo; } /** * Render the body content. * * @return string */ public static function render(): string { if (self::$debugbar instanceof StandardDebugBar) { return self::$renderer->render(); } return ''; } /** * Render the head content. * * @return string */ public static function renderHead(): string { if (self::$debugbar instanceof StandardDebugBar) { return self::$renderer->renderHead(); } return ''; } /** * For POST/redirect responses, we "stack" the data onto the next GET request. * * @return void */ public static function stackData(): void { if (self::$debugbar instanceof StandardDebugBar) { self::$debugbar->stackData(); } } /** * For JSON responses, we send the data in HTTP headers. * * @return void */ public static function sendDataInHeaders(): void { if (self::$debugbar instanceof StandardDebugBar) { self::$debugbar->sendDataInHeaders(); } } /** * Add a message. * * @param string $message * @param string $label * @param bool $isString * * @return void */ public static function addMessage($message, $label = 'info', $isString = true): void { if (self::$debugbar instanceof StandardDebugBar) { $collector = self::$debugbar->getCollector('messages'); if ($collector instanceof MessagesCollector) { $collector->addMessage($message, $label, $isString); } } } /** * Start a timer. * * @param string $name * * @return void */ public static function startMeasure($name): void { if (self::$debugbar instanceof StandardDebugBar) { $collector = self::$debugbar->getCollector('time'); if ($collector instanceof TimeDataCollector) { $collector->startMeasure($name); } } } /** * Stop a timer. * * @param string $name * * @return void */ public static function stopMeasure($name): void { if (self::$debugbar instanceof StandardDebugBar) { $collector = self::$debugbar->getCollector('time'); if ($collector instanceof TimeDataCollector) { $collector->stopMeasure($name); } } } /** * Log an exception/throwable * * @param string $view * @param mixed[] $data * * @return void */ public static function addView(string $view, array $data): void { if (self::$debugbar instanceof StandardDebugBar) { $collector = self::$debugbar->getCollector('views'); if ($collector instanceof ViewCollector) { $collector->addView($view, $data); } } } }