Skip to content

Commit fd19da0

Browse files
committed
POC Dump panel.
1 parent 8c6bd6c commit fd19da0

File tree

5 files changed

+184
-2
lines changed

5 files changed

+184
-2
lines changed

config/bootstrap.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
*/
1515
use Cake\Database\Query;
1616
use Cake\Datasource\ConnectionManager;
17+
use Cake\Error\Debugger;
1718
use Cake\Log\Log;
1819
use DebugKit\DebugSql;
20+
use DebugKit\DebugStatements;
1921

2022
$hasDebugKitConfig = ConnectionManager::getConfig('debug_kit');
2123
if (!$hasDebugKitConfig && !in_array('sqlite', PDO::getAvailableDrivers())) {
@@ -37,6 +39,24 @@
3739
]);
3840
}
3941

42+
if (!function_exists('debugKitDump')) {
43+
function debugKitDump(mixed $var, ?bool $showHtml = null, bool $showFrom = true): mixed
44+
{
45+
$location = [];
46+
if ($showFrom) {
47+
$trace = Debugger::trace(['start' => 0, 'depth' => 1, 'format' => 'array']);
48+
if (isset($trace[0]['line']) && isset($trace[0]['file'])) {
49+
$location = [
50+
'line' => $trace[0]['line'],
51+
'file' => $trace[0]['file'],
52+
];
53+
}
54+
}
55+
56+
return DebugStatements::add($var, $showHtml, $location);
57+
}
58+
}
59+
4060
if (!function_exists('sql')) {
4161
/**
4262
* Prints out the SQL statements generated by a Query object.
@@ -50,7 +70,7 @@
5070
* data in a browser-friendly way.
5171
* @return \Cake\Database\Query
5272
*/
53-
function sql(Query $query, $showValues = true, $showHtml = null)
73+
function sql(Query $query, bool $showValues = true, ?bool $showHtml = null): Query
5474
{
5575
return DebugSql::sql($query, $showValues, $showHtml, 1);
5676
}
@@ -69,7 +89,7 @@ function sql(Query $query, $showValues = true, $showHtml = null)
6989
* data in a browser-friendly way.
7090
* @return void
7191
*/
72-
function sqld(Query $query, $showValues = true, $showHtml = null)
92+
function sqld(Query $query, bool $showValues = true, ?bool $showHtml = null): void
7393
{
7494
DebugSql::sqld($query, $showValues, $showHtml, 2);
7595
}

src/DebugStatements.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
6+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
7+
*
8+
* Licensed under The MIT License
9+
* Redistributions of files must retain the above copyright notice.
10+
*
11+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
12+
* @link https://cakephp.org CakePHP(tm) Project
13+
* @since DebugKit 5.1
14+
* @license https://www.opensource.org/licenses/mit-license.php MIT License
15+
*/
16+
namespace DebugKit;
17+
18+
use Cake\Core\Configure;
19+
20+
/**
21+
* Contains methods for dumping well formatted SQL queries.
22+
*/
23+
class DebugStatements
24+
{
25+
/**
26+
* @var array<string>
27+
*/
28+
protected static $statements = [];
29+
30+
/**
31+
* @return array<string>
32+
*/
33+
public static function all(): array
34+
{
35+
return static::$statements;
36+
}
37+
38+
/**
39+
* Template used for HTML output.
40+
*
41+
* @var string
42+
*/
43+
private static string $templateHtml = <<<HTML
44+
<div class="cake-debug-output">
45+
%s
46+
<pre class="cake-debug">
47+
%s
48+
</pre>
49+
</div>
50+
HTML;
51+
52+
/**
53+
* Template used for CLI and text output.
54+
*
55+
* @var string
56+
*/
57+
private static string $templateText = <<<TEXT
58+
%s
59+
########## DEBUG ##########
60+
%s
61+
###########################
62+
63+
TEXT;
64+
65+
/**
66+
* Prints out the SQL statements generated by a Query object.
67+
*
68+
* This function returns the same variable that was passed.
69+
* Only runs if debug mode is enabled.
70+
*
71+
* @param mixed $var
72+
* @param bool|null $showHtml
73+
* @param array $location
74+
* @return string Empty string.
75+
*/
76+
public static function add(mixed $var, ?bool $showHtml = null, array $location = []): string
77+
{
78+
if (!Configure::read('debug')) {
79+
return '';
80+
}
81+
82+
static::$statements[] = [
83+
'var' => $var,
84+
'showHtml' => $showHtml,
85+
'location' => $location,
86+
];
87+
88+
return '';
89+
}
90+
}

src/Panel/DumpPanel.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
6+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
7+
*
8+
* Licensed under The MIT License
9+
* Redistributions of files must retain the above copyright notice.
10+
*
11+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
12+
* @link https://cakephp.org CakePHP(tm) Project
13+
* @license https://www.opensource.org/licenses/mit-license.php MIT License
14+
*/
15+
namespace DebugKit\Panel;
16+
17+
use DebugKit\DebugPanel;
18+
use DebugKit\DebugStatements;
19+
20+
/**
21+
* A panel for displaying dumped statements.
22+
*/
23+
class DumpPanel extends DebugPanel
24+
{
25+
/**
26+
* Get the data for this panel
27+
*
28+
* @return array
29+
*/
30+
public function data(): array
31+
{
32+
return [
33+
'statements' => DebugStatements::all(),
34+
];
35+
}
36+
}

src/ToolbarService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class ToolbarService
5555
protected array $_defaultConfig = [
5656
'panels' => [
5757
'DebugKit.Cache' => true,
58+
'DebugKit.Dump' => true,
5859
'DebugKit.Request' => true,
5960
'DebugKit.SqlLog' => true,
6061
'DebugKit.Timer' => true,

templates/element/dump_panel.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Debug Panel Element
4+
*
5+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
6+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
7+
*
8+
* Licensed under The MIT License
9+
* Redistributions of files must retain the above copyright notice.
10+
*
11+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
12+
* @link https://cakephp.org CakePHP(tm) Project
13+
* @since DebugKit 5.1
14+
* @license https://www.opensource.org/licenses/mit-license.php MIT License
15+
*/
16+
17+
/**
18+
* @var \DebugKit\View\AjaxView $this
19+
* @var array $statements
20+
*/
21+
22+
?>
23+
<div class="c-dump-panel">
24+
<?php
25+
foreach ($statements as $statement) {
26+
echo '<pre class="cake-debug-string">' . h($statement['var']) . '</pre>';
27+
if (!empty($statement['location'])) {
28+
echo '<small>' . h($statement['location']['file']) . ':' . h($statement['location']['line']) . '</small>';
29+
}
30+
}
31+
32+
?>
33+
<h4></h4>
34+
35+
</div>

0 commit comments

Comments
 (0)