Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions config/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
Expand All @@ -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.
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
90 changes: 90 additions & 0 deletions src/DebugStatements.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
declare(strict_types=1);

/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since DebugKit 5.1
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace DebugKit;

use Cake\Core\Configure;

/**
* Contains methods for dumping well formatted SQL queries.
*/
class DebugStatements
{
/**
* @var array<string>
*/
protected static $statements = [];

Check failure on line 28 in src/DebugStatements.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Property \DebugKit\DebugStatements::$statements does not have native type hint for its value but it should be possible to add it based on @var annotation "array<string>".

/**
* @return array<string>
*/
public static function all(): array
{
return static::$statements;
}

/**
* Template used for HTML output.
*
* @var string
*/
private static string $templateHtml = <<<HTML

Check failure on line 43 in src/DebugStatements.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Static property DebugKit\DebugStatements::$templateHtml is never read, only written.
<div class="cake-debug-output">
%s
<pre class="cake-debug">
%s
</pre>
</div>
HTML;

/**
* Template used for CLI and text output.
*
* @var string
*/
private static string $templateText = <<<TEXT

Check failure on line 57 in src/DebugStatements.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Static property DebugKit\DebugStatements::$templateText is never read, only written.
%s
########## DEBUG ##########
%s
###########################

TEXT;

/**
* Prints out the SQL statements generated by a Query object.
*
* This function returns the same variable that was passed.
* Only runs if debug mode is enabled.
*
* @param mixed $var
* @param bool|null $showHtml
* @param array $location
* @return string Empty string.
*/
public static function add(mixed $var, ?bool $showHtml = null, array $location = []): string
{
if (!Configure::read('debug')) {
return '';
}

static::$statements[] = [

Check failure on line 82 in src/DebugStatements.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Static property DebugKit\DebugStatements::$statements (array<string>) does not accept array<array<string, mixed>|string>.
'var' => $var,
'showHtml' => $showHtml,
'location' => $location,
];

return '';
}
}
36 changes: 36 additions & 0 deletions src/Panel/DumpPanel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);

/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace DebugKit\Panel;

use DebugKit\DebugPanel;
use DebugKit\DebugStatements;

/**
* A panel for displaying dumped statements.
*/
class DumpPanel extends DebugPanel
{
/**
* Get the data for this panel
*
* @return array
*/
public function data(): array
{
return [
'statements' => DebugStatements::all(),
];
}
}
1 change: 1 addition & 0 deletions src/ToolbarService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
35 changes: 35 additions & 0 deletions templates/element/dump_panel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Debug Panel Element
*
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since DebugKit 5.1
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/

/**
* @var \DebugKit\View\AjaxView $this
* @var array $statements
*/

?>
<div class="c-dump-panel">
<?php
foreach ($statements as $statement) {
echo '<pre class="cake-debug-string">' . h($statement['var']) . '</pre>';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need to handle more complex values as well? If we're only going to support simple values, one could use logging instead.

if (!empty($statement['location'])) {
echo '<small>' . h($statement['location']['file']) . ':' . h($statement['location']['line']) . '</small>';
}
}

?>
<h4></h4>

</div>
Loading