Skip to content

Commit 7dea318

Browse files
authored
Merge pull request #21 from greg0ire/use-polymorphism
Use polymorphism for highlighting
2 parents 4db282d + a89bbdd commit 7dea318

File tree

9 files changed

+294
-352
lines changed

9 files changed

+294
-352
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SqlFormatter
22

3-
A lightweight php class for formatting sql statements.
3+
A lightweight php package for formatting sql statements.
44

55
It can automatically indent and add line breaks in addition to syntax
66
highlighting.

composer.lock

Lines changed: 25 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/examples.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
require_once '../vendor/autoload.php';
5+
require_once __DIR__ . '/../vendor/autoload.php';
66

77
use Doctrine\SqlFormatter\SqlFormatter;
88

src/CliHighlighter.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\SqlFormatter;
6+
7+
final class CliHighlighter implements Highlighter
8+
{
9+
/** @var string */
10+
public $cliWord = '';
11+
12+
/** @var string */
13+
public $cliQuote = "\x1b[34;1m";
14+
15+
/** @var string */
16+
public $cliBacktickQuote = "\x1b[35;1m";
17+
18+
/** @var string */
19+
public $cliReserved = "\x1b[37m";
20+
21+
/** @var string */
22+
public $cliBoundary = '';
23+
24+
/** @var string */
25+
public $cliNumber = "\x1b[32;1m";
26+
27+
/** @var string */
28+
public $cliError = "\x1b[31;1;7m";
29+
30+
/** @var string */
31+
public $cliComment = "\x1b[30;1m";
32+
33+
/** @var string */
34+
public $cliFunctions = "\x1b[37m";
35+
36+
/** @var string */
37+
public $cliVariable = "\x1b[36;1m";
38+
39+
public function highlightToken(int $type, string $value) : string
40+
{
41+
if ($type === SqlFormatter::TOKEN_TYPE_BOUNDARY && ($value==='(' || $value===')')) {
42+
return $value;
43+
}
44+
45+
$prefix = $this->prefix($type);
46+
if ($prefix === null) {
47+
return $value;
48+
}
49+
50+
return $prefix . $value . "\x1b[0m";
51+
}
52+
53+
private function prefix(int $type) : ?string
54+
{
55+
switch ($type) {
56+
case SqlFormatter::TOKEN_TYPE_BOUNDARY:
57+
return $this->cliBoundary;
58+
case SqlFormatter::TOKEN_TYPE_WORD:
59+
return $this->cliWord;
60+
case SqlFormatter::TOKEN_TYPE_BACKTICK_QUOTE:
61+
return $this->cliBacktickQuote;
62+
case SqlFormatter::TOKEN_TYPE_QUOTE:
63+
return $this->cliQuote;
64+
case SqlFormatter::TOKEN_TYPE_RESERVED:
65+
case SqlFormatter::TOKEN_TYPE_RESERVED_TOPLEVEL:
66+
case SqlFormatter::TOKEN_TYPE_RESERVED_NEWLINE:
67+
return $this->cliReserved;
68+
case SqlFormatter::TOKEN_TYPE_NUMBER:
69+
return $this->cliNumber;
70+
case SqlFormatter::TOKEN_TYPE_VARIABLE:
71+
return $this->cliVariable;
72+
case SqlFormatter::TOKEN_TYPE_COMMENT:
73+
case SqlFormatter::TOKEN_TYPE_BLOCK_COMMENT:
74+
return $this->cliComment;
75+
default:
76+
return null;
77+
}
78+
}
79+
80+
public function highlightError(string $value) : string
81+
{
82+
return $this->cliError . $value . "\x1b[0m";
83+
}
84+
85+
public function output(string $string) : string
86+
{
87+
return $string . "\n";
88+
}
89+
}

src/Highlighter.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\SqlFormatter;
6+
7+
interface Highlighter
8+
{
9+
/**
10+
* Highlights a token depending on its type.
11+
*/
12+
public function highlightToken(int $type, string $value) : string;
13+
14+
public function highlightError(string $value) : string;
15+
16+
/**
17+
* Helper function for building string output
18+
*
19+
* @param string $string The string to be quoted
20+
*
21+
* @return string The quoted string
22+
*/
23+
public function output(string $string) : string;
24+
}

src/HtmlHighlighter.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\SqlFormatter;
6+
7+
use function htmlentities;
8+
use function trim;
9+
use const ENT_COMPAT;
10+
use const ENT_IGNORE;
11+
12+
final class HtmlHighlighter implements Highlighter
13+
{
14+
// Styles applied to different token types
15+
/** @var string */
16+
public $quoteAttributes = 'style="color: blue;"';
17+
18+
/** @var string */
19+
public $backtickQuoteAttributes = 'style="color: purple;"';
20+
21+
/** @var string */
22+
public $reservedAttributes = 'style="font-weight:bold;"';
23+
24+
/** @var string */
25+
public $boundaryAttributes = '';
26+
27+
/** @var string */
28+
public $numberAttributes = 'style="color: green;"';
29+
30+
/** @var string */
31+
public $wordAttributes = 'style="color: #333;"';
32+
33+
/** @var string */
34+
public $errorAttributes = 'style="background-color: red;"';
35+
36+
/** @var string */
37+
public $commentAttributes = 'style="color: #aaa;"';
38+
39+
/** @var string */
40+
public $variableAttributes = 'style="color: orange;"';
41+
42+
/** @var string */
43+
public $preAttributes = 'style="color: black; background-color: white;"';
44+
45+
/**
46+
* This flag tells us if queries need to be enclosed in <pre> tags
47+
*
48+
* @var bool
49+
*/
50+
public $usePre = true;
51+
52+
public function highlightToken(int $type, string $value) : string
53+
{
54+
$value = htmlentities($value, ENT_COMPAT | ENT_IGNORE, 'UTF-8');
55+
56+
if ($type === SqlFormatter::TOKEN_TYPE_BOUNDARY && ($value==='(' || $value===')')) {
57+
return $value;
58+
}
59+
60+
$attributes = $this->attributes($type);
61+
if ($attributes === null) {
62+
return $value;
63+
}
64+
65+
return '<span ' . $this->attributes($type) . '>' . $value . '</span>';
66+
}
67+
68+
public function attributes(int $type) : ?string
69+
{
70+
switch ($type) {
71+
case SqlFormatter::TOKEN_TYPE_BOUNDARY:
72+
return $this->boundaryAttributes;
73+
case SqlFormatter::TOKEN_TYPE_WORD:
74+
return $this->wordAttributes;
75+
case SqlFormatter::TOKEN_TYPE_BACKTICK_QUOTE:
76+
return $this->backtickQuoteAttributes;
77+
case SqlFormatter::TOKEN_TYPE_QUOTE:
78+
return $this->quoteAttributes;
79+
case SqlFormatter::TOKEN_TYPE_RESERVED:
80+
case SqlFormatter::TOKEN_TYPE_RESERVED_TOPLEVEL:
81+
case SqlFormatter::TOKEN_TYPE_RESERVED_NEWLINE:
82+
return $this->reservedAttributes;
83+
case SqlFormatter::TOKEN_TYPE_NUMBER:
84+
return $this->numberAttributes;
85+
case SqlFormatter::TOKEN_TYPE_VARIABLE:
86+
return $this->variableAttributes;
87+
case SqlFormatter::TOKEN_TYPE_COMMENT:
88+
case SqlFormatter::TOKEN_TYPE_BLOCK_COMMENT:
89+
return $this->commentAttributes;
90+
default:
91+
return null;
92+
}
93+
}
94+
95+
public function highlightError(string $value) : string
96+
{
97+
return '<span ' . $this->errorAttributes . '>' . $value . '</span>';
98+
}
99+
100+
public function output(string $string) : string
101+
{
102+
$string =trim($string);
103+
if (! $this->usePre) {
104+
return $string;
105+
}
106+
107+
return '<pre ' . $this->preAttributes . '>' . $string . '</pre>';
108+
}
109+
}

0 commit comments

Comments
 (0)