Skip to content

Commit

Permalink
Merge pull request #477 from javiereguiluz/fix_469
Browse files Browse the repository at this point in the history
Refactored the doctrine_pretty_query filter to support "highlight only" mode
  • Loading branch information
guilhermeblanco committed Nov 4, 2015
2 parents 5202b51 + 8087a31 commit ba11cc4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Resources/views/Collector/db.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
<td>{{ loop.index }}</td>
<td class="nowrap">{{ '%0.2f'|format(query.executionMS * 1000) }}&nbsp;ms</td>
<td>
{{ query.sql|doctrine_highlight_sql }}
{{ query.sql|doctrine_pretty_query(highlight_only = true) }}

<div>
<strong class="font-normal text-small">Parameters</strong>: {{ query.params|yaml_encode }}
Expand All @@ -188,7 +188,7 @@
</div>

<div id="original-query-{{ i }}-{{ loop.parent.loop.index }}" class="sql-runnable hidden">
{{ (query.sql ~ ';')|doctrine_replace_query_parameters(query.params)|doctrine_highlight_sql }}
{{ (query.sql ~ ';')|doctrine_replace_query_parameters(query.params)|doctrine_pretty_query(highlight_only = true) }}
</div>

{% if query.explainable %}
Expand Down
18 changes: 12 additions & 6 deletions Twig/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ public function getFilters()
{
return array(
new \Twig_SimpleFilter('doctrine_minify_query', array($this, 'minifyQuery'), array('deprecated' => true)),
new \Twig_SimpleFilter('doctrine_pretty_query', 'SqlFormatter::format', array('is_safe' => array('html'))),
new \Twig_SimpleFilter('doctrine_pretty_query', array($this, 'formatQuery'), array('is_safe' => array('html'))),
new \Twig_SimpleFilter('doctrine_replace_query_parameters', array($this, 'replaceQueryParameters')),
new \Twig_SimpleFilter('doctrine_highlight_sql', array($this, 'highlightSqlSyntax'), array('is_safe' => array('html'))),
);
}

Expand Down Expand Up @@ -309,12 +308,14 @@ function ($matches) use ($parameters, &$i) {
}

/**
* Highlights the syntax of the given SQL statement.
* Formats and/or highlights the given SQL statement.
*
* @param string $sql
* @param bool $highlightOnly If true the query is not formatted, just highlighted
*
* @return string
*/
public function highlightSqlSyntax($sql)
public function formatQuery($sql, $highlightOnly = false)
{
\SqlFormatter::$pre_attributes = 'class="highlight highlight-sql"';
\SqlFormatter::$quote_attributes = 'class="string"';
Expand All @@ -327,8 +328,13 @@ public function highlightSqlSyntax($sql)
\SqlFormatter::$comment_attributes = 'class="comment"';
\SqlFormatter::$variable_attributes = 'class="variable"';

$html = \SqlFormatter::highlight($sql);
$html = preg_replace('/<pre class="(.*)">([^"]*+)<\/pre>/Us', '<div class="\1"><pre>\2</pre></div>', $html);
if ($highlightOnly) {
$html = \SqlFormatter::highlight($sql);
$html = preg_replace('/<pre class=".*">([^"]*+)<\/pre>/Us', '\1', $html);
} else {
$html = \SqlFormatter::format($sql);
$html = preg_replace('/<pre class="(.*)">([^"]*+)<\/pre>/Us', '<div class="\1"><pre>\2</pre></div>', $html);
}

return $html;
}
Expand Down

0 comments on commit ba11cc4

Please sign in to comment.