diff options
| author | Jan-Hendrik Willms <tleilax+studip@gmail.com> | 2024-05-28 07:55:24 +0000 |
|---|---|---|
| committer | Jan-Hendrik Willms <tleilax+studip@gmail.com> | 2024-05-28 07:55:24 +0000 |
| commit | fe2b584cb79ba8a2c642be085cacdd82d526df25 (patch) | |
| tree | 875665187b3f3c9e26bc0056bf9d658c8b724d29 /lib | |
| parent | 4e798558115a71c61d9a5f77a909356aeb0873b6 (diff) | |
add debug bar, fixes #4220
Closes #4220
Merge request studip/studip!3049
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/bootstrap-definitions.php | 46 | ||||
| -rw-r--r-- | lib/bootstrap.php | 11 | ||||
| -rw-r--r-- | lib/classes/Debug/DebugBar.php | 12 | ||||
| -rw-r--r-- | lib/classes/Debug/TrailsCollector.php | 69 | ||||
| -rw-r--r-- | lib/classes/PageLayout.php | 15 | ||||
| -rw-r--r-- | lib/classes/StudipController.php | 16 |
6 files changed, 160 insertions, 9 deletions
diff --git a/lib/bootstrap-definitions.php b/lib/bootstrap-definitions.php index d34d023..2f59dc9 100644 --- a/lib/bootstrap-definitions.php +++ b/lib/bootstrap-definitions.php @@ -1,10 +1,18 @@ <?php +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 Monolog\Handler\StreamHandler; use Monolog\Logger; use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; +use function DI\create; + return [ LoggerInterface::class => DI\factory(function () { return new Logger('studip', [ @@ -17,10 +25,46 @@ return [ \Studip\Cache\Cache::class => DI\factory(function () { return \Studip\Cache\Factory::getCache(); }), - StudipPDO::class => DI\factory(function () { + PDO::class => DI\factory(function () { return DBManager::get(); }), Trails\Dispatcher::class => DI\factory(function (ContainerInterface $container) { return new \StudipDispatcher($container); }), + DebugBar\DebugBar::class => DI\factory(function (ContainerInterface $container) { + $debugBar = new DebugBar\DebugBar(); + $debugBar->addCollector(new PhpInfoCollector()); + $debugBar->addCollector(new RequestDataCollector()); + $debugBar->addCollector(new MemoryCollector()); + $debugBar->addCollector(new ExceptionsCollector()); + + // Future Improvements, not used/activated right now + # $debugBar->addCollector(new MessagesCollector()); + # $debugBar->addCollector(new TimeDataCollector()); + + $config = iterator_to_array(Config::getInstance()->getIterator()); + ksort($config); + $debugBar->addCollector(new DebugBar\DataCollector\ConfigCollector($config)); + + $pdo = $container->get(PDO::class); + if ($pdo instanceof DebugBar\DataCollector\PDO\TraceablePDO) { + $collector = new DebugBar\DataCollector\PDO\PDOCollector($pdo); + $debugBar->addCollector($collector); + } + + return $debugBar; + }), + StudipPDO::class => DI\factory(function () { + $pdo = new StudipPDO( + "mysql:host={$GLOBALS['DB_STUDIP_HOST']};dbname={$GLOBALS['DB_STUDIP_DATABASE']};charset=utf8mb4", + $GLOBALS['DB_STUDIP_USER'], + $GLOBALS['DB_STUDIP_PASSWORD'] + ); + + if (Studip\Debug\DebugBar::isActivated()) { + $pdo = new DebugBar\DataCollector\PDO\TraceablePDO($pdo); + } + + return $pdo; + }), ]; diff --git a/lib/bootstrap.php b/lib/bootstrap.php index e9c9bb5..8c618ff 100644 --- a/lib/bootstrap.php +++ b/lib/bootstrap.php @@ -129,13 +129,10 @@ $GLOBALS['template_factory'] = new Flexi\Factory("{$STUDIP_BASE_PATH}/templates" // set default pdo connection try { - DBManager::getInstance() - ->setConnection('studip', - 'mysql:host=' . $GLOBALS['DB_STUDIP_HOST'] . - ';dbname=' . $GLOBALS['DB_STUDIP_DATABASE'] . - ';charset=utf8mb4', - $GLOBALS['DB_STUDIP_USER'], - $GLOBALS['DB_STUDIP_PASSWORD']); + DBManager::getInstance()->setConnection( + 'studip', + app(StudipPDO::class) + ); } catch (PDOException $exception) { if (Studip\ENV === 'development') { throw $exception; diff --git a/lib/classes/Debug/DebugBar.php b/lib/classes/Debug/DebugBar.php new file mode 100644 index 0000000..bbd80c9 --- /dev/null +++ b/lib/classes/Debug/DebugBar.php @@ -0,0 +1,12 @@ +<?php +namespace Studip\Debug; + +final class DebugBar +{ + public static function isActivated(): bool + { + return \Studip\ENV === 'development' + && ($_ENV['DEBUG_BAR'] ?? false) + && class_exists(\DebugBar\DebugBar::class); + } +} diff --git a/lib/classes/Debug/TrailsCollector.php b/lib/classes/Debug/TrailsCollector.php new file mode 100644 index 0000000..f4b8a65 --- /dev/null +++ b/lib/classes/Debug/TrailsCollector.php @@ -0,0 +1,69 @@ +<?php +namespace Studip\Debug; + +use DebugBar\DataCollector\DataCollector; +use DebugBar\DataCollector\Renderable; +use Trails\Controller; + +final class TrailsCollector extends DataCollector implements Renderable +{ + public function __construct( + private readonly Controller $controller + ) { + $this->useHtmlVarDumper(false); + } + + public function collect() + { + $data = []; + foreach ($this->controller->get_assigned_variables() as $k => $v) { + if ($this->isHtmlVarDumperUsed()) { + $v = $this->getVarDumper()->renderVar($v); + } else if (!is_string($v)) { + $v = $this->getDataFormatter()->formatVar($v); + } + $data[$k] = $v; + } + + ksort($data); + + return $data; + } + + public function getName() + { + return 'trails'; + } + + /** + * @return array + */ + public function getAssets() + { + return $this->isHtmlVarDumperUsed() ? $this->getVarDumper()->getAssets() : []; + } + + /** + * @return array[] + */ + public function getWidgets() + { + $name = $this->getName(); + $widget = $this->isHtmlVarDumperUsed() + ? 'PhpDebugBar.Widgets.HtmlVariableListWidget' + : 'PhpDebugBar.Widgets.VariableListWidget'; + + return [ + $name => [ + 'icon' => 'code', + 'widget' => $widget, + 'map' => $name, + 'default' => '{}' + ], + "{$name}:badge" => [ + 'map' => "{$name}:variable__count", + 'default' => count($this->controller->get_assigned_variables()), + ], + ]; + } +} diff --git a/lib/classes/PageLayout.php b/lib/classes/PageLayout.php index d3f3028..a4178a5 100644 --- a/lib/classes/PageLayout.php +++ b/lib/classes/PageLayout.php @@ -139,6 +139,21 @@ class PageLayout self::addScript('studip-wysiwyg.js?v=' . $v); self::addStylesheet('print.css?v=' . $v, ['media' => 'print']); + + if (Studip\Debug\DebugBar::isActivated()) { + $old_base = URLHelper::setBaseURL($GLOBALS['ABSOLUTE_URI_STUDIP']); + + self::addHeadElement('link', [ + 'href' => URLHelper::getURL('dispatch.php/debugbar/css'), + 'rel' => 'stylesheet', + 'type' => 'text/css', + ]); + self::addHeadElement('script', [ + 'src' => URLHelper::getURL('dispatch.php/debugbar/js'), + ]); + + URLHelper::setBaseURL($old_base); + } } /** diff --git a/lib/classes/StudipController.php b/lib/classes/StudipController.php index 4fbcc42..0643836 100644 --- a/lib/classes/StudipController.php +++ b/lib/classes/StudipController.php @@ -9,6 +9,7 @@ * the License, or (at your option) any later version. */ +use DebugBar\DebugBar; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Csv; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; @@ -294,7 +295,7 @@ abstract class StudipController extends Trails\Controller // Extract fragment (if any) if (strpos($to, '#') !== false) { - list($args[0], $fragment) = explode('#', $to); + [$args[0], $fragment] = explode('#', $to); } // Extract parameters (if any) @@ -666,6 +667,19 @@ abstract class StudipController extends Trails\Controller return $this->response; } + public function render_template($template_name, $layout = null) + { + if (Studip\Debug\DebugBar::isActivated()) { + $debugbar = app()->get(Debugbar::class); + if (!isset($debugbar['trails'])) { + $collector = new \Studip\Debug\TrailsCollector($this); + $debugbar->addCollector($collector); + } + } + + parent::render_template($template_name, $layout); + } + /** * Renders a given template and returns the resulting string. * |
