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/Debug | |
| parent | 4e798558115a71c61d9a5f77a909356aeb0873b6 (diff) | |
add debug bar, fixes #4220
Closes #4220
Merge request studip/studip!3049
Diffstat (limited to 'lib/classes/Debug')
| -rw-r--r-- | lib/classes/Debug/DebugBar.php | 12 | ||||
| -rw-r--r-- | lib/classes/Debug/TrailsCollector.php | 69 |
2 files changed, 81 insertions, 0 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()), + ], + ]; + } +} |
