From fd19da0c30b5e44bacddfb0481831dd084df9eab Mon Sep 17 00:00:00 2001 From: mscherer Date: Wed, 18 Jun 2025 14:56:31 +0200 Subject: [PATCH] POC Dump panel. --- config/bootstrap.php | 24 ++++++++- src/DebugStatements.php | 90 ++++++++++++++++++++++++++++++++ src/Panel/DumpPanel.php | 36 +++++++++++++ src/ToolbarService.php | 1 + templates/element/dump_panel.php | 35 +++++++++++++ 5 files changed, 184 insertions(+), 2 deletions(-) create mode 100644 src/DebugStatements.php create mode 100644 src/Panel/DumpPanel.php create mode 100644 templates/element/dump_panel.php diff --git a/config/bootstrap.php b/config/bootstrap.php index 2afe7f8b..2145b5cd 100644 --- a/config/bootstrap.php +++ b/config/bootstrap.php @@ -14,8 +14,10 @@ */ use Cake\Database\Query; use Cake\Datasource\ConnectionManager; +use Cake\Error\Debugger; use Cake\Log\Log; use DebugKit\DebugSql; +use DebugKit\DebugStatements; $hasDebugKitConfig = ConnectionManager::getConfig('debug_kit'); if (!$hasDebugKitConfig && !in_array('sqlite', PDO::getAvailableDrivers())) { @@ -37,6 +39,24 @@ ]); } +if (!function_exists('debugKitDump')) { + function debugKitDump(mixed $var, ?bool $showHtml = null, bool $showFrom = true): mixed + { + $location = []; + if ($showFrom) { + $trace = Debugger::trace(['start' => 0, 'depth' => 1, 'format' => 'array']); + if (isset($trace[0]['line']) && isset($trace[0]['file'])) { + $location = [ + 'line' => $trace[0]['line'], + 'file' => $trace[0]['file'], + ]; + } + } + + return DebugStatements::add($var, $showHtml, $location); + } +} + if (!function_exists('sql')) { /** * Prints out the SQL statements generated by a Query object. @@ -50,7 +70,7 @@ * data in a browser-friendly way. * @return \Cake\Database\Query */ - function sql(Query $query, $showValues = true, $showHtml = null) + function sql(Query $query, bool $showValues = true, ?bool $showHtml = null): Query { return DebugSql::sql($query, $showValues, $showHtml, 1); } @@ -69,7 +89,7 @@ function sql(Query $query, $showValues = true, $showHtml = null) * data in a browser-friendly way. * @return void */ - function sqld(Query $query, $showValues = true, $showHtml = null) + function sqld(Query $query, bool $showValues = true, ?bool $showHtml = null): void { DebugSql::sqld($query, $showValues, $showHtml, 2); } diff --git a/src/DebugStatements.php b/src/DebugStatements.php new file mode 100644 index 00000000..ea251c12 --- /dev/null +++ b/src/DebugStatements.php @@ -0,0 +1,90 @@ + + */ + protected static $statements = []; + + /** + * @return array + */ + public static function all(): array + { + return static::$statements; + } + + /** + * Template used for HTML output. + * + * @var string + */ + private static string $templateHtml = << +%s +
+%s
+
+ +HTML; + + /** + * Template used for CLI and text output. + * + * @var string + */ + private static string $templateText = << $var, + 'showHtml' => $showHtml, + 'location' => $location, + ]; + + return ''; + } +} diff --git a/src/Panel/DumpPanel.php b/src/Panel/DumpPanel.php new file mode 100644 index 00000000..09b7015d --- /dev/null +++ b/src/Panel/DumpPanel.php @@ -0,0 +1,36 @@ + DebugStatements::all(), + ]; + } +} diff --git a/src/ToolbarService.php b/src/ToolbarService.php index 729fee5f..206d094c 100644 --- a/src/ToolbarService.php +++ b/src/ToolbarService.php @@ -55,6 +55,7 @@ class ToolbarService protected array $_defaultConfig = [ 'panels' => [ 'DebugKit.Cache' => true, + 'DebugKit.Dump' => true, 'DebugKit.Request' => true, 'DebugKit.SqlLog' => true, 'DebugKit.Timer' => true, diff --git a/templates/element/dump_panel.php b/templates/element/dump_panel.php new file mode 100644 index 00000000..a415fc76 --- /dev/null +++ b/templates/element/dump_panel.php @@ -0,0 +1,35 @@ + +
+ ' . h($statement['var']) . ''; + if (!empty($statement['location'])) { + echo '' . h($statement['location']['file']) . ':' . h($statement['location']['line']) . ''; + } + } + + ?> +

+ +