Skip to content

Commit

Permalink
Remove escaping issue of some chars on formatted query copy
Browse files Browse the repository at this point in the history
  • Loading branch information
ostrolucky committed Jan 5, 2020
1 parent d139c8e commit aa730eb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
9 changes: 4 additions & 5 deletions Resources/views/Collector/db.html.twig
Expand Up @@ -239,7 +239,7 @@
<td class="nowrap">{{ '%0.2f'|format(query.executionMS * 1000) }}&nbsp;ms</td>
{% endif %}
<td>
{{ query.sql|doctrine_pretty_query(highlight_only = true) }}
{{ query.sql|doctrine_prettify_sql }}

<div>
<strong class="font-normal text-small">Parameters</strong>: {{ profiler_dump(query.params, 2) }}
Expand All @@ -265,15 +265,14 @@
</div>

<div id="formatted-query-{{ i }}-{{ loop.parent.loop.index }}" class="sql-runnable hidden">
{% set formatted_sql = query.sql|doctrine_pretty_query %}
{{ formatted_sql|raw }}
<button class="btn btn-sm" data-clipboard-text="{{ formatted_sql|striptags|e('html_attr') }}">Copy</button>
{{ query.sql|doctrine_format_sql(highlight = true) }}
<button class="btn btn-sm" data-clipboard-text="{{ query.sql|doctrine_format_sql(highlight = false)|e('html_attr') }}">Copy</button>
</div>

{% if query.runnable %}
<div id="original-query-{{ i }}-{{ loop.parent.loop.index }}" class="sql-runnable hidden">
{% set runnable_sql = (query.sql ~ ';')|doctrine_replace_query_parameters(query.params) %}
{{ runnable_sql|doctrine_pretty_query(highlight_only = true) }}
{{ runnable_sql|doctrine_prettify_sql }}
<button class="btn btn-sm" data-clipboard-text="{{ runnable_sql|e('html_attr') }}">Copy</button>
</div>
{% endif %}
Expand Down
11 changes: 9 additions & 2 deletions Tests/ProfilerTest.php
Expand Up @@ -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,
Expand All @@ -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&#x20;&#x0A;&#x20;&#x20;&#x2A;&#x20;&#x0A;FROM&#x20;&#x0A;&#x20;&#x20;foo&#x20;&#x0A;WHERE&#x20;&#x0A;&#x20;&#x20;bar&#x20;IN&#x20;&#x28;&#x3F;,&#x20;&#x3F;&#x29;&#x20;&#x0A;&#x20;&#x20;AND&#x20;&quot;&quot;&#x20;&gt;&#x3D;&#x20;&quot;&quot;';
$this->assertContains($expectedEscapedSql, $output);
$this->assertSame(
"SELECT \n * \nFROM \n foo \nWHERE \n bar IN (?, ?) \n AND \"\" >= \"\"",
html_entity_decode($expectedEscapedSql)
);
}
}
45 changes: 34 additions & 11 deletions Twig/DoctrineExtension.php
Expand Up @@ -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']),
];
}
Expand Down Expand Up @@ -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);
Expand All @@ -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
*
Expand Down

0 comments on commit aa730eb

Please sign in to comment.