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
34 * @throws \JakubOnderka\PhpConsoleColor\InvalidStyleException
36 public function __construct(ConsoleColor $color)
38 $this->color = $color;
40 foreach ($this->defaultTheme as $name => $styles) {
41 if (!$this->color->hasTheme($name)) {
42 $this->color->addTheme($name, $styles);
48 * @param string $source
49 * @param int $lineNumber
50 * @param int $linesBefore
51 * @param int $linesAfter
53 * @throws \JakubOnderka\PhpConsoleColor\InvalidStyleException
54 * @throws \InvalidArgumentException
56 public function getCodeSnippet($source, $lineNumber, $linesBefore = 2, $linesAfter = 2)
58 $tokenLines = $this->getHighlightedLines($source);
60 $offset = $lineNumber - $linesBefore - 1;
61 $offset = max($offset, 0);
62 $length = $linesAfter + $linesBefore + 1;
63 $tokenLines = array_slice($tokenLines, $offset, $length, $preserveKeys = true);
65 $lines = $this->colorLines($tokenLines);
67 return $this->lineNumbers($lines, $lineNumber);
71 * @param string $source
73 * @throws \JakubOnderka\PhpConsoleColor\InvalidStyleException
74 * @throws \InvalidArgumentException
76 public function getWholeFile($source)
78 $tokenLines = $this->getHighlightedLines($source);
79 $lines = $this->colorLines($tokenLines);
80 return implode(PHP_EOL, $lines);
84 * @param string $source
86 * @throws \JakubOnderka\PhpConsoleColor\InvalidStyleException
87 * @throws \InvalidArgumentException
89 public function getWholeFileWithLineNumbers($source)
91 $tokenLines = $this->getHighlightedLines($source);
92 $lines = $this->colorLines($tokenLines);
93 return $this->lineNumbers($lines);
97 * @param string $source
100 private function getHighlightedLines($source)
102 $source = str_replace(array("\r\n", "\r"), "\n", $source);
103 $tokens = $this->tokenize($source);
104 return $this->splitToLines($tokens);
108 * @param string $source
111 private function tokenize($source)
113 $tokens = token_get_all($source);
119 foreach ($tokens as $token) {
120 if (is_array($token)) {
126 case T_OPEN_TAG_WITH_ECHO:
142 $newType = self::TOKEN_DEFAULT;
147 $newType = self::TOKEN_COMMENT;
150 case T_ENCAPSED_AND_WHITESPACE:
151 case T_CONSTANT_ENCAPSED_STRING:
152 $newType = self::TOKEN_STRING;
156 $newType = self::TOKEN_HTML;
160 $newType = self::TOKEN_KEYWORD;
163 $newType = $token === '"' ? self::TOKEN_STRING : self::TOKEN_KEYWORD;
166 if ($currentType === null) {
167 $currentType = $newType;
170 if ($currentType !== $newType) {
171 $output[] = array($currentType, $buffer);
173 $currentType = $newType;
176 $buffer .= is_array($token) ? $token[1] : $token;
179 if (isset($newType)) {
180 $output[] = array($newType, $buffer);
187 * @param array $tokens
190 private function splitToLines(array $tokens)
195 foreach ($tokens as $token) {
196 foreach (explode("\n", $token[1]) as $count => $tokenLine) {
202 if ($tokenLine === '') {
206 $line[] = array($token[0], $tokenLine);
216 * @param array $tokenLines
218 * @throws \JakubOnderka\PhpConsoleColor\InvalidStyleException
219 * @throws \InvalidArgumentException
221 private function colorLines(array $tokenLines)
224 foreach ($tokenLines as $lineCount => $tokenLine) {
226 foreach ($tokenLine as $token) {
227 list($tokenType, $tokenValue) = $token;
228 if ($this->color->hasTheme($tokenType)) {
229 $line .= $this->color->apply($tokenType, $tokenValue);
231 $line .= $tokenValue;
234 $lines[$lineCount] = $line;
241 * @param array $lines
242 * @param null|int $markLine
244 * @throws \JakubOnderka\PhpConsoleColor\InvalidStyleException
246 private function lineNumbers(array $lines, $markLine = null)
249 $lineStrlen = strlen(key($lines) + 1);
252 foreach ($lines as $i => $line) {
253 if ($markLine !== null) {
254 $snippet .= ($markLine === $i + 1 ? $this->color->apply(self::ACTUAL_LINE_MARK, ' > ') : ' ');
257 $snippet .= $this->color->apply(self::LINE_NUMBER, str_pad($i + 1, $lineStrlen, ' ', STR_PAD_LEFT) . '| ');
258 $snippet .= $line . PHP_EOL;