Skip to content

Commit

Permalink
Replace unmaintained dependency
Browse files Browse the repository at this point in the history
Closes #785
  • Loading branch information
greg0ire committed May 22, 2020
1 parent 65bb2eb commit b4d75bd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
18 changes: 18 additions & 0 deletions Tests/Twig/DoctrineExtensionTest.php
Expand Up @@ -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 📚;'), '<pre>')
);
}
}

class DummyClass
Expand Down
35 changes: 18 additions & 17 deletions Twig/DoctrineExtension.php
Expand Up @@ -2,7 +2,8 @@

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;
Expand Down Expand Up @@ -315,26 +316,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 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 $sqlFormatter->highlight($sql);
}

return $html;
return sprintf(
'<div class="highlight highlight-sql"><pre>%s</pre></div>',
$sqlFormatter->format($sql)
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -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",
Expand Down

0 comments on commit b4d75bd

Please sign in to comment.