From 7bdcb49807b6ea763fe510b3a90fac275abb1376 Mon Sep 17 00:00:00 2001 From: EnixApp Date: Wed, 19 Aug 2020 23:52:47 +0700 Subject: [PATCH] Optional pretty SQL syntax in toolbar --- system/Debug/Toolbar/Collectors/Database.php | 65 +++++++++++++++++++- system/Debug/Toolbar/Views/_database.tpl | 1 + 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/system/Debug/Toolbar/Collectors/Database.php b/system/Debug/Toolbar/Collectors/Database.php index 00aa7400eaf5..b5edb822f1a3 100644 --- a/system/Debug/Toolbar/Collectors/Database.php +++ b/system/Debug/Toolbar/Collectors/Database.php @@ -210,17 +210,22 @@ public function display(): array { $sql = $query->getQuery(); - foreach ($highlight as $term) + if( !class_exists('Highlight\Highlighter') ) { - $sql = str_replace($term, "{$term}", $sql); + foreach ($highlight as $term) + { + $sql = str_replace($term, "{$term}", $sql); + } } $data['queries'][] = [ 'duration' => ($query->getDuration(5) * 1000) . ' ms', - 'sql' => $sql, + 'sql' => $this->_hljs($sql), ]; } + $data['hljsCss'] = $this->_hljsCss(); + return $data; } @@ -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; + } + + //-------------------------------------------------------------------- + + /** + * 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 = "language}\">"; + $out .= $highlighted->value; + $out .= ""; + } + catch (\DomainException $e) { + $out .= "{$code}"; + } + + return $out; + } + } diff --git a/system/Debug/Toolbar/Views/_database.tpl b/system/Debug/Toolbar/Views/_database.tpl index b5cf1a43a7d7..edd87e73e6e4 100644 --- a/system/Debug/Toolbar/Views/_database.tpl +++ b/system/Debug/Toolbar/Views/_database.tpl @@ -1,3 +1,4 @@ +{! hljsCss !}