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/classes | |
| parent | 4e798558115a71c61d9a5f77a909356aeb0873b6 (diff) | |
add debug bar, fixes #4220
Closes #4220
Merge request studip/studip!3049
Diffstat (limited to 'lib/classes')
| -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 |
4 files changed, 111 insertions, 1 deletions
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. * |
