From aa730eb8a4583d5d6576f06d73813ec8aaf76e4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Ostroluck=C3=BD?= Date: Sun, 5 Jan 2020 15:36:31 +0100 Subject: [PATCH] Remove escaping issue of some chars on formatted query copy --- Resources/views/Collector/db.html.twig | 9 +++--- Tests/ProfilerTest.php | 11 +++++-- Twig/DoctrineExtension.php | 45 +++++++++++++++++++------- 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/Resources/views/Collector/db.html.twig b/Resources/views/Collector/db.html.twig index 75f7c314c..170c9088d 100644 --- a/Resources/views/Collector/db.html.twig +++ b/Resources/views/Collector/db.html.twig @@ -239,7 +239,7 @@ {{ '%0.2f'|format(query.executionMS * 1000) }} ms {% endif %} - {{ query.sql|doctrine_pretty_query(highlight_only = true) }} + {{ query.sql|doctrine_prettify_sql }}
Parameters: {{ profiler_dump(query.params, 2) }} @@ -265,15 +265,14 @@
{% if query.runnable %} {% endif %} diff --git a/Tests/ProfilerTest.php b/Tests/ProfilerTest.php index e7245fea9..3fb80188a 100644 --- a/Tests/ProfilerTest.php +++ b/Tests/ProfilerTest.php @@ -67,7 +67,7 @@ public function testRender() { $this->logger->queries = [ [ - 'sql' => 'SELECT * FROM foo WHERE bar IN (?, ?)', + 'sql' => 'SELECT * FROM foo WHERE bar IN (?, ?) AND "" >= ""', 'params' => ['foo', 'bar'], 'types' => null, 'executionMS' => 1, @@ -88,6 +88,13 @@ public function testRender() ]); $output = str_replace(["\e[37m", "\e[0m", "\e[32;1m", "\e[34;1m"], '', $output); - $this->assertContains("SELECT * FROM foo WHERE bar IN ('foo', 'bar');", $output); + $this->assertContains("SELECT * FROM foo WHERE bar IN ('foo', 'bar') AND \"\" >= \"\";", $output); + + $expectedEscapedSql = 'SELECT * FROM foo WHERE bar IN (?, ?) AND "" >= ""'; + $this->assertContains($expectedEscapedSql, $output); + $this->assertSame( + "SELECT \n * \nFROM \n foo \nWHERE \n bar IN (?, ?) \n AND \"\" >= \"\"", + html_entity_decode($expectedEscapedSql) + ); } } diff --git a/Twig/DoctrineExtension.php b/Twig/DoctrineExtension.php index b06ac94c9..e38280e56 100644 --- a/Twig/DoctrineExtension.php +++ b/Twig/DoctrineExtension.php @@ -20,7 +20,9 @@ class DoctrineExtension extends AbstractExtension public function getFilters() { return [ - new TwigFilter('doctrine_pretty_query', [$this, 'formatQuery'], ['is_safe' => ['html']]), + new TwigFilter('doctrine_pretty_query', [$this, 'formatQuery'], ['is_safe' => ['html'], 'deprecated' => true]), + new TwigFilter('doctrine_prettify_sql', [$this, 'prettifySql'], ['is_safe' => ['html']]), + new TwigFilter('doctrine_format_sql', [$this, 'formatSql'], ['is_safe' => ['html']]), new TwigFilter('doctrine_replace_query_parameters', [$this, 'replaceQueryParameters']), ]; } @@ -169,16 +171,9 @@ 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"'; + @trigger_error(sprintf('The "%s()" method is deprecated and will be removed in DoctrineBundle 3.0.', __METHOD__), E_USER_DEPRECATED); + + $this->setUpSqlFormatter(); if ($highlightOnly) { $html = SqlFormatter::highlight($sql); @@ -191,6 +186,34 @@ public function formatQuery($sql, $highlightOnly = false) return $html; } + public function prettifySql(string $sql) : string + { + $this->setUpSqlFormatter(); + + return SqlFormatter::highlight($sql); + } + + public function formatSql(string $sql, bool $highlight) : string + { + $this->setUpSqlFormatter(); + + return SqlFormatter::format($sql, $highlight); + } + + private function setUpSqlFormatter() : void + { + 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"'; + } + /** * Get the name of the extension *