Highlighter.php
7.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/**
* This file is part of Collision.
*
* (c) Nuno Maduro <enunomaduro@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NunoMaduro\Collision;
use NunoMaduro\Collision\Contracts\Highlighter as HighlighterContract;
/**
* This is an Collision Highlighter implementation.
*
* Code originally from { JakubOnderka\\PhpConsoleColor }. But the package got deprecated.
*
* @internal
*
* @final
*/
class Highlighter implements HighlighterContract
{
/**
* Holds the theme.
*
* @var array
*/
private $theme = [
self::TOKEN_STRING => ['light_gray'],
self::TOKEN_COMMENT => ['dark_gray', 'italic'],
self::TOKEN_KEYWORD => ['magenta', 'bold'],
self::TOKEN_DEFAULT => ['default', 'bold'],
self::TOKEN_HTML => ['blue', 'bold'],
self::ACTUAL_LINE_MARK => ['red', 'bold'],
self::LINE_NUMBER => ['dark_gray'],
];
const TOKEN_DEFAULT = 'token_default';
const TOKEN_COMMENT = 'token_comment';
const TOKEN_STRING = 'token_string';
const TOKEN_HTML = 'token_html';
const TOKEN_KEYWORD = 'token_keyword';
const ACTUAL_LINE_MARK = 'actual_line_mark';
const LINE_NUMBER = 'line_number';
/** @var ConsoleColor */
private $color;
/** @var array */
private $defaultTheme = [
self::TOKEN_STRING => 'red',
self::TOKEN_COMMENT => 'yellow',
self::TOKEN_KEYWORD => 'green',
self::TOKEN_DEFAULT => 'default',
self::TOKEN_HTML => 'cyan',
self::ACTUAL_LINE_MARK => 'red',
self::LINE_NUMBER => 'dark_gray',
];
/**
* Creates an instance of the Highlighter.
*/
public function __construct(ConsoleColor $color = null)
{
$this->color = $color ?: new ConsoleColor();
foreach ($this->defaultTheme as $name => $styles) {
if (!$this->color->hasTheme($name)) {
$this->color->addTheme($name, $styles);
}
}
foreach ($this->theme as $name => $styles) {
$this->color->addTheme((string) $name, $styles);
}
}
/**
* {@inheritdoc}
*/
public function highlight(string $content, int $line): string
{
return rtrim($this->getCodeSnippet($content, $line, 4, 4));
}
/**
* @param string $source
* @param int $lineNumber
* @param int $linesBefore
* @param int $linesAfter
*
* @return string
*/
public function getCodeSnippet($source, $lineNumber, $linesBefore = 2, $linesAfter = 2)
{
$tokenLines = $this->getHighlightedLines($source);
$offset = $lineNumber - $linesBefore - 1;
$offset = max($offset, 0);
$length = $linesAfter + $linesBefore + 1;
$tokenLines = array_slice($tokenLines, $offset, $length, $preserveKeys = true);
$lines = $this->colorLines($tokenLines);
return $this->lineNumbers($lines, $lineNumber);
}
/**
* @param string $source
*
* @return array
*/
private function getHighlightedLines($source)
{
$source = str_replace(["\r\n", "\r"], "\n", $source);
$tokens = $this->tokenize($source);
return $this->splitToLines($tokens);
}
/**
* @param string $source
*
* @return array
*/
private function tokenize($source)
{
$tokens = token_get_all($source);
$output = [];
$currentType = null;
$buffer = '';
foreach ($tokens as $token) {
if (is_array($token)) {
switch ($token[0]) {
case T_WHITESPACE:
break;
case T_OPEN_TAG:
case T_OPEN_TAG_WITH_ECHO:
case T_CLOSE_TAG:
case T_STRING:
case T_VARIABLE:
// Constants
case T_DIR:
case T_FILE:
case T_METHOD_C:
case T_DNUMBER:
case T_LNUMBER:
case T_NS_C:
case T_LINE:
case T_CLASS_C:
case T_FUNC_C:
case T_TRAIT_C:
$newType = self::TOKEN_DEFAULT;
break;
case T_COMMENT:
case T_DOC_COMMENT:
$newType = self::TOKEN_COMMENT;
break;
case T_ENCAPSED_AND_WHITESPACE:
case T_CONSTANT_ENCAPSED_STRING:
$newType = self::TOKEN_STRING;
break;
case T_INLINE_HTML:
$newType = self::TOKEN_HTML;
break;
default:
$newType = self::TOKEN_KEYWORD;
}
} else {
$newType = $token === '"' ? self::TOKEN_STRING : self::TOKEN_KEYWORD;
}
if ($currentType === null) {
$currentType = $newType;
}
if ($currentType !== $newType) {
$output[] = [$currentType, $buffer];
$buffer = '';
$currentType = $newType;
}
$buffer .= is_array($token) ? $token[1] : $token;
}
if (isset($newType)) {
$output[] = [$newType, $buffer];
}
return $output;
}
/**
* @return array
*/
private function splitToLines(array $tokens)
{
$lines = [];
$line = [];
foreach ($tokens as $token) {
foreach (explode("\n", $token[1]) as $count => $tokenLine) {
if ($count > 0) {
$lines[] = $line;
$line = [];
}
if ($tokenLine === '') {
continue;
}
$line[] = [$token[0], $tokenLine];
}
}
$lines[] = $line;
return $lines;
}
/**
* @return array
*/
private function colorLines(array $tokenLines)
{
$lines = [];
foreach ($tokenLines as $lineCount => $tokenLine) {
$line = '';
foreach ($tokenLine as $token) {
[$tokenType, $tokenValue] = $token;
if ($this->color->hasTheme($tokenType)) {
$line .= $this->color->apply($tokenType, $tokenValue);
} else {
$line .= $tokenValue;
}
}
$lines[$lineCount] = $line;
}
return $lines;
}
/**
* @param int|null $markLine
*
* @return string
*/
private function lineNumbers(array $lines, $markLine = null)
{
end($lines);
$lineStrlen = strlen(key($lines) + 1);
$snippet = '';
foreach ($lines as $i => $line) {
if ($markLine !== null) {
$snippet .= ($markLine === $i + 1 ? $this->color->apply(self::ACTUAL_LINE_MARK, ' > ') : ' ');
}
$snippet .= $this->color->apply(self::LINE_NUMBER, str_pad($i + 1, $lineStrlen, ' ', STR_PAD_LEFT) . '| ');
$snippet .= $line . PHP_EOL;
}
return $snippet;
}
}