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