Skip to content

Commit 226499b

Browse files
committed
bug fixed: token_get_all not exist for Highlighter
1 parent e9f2631 commit 226499b

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/Base/AbstractApplication.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ protected function runtimeCheck()
238238
*/
239239
public function handleException($e)
240240
{
241+
$class = \get_class($e);
241242
$type = $e instanceof \Error ? 'Error' : 'Exception';
242243
$title = ":( OO ... An $type Occurred!";
243244
$this->logError($e);
@@ -249,6 +250,7 @@ public function handleException($e)
249250
250251
Message <magenta>%s</magenta>
251252
At File <cyan>%s</cyan> line <cyan>%d</cyan>
253+
Exception $class
252254
Catch by %s()\n
253255
Code Trace:\n%s\n
254256
ERR;

src/Components/Style/Highlighter.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
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

Comments
 (0)