1111/**
1212 * Class Highlighter
1313 * @package Inhere\Console\Components\Style
14- *
1514 * @see jakub-onderka/php-console-highlighter
1615 * @link https://github.com/JakubOnderka/PHP-Console-Highlighter/blob/master/src/Highlighter.php
1716 */
@@ -45,6 +44,9 @@ class Highlighter
4544 self ::LINE_NUMBER => 'darkGray ' ,
4645 ];
4746
47+ /** @var bool */
48+ private $ hasTokenFunc ;
49+
4850 /**
4951 * @return Highlighter
5052 */
@@ -63,6 +65,7 @@ public static function create(): Highlighter
6365 public function __construct (Style $ color = null )
6466 {
6567 $ this ->color = $ color ?: Style::create ();
68+ $ this ->hasTokenFunc = \function_exists ('token_get_all ' );
6669 }
6770
6871 /**
@@ -98,6 +101,7 @@ public function highlightSnippet($source, $lineNumber, $linesBefore = 2, $linesA
98101 $ offset = max ($ offset , 0 );
99102 $ length = $ linesAfter + $ linesBefore + 1 ;
100103 $ tokenLines = \array_slice ($ tokenLines , $ offset , $ length , $ preserveKeys = true );
104+
101105 $ lines = $ this ->colorLines ($ tokenLines );
102106
103107 return $ this ->lineNumbers ($ lines , $ lineNumber );
@@ -135,10 +139,15 @@ public function getWholeFileWithLineNumbers($source): string
135139 */
136140 private function getHighlightedLines ($ source ): array
137141 {
138- $ source = str_replace (array ("\r\n" , "\r" ), "\n" , $ source );
139- $ tokens = $ this ->tokenize ($ source );
142+ $ source = str_replace (["\r\n" , "\r" ], "\n" , $ source );
143+
144+ if ($ this ->hasTokenFunc ) {
145+ $ tokens = $ this ->tokenize ($ source );
146+ return $ this ->splitToLines ($ tokens );
147+ }
140148
141- return $ this ->splitToLines ($ tokens );
149+ // if no func: token_get_all
150+ return explode ("\n" , $ source );
142151 }
143152
144153 /**
@@ -250,6 +259,10 @@ private function splitToLines(array $tokens): array
250259 */
251260 private function colorLines (array $ tokenLines ): array
252261 {
262+ if (!$ this ->hasTokenFunc ) {
263+ return $ tokenLines ;
264+ }
265+
253266 $ lines = [];
254267
255268 foreach ($ tokenLines as $ lineCount => $ tokenLine ) {
0 commit comments