Skip to content

Commit 225e898

Browse files
committed
Use own theme handling instead Colors\Color
1 parent 575f8fc commit 225e898

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

example.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@
22
use Colors\Color;
33
use JakubOnderka\PhpConsoleHighlighter\Highlighter;
44

5-
$colors = new Color();
6-
$colors->setTheme(array(
7-
Highlighter::TOKEN_STRING => 'red',
8-
Highlighter::TOKEN_COMMENT => 'yellow',
9-
Highlighter::TOKEN_KEYWORD => 'green',
10-
Highlighter::TOKEN_DEFAULT => 'white',
11-
Highlighter::TOKEN_HTML => 'cyan'
12-
));
13-
$highlighter = new Highlighter($colors);
5+
require __DIR__ . '/vendor/autoload.php';
6+
7+
$highlighter = new Highlighter(new Color());
148

159
$fileContent = file_get_contents(__DIR__ . '/example.php');
1610
echo $highlighter->getWholeFile($fileContent);

src/JakubOnderka/PhpConsoleHighlighter/Highlighter.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,26 @@ class Highlighter
1414
/** @var Color */
1515
private $color;
1616

17-
public function __construct(Color $color)
17+
/** @var array */
18+
private $theme = array(
19+
self::TOKEN_STRING => 'red',
20+
self::TOKEN_COMMENT => 'yellow',
21+
self::TOKEN_KEYWORD => 'green',
22+
self::TOKEN_DEFAULT => 'white',
23+
self::TOKEN_HTML => 'cyan'
24+
);
25+
26+
/**
27+
* @param Color $color
28+
* @param array|null $theme
29+
*/
30+
public function __construct(Color $color, array $theme = null)
1831
{
1932
$this->color = $color;
33+
34+
if ($theme) {
35+
$this->setTheme($theme);
36+
}
2037
}
2138

2239
/**
@@ -58,6 +75,22 @@ public function getWholeFile($source)
5875
return $this->output($tokenLines);
5976
}
6077

78+
/**
79+
* @return array
80+
*/
81+
public function getTheme()
82+
{
83+
return $this->theme;
84+
}
85+
86+
/**
87+
* @param array $theme
88+
*/
89+
public function setTheme(array $theme)
90+
{
91+
$this->theme = $theme;
92+
}
93+
6194
/**
6295
* @param string $source
6396
* @return array
@@ -175,7 +208,12 @@ private function colorLines(array $tokenLines)
175208
foreach ($tokenLines as $lineCount => $tokenLine) {
176209
$line = '';
177210
foreach ($tokenLine as $token) {
178-
$line .= $this->color->apply($token[0], $token[1]);
211+
list($tokenType, $tokenValue) = $token;
212+
if (isset($this->theme[$tokenType])) {
213+
$line .= $this->color->apply($this->theme[$tokenType], $tokenValue);
214+
} else {
215+
$line .= $tokenValue;
216+
}
179217
}
180218
$lines[$lineCount] = $line;
181219
}

0 commit comments

Comments
 (0)