aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/Debug
diff options
context:
space:
mode:
authorPhilipp Schüttlöffel <schuettloeffel@zqs.uni-hannover.de>2024-09-24 10:53:31 +0200
committerPhilipp Schüttlöffel <schuettloeffel@zqs.uni-hannover.de>2024-09-24 10:53:31 +0200
commit4459dd7917f4d1c34f40bb68f0e991e9c3d53e4c (patch)
tree5c07151ae61276d334e88f6309c30d439a85c12e /lib/classes/Debug
parentda0022e5c1abbf9825ae76debaabdff7e8623bb4 (diff)
parent97a188592c679890a25c37ab78463add76a52ff7 (diff)
Merge branch 'main' into issue-3911issue-3911
Diffstat (limited to 'lib/classes/Debug')
-rw-r--r--lib/classes/Debug/DebugBar.php12
-rw-r--r--lib/classes/Debug/TraceableStudipPDO.php24
-rw-r--r--lib/classes/Debug/TrailsCollector.php69
3 files changed, 105 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/TraceableStudipPDO.php b/lib/classes/Debug/TraceableStudipPDO.php
new file mode 100644
index 0000000..6100c22
--- /dev/null
+++ b/lib/classes/Debug/TraceableStudipPDO.php
@@ -0,0 +1,24 @@
+<?php
+namespace Studip\Debug;
+
+use DebugBar\DataCollector\PDO\TraceablePDO;
+
+final class TraceableStudipPDO extends TraceablePDO
+{
+ /**
+ * Quotes a string for use in a query.
+ *
+ * @link http://php.net/manual/en/pdo.quote.php
+ * @param string $string The string to be quoted.
+ * @param int $parameter_type [optional] Provides a data type hint for drivers that have
+ * alternate quoting styles.
+ * @return string|bool A quoted string that is theoretically safe to pass into an SQL statement.
+ * Returns FALSE if the driver does not support quoting in this way.
+ */
+ #[\ReturnTypeWillChange]
+ public function quote($string, $parameter_type = null)
+ {
+ return $this->pdo->quote($string, $parameter_type);
+ }
+
+}
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()),
+ ],
+ ];
+ }
+}