diff --git a/Tests/Twig/DoctrineExtensionTest.php b/Tests/Twig/DoctrineExtensionTest.php index 47ddb6fcf..1c3069928 100644 --- a/Tests/Twig/DoctrineExtensionTest.php +++ b/Tests/Twig/DoctrineExtensionTest.php @@ -87,6 +87,24 @@ public function testEscapeBooleanParameter() { $this->assertEquals('1', DoctrineExtension::escapeFunction(true)); } + + public function testItFormatsSqlQueriesUsingCssClasses() : void + { + $extension = new DoctrineExtension(); + self::assertStringContainsString( + 'class=', + $extension->formatQuery('CREATE DATABASE 📚;') + ); + } + + public function testItDoesNotOutputDuplicatePreTags() : void + { + $extension = new DoctrineExtension(); + self::assertSame( + 1, + substr_count($extension->formatQuery('CREATE DATABASE 📚;'), '
') + ); + } } class DummyClass diff --git a/Twig/DoctrineExtension.php b/Twig/DoctrineExtension.php index 8051414b4..87cbc9f32 100644 --- a/Twig/DoctrineExtension.php +++ b/Twig/DoctrineExtension.php @@ -2,11 +2,14 @@ namespace Doctrine\Bundle\DoctrineBundle\Twig; -use SqlFormatter; +use Doctrine\SqlFormatter\HtmlHighlighter; +use Doctrine\SqlFormatter\SqlFormatter; use Symfony\Component\VarDumper\Cloner\Data; use Twig\Extension\AbstractExtension; use Twig\TwigFilter; + + /** * This class contains the needed functions in order to do the query highlighting */ @@ -315,26 +318,26 @@ static function ($matches) use ($parameters, &$i) { */ public function formatQuery($sql, $highlightOnly = false) { - SqlFormatter::$pre_attributes = 'class="highlight highlight-sql"'; - SqlFormatter::$quote_attributes = 'class="string"'; - SqlFormatter::$backtick_quote_attributes = 'class="string"'; - SqlFormatter::$reserved_attributes = 'class="keyword"'; - SqlFormatter::$boundary_attributes = 'class="symbol"'; - SqlFormatter::$number_attributes = 'class="number"'; - SqlFormatter::$word_attributes = 'class="word"'; - SqlFormatter::$error_attributes = 'class="error"'; - SqlFormatter::$comment_attributes = 'class="comment"'; - SqlFormatter::$variable_attributes = 'class="variable"'; + $sqlFormatter = new SqlFormatter(new HtmlHighlighter([ + HtmlHighlighter::HIGHLIGHT_QUOTE => 'class="string"', + HtmlHighlighter::HIGHLIGHT_BACKTICK_QUOTE => 'class="string"', + HtmlHighlighter::HIGHLIGHT_RESERVED => 'class="keyword"', + HtmlHighlighter::HIGHLIGHT_BOUNDARY => 'class="symbol"', + HtmlHighlighter::HIGHLIGHT_NUMBER => 'class="number"', + HtmlHighlighter::HIGHLIGHT_WORD => 'class="word"', + HtmlHighlighter::HIGHLIGHT_ERROR => 'class="error"', + HtmlHighlighter::HIGHLIGHT_COMMENT => 'class="comment"', + HtmlHighlighter::HIGHLIGHT_VARIABLE => 'class="variable"', + ], false)); if ($highlightOnly) { - $html = SqlFormatter::highlight($sql); - $html = preg_replace('/([^"]*+)<\/pre>/Us', '\1', $html); - } else { - $html = SqlFormatter::format($sql); - $html = preg_replace('/([^"]*+)<\/pre>/Us', '', $html); + return $sqlFormatter->highlight($sql); } - return $html; + return sprintf( + '\2', + $sqlFormatter->format($sql) + ); } /** diff --git a/composer.json b/composer.json index 370df8d80..8fde53dc5 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ "doctrine/dbal": "^2.5.12", "doctrine/doctrine-cache-bundle": "~1.2", "doctrine/persistence": "^1.3.3", - "jdorn/sql-formatter": "^1.2.16", + "doctrine/sql-formatter": "^1.0.1", "symfony/cache": "^3.4.30|^4.3.3", "symfony/config": "^3.4.30|^4.3.3", "symfony/service-contracts": "^1.1.1|^2.0",%s