Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional pretty SQL syntax in toolbar #3515

Closed
wants to merge 1 commit into from
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
65 changes: 62 additions & 3 deletions system/Debug/Toolbar/Collectors/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,22 @@ public function display(): array
{
$sql = $query->getQuery();

foreach ($highlight as $term)
if( !class_exists('Highlight\Highlighter') )
{
$sql = str_replace($term, "<strong>{$term}</strong>", $sql);
foreach ($highlight as $term)
{
$sql = str_replace($term, "<strong>{$term}</strong>", $sql);
}
}

$data['queries'][] = [
'duration' => ($query->getDuration(5) * 1000) . ' ms',
'sql' => $sql,
'sql' => $this->_hljs($sql),
];
}

$data['hljsCss'] = $this->_hljsCss();

return $data;
}

Expand Down Expand Up @@ -275,4 +280,58 @@ public function icon(): string
return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAADMSURBVEhLY6A3YExLSwsA4nIycQDIDIhRWEBqamo/UNF/SjDQjF6ocZgAKPkRiFeEhoYyQ4WIBiA9QAuWAPEHqBAmgLqgHcolGQD1V4DMgHIxwbCxYD+QBqcKINseKo6eWrBioPrtQBq/BcgY5ht0cUIYbBg2AJKkRxCNWkDQgtFUNJwtABr+F6igE8olGQD114HMgHIxAVDyAhA/AlpSA8RYUwoeXAPVex5qHCbIyMgwBCkAuQJIY00huDBUz/mUlBQDqHGjgBjAwAAACexpph6oHSQAAAAASUVORK5CYII=';
}

//--------------------------------------------------------------------

/**
* Returns highlight.js style if Highlight.php exists.
*
* @return string
*/
protected function _hljsCss(): string
{
if( !class_exists("Highlight\Highlighter") ) return "";

$style = @file_get_contents(VENDORPATH . 'scrivo/highlight.php/styles/github.css') ?? '';
$darkStyle = @file_get_contents(VENDORPATH . 'scrivo/highlight.php/styles/dracula.css') ?? '';
$style .= str_replace('.hljs', '#toolbarContainer.dark .hljs', $darkStyle);

return <<<STYLE
<style>
.hljs-pre-line{white-space:pre-line;margin-bottom:4px}
{$style}
</style>
STYLE;
}

//--------------------------------------------------------------------

/**
* Returns pretty highlight SQL syntax if Highlight.php exists.
*
* @param string $code
*
* @return string
*/
protected function _hljs(string $code = ""): string
{
if( !class_exists('Highlight\Highlighter') ) return $code;
// Instantiate the Highlighter.
$hl = new \Highlight\Highlighter;
$out = '';

try {
// Highlight some code.
$highlighted = $hl->highlight('sql', $code);

$out = "<code class=\"hljs hljs-pre-line {$highlighted->language}\">";
$out .= $highlighted->value;
$out .= "</code>";
}
catch (\DomainException $e) {
$out .= "<code>{$code}</code>";
}

return $out;
}

}
1 change: 1 addition & 0 deletions system/Debug/Toolbar/Views/_database.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{! hljsCss !}
<table>
<thead>
<tr>
Expand Down