2 namespace JakubOnderka\PhpConsoleHighlighter;
4 use JakubOnderka\PhpConsoleColor\ConsoleColor;
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';
14 const ACTUAL_LINE_MARK = 'actual_line_mark',
15 LINE_NUMBER = 'line_number';
17 /** @var ConsoleColor */
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',
28 self::ACTUAL_LINE_MARK => 'red',
29 self::LINE_NUMBER => 'dark_gray',
33 * @param ConsoleColor $color
35 public function __construct(ConsoleColor $color)
37 $this->color = $color;
39 foreach ($this->defaultTheme as $name => $styles) {
40 if (!$this->color->hasTheme($name)) {
41 $this->color->addTheme($name, $styles);
47 * @param string $source
48 * @param int $lineNumber
49 * @param int $linesBefore
50 * @param int $linesAfter
52 * @throws \JakubOnderka\PhpConsoleColor\InvalidStyleException
53 * @throws \InvalidArgumentException
55 public function getCodeSnippet($source, $lineNumber, $linesBefore = 2, $linesAfter = 2)
57 $tokenLines = $this->getHighlightedLines($source);
59 $offset = $lineNumber - $linesBefore - 1;
60 $offset = max($offset, 0);
61 $length = $linesAfter + $linesBefore + 1;
62 $tokenLines = array_slice($tokenLines, $offset, $length, $preserveKeys = true);
64 $lines = $this->colorLines($tokenLines);
66 return $this->lineNumbers($lines, $lineNumber);
70 * @param string $source
72 * @throws \JakubOnderka\PhpConsoleColor\InvalidStyleException
73 * @throws \InvalidArgumentException
75 public function getWholeFile($source)
77 $tokenLines = $this->getHighlightedLines($source);
78 $lines = $this->colorLines($tokenLines);
79 return implode(PHP_EOL, $lines);
83 * @param string $source
85 * @throws \JakubOnderka\PhpConsoleColor\InvalidStyleException
86 * @throws \InvalidArgumentException
88 public function getWholeFileWithLineNumbers($source)
90 $tokenLines = $this->getHighlightedLines($source);
91 $lines = $this->colorLines($tokenLines);
92 return $this->lineNumbers($lines);
96 * @param string $source
99 private function getHighlightedLines($source)
101 $source = str_replace(array("\r\n", "\r"), "\n", $source);
102 $tokens = $this->tokenize($source);
103 return $this->splitToLines($tokens);
107 * @param string $source
110 private function tokenize($source)
112 $tokens = token_get_all($source);
118 foreach ($tokens as $token) {
119 if (is_array($token)) {
122 $newType = self::TOKEN_HTML;
127 $newType = self::TOKEN_COMMENT;
130 case T_ENCAPSED_AND_WHITESPACE:
131 case T_CONSTANT_ENCAPSED_STRING:
132 $newType = self::TOKEN_STRING;
139 case T_OPEN_TAG_WITH_ECHO:
155 $newType = self::TOKEN_DEFAULT;
159 // Compatibility with PHP 5.3
160 if (defined('T_TRAIT_C') && $token[0] === T_TRAIT_C) {
161 $newType = self::TOKEN_DEFAULT;
163 $newType = self::TOKEN_KEYWORD;
167 $newType = $token === '"' ? self::TOKEN_STRING : self::TOKEN_KEYWORD;
170 if ($currentType === null) {
171 $currentType = $newType;
174 if ($currentType != $newType) {
175 $output[] = array($currentType, $buffer);
177 $currentType = $newType;
180 $buffer .= is_array($token) ? $token[1] : $token;
183 if (isset($newType)) {
184 $output[] = array($newType, $buffer);
191 * @param array $tokens
194 private function splitToLines(array $tokens)
199 foreach ($tokens as $token) {
200 foreach (explode("\n", $token[1]) as $count => $tokenLine) {
206 if ($tokenLine === '') {
210 $line[] = array($token[0], $tokenLine);
220 * @param array $tokenLines
222 * @throws \JakubOnderka\PhpConsoleColor\InvalidStyleException
223 * @throws \InvalidArgumentException
225 private function colorLines(array $tokenLines)
228 foreach ($tokenLines as $lineCount => $tokenLine) {
230 foreach ($tokenLine as $token) {
231 list($tokenType, $tokenValue) = $token;
232 if ($this->color->hasTheme($tokenType)) {
233 $line .= $this->color->apply($tokenType, $tokenValue);
235 $line .= $tokenValue;
238 $lines[$lineCount] = $line;
245 * @param array $lines
246 * @param null|int $markLine
248 * @throws \JakubOnderka\PhpConsoleColor\InvalidStyleException
250 private function lineNumbers(array $lines, $markLine = null)
253 $lineStrlen = strlen(key($lines) + 1);
256 foreach ($lines as $i => $line) {
257 if ($markLine !== null) {
258 $snippet .= ($markLine === $i + 1 ? $this->color->apply(self::ACTUAL_LINE_MARK, ' > ') : ' ');
261 $snippet .= $this->color->apply(self::LINE_NUMBER, str_pad($i + 1, $lineStrlen, ' ', STR_PAD_LEFT) . '| ');
262 $snippet .= $line . PHP_EOL;