|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Spiral Framework. |
|
4
|
|
|
* |
|
5
|
|
|
* @license MIT |
|
6
|
|
|
* @author Anton Titov (Wolfy-J) |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace Spiral\Tokenizer; |
|
9
|
|
|
|
|
10
|
|
|
use Spiral\Core\Component; |
|
11
|
|
|
use Spiral\Tokenizer\Highlighter\Style; |
|
12
|
|
|
use Spiral\Tokenizer\Traits\TokensTrait; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Highlights php file using specified style. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Highlighter extends Component |
|
18
|
|
|
{ |
|
19
|
|
|
use TokensTrait; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @invisible |
|
23
|
|
|
* @var Style |
|
24
|
|
|
*/ |
|
25
|
|
|
private $style = null; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @invisible |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
private $tokens = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Highlighted source. |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
private $highlighted = ''; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $source |
|
42
|
|
|
* @param Style|null $style |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct($source, Style $style = null) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->style = !empty($style) ? $style : new Style(); |
|
47
|
|
|
$this->tokens = $this->normalizeTokens(token_get_all($source)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Set highlighter styler. |
|
52
|
|
|
* |
|
53
|
|
|
* @param Style $style |
|
54
|
|
|
* @return $this |
|
55
|
|
|
*/ |
|
56
|
|
|
public function setStyle(Style $style) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->style = $style; |
|
59
|
|
|
|
|
60
|
|
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get highlighted source. |
|
65
|
|
|
* |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
public function highlight() |
|
69
|
|
|
{ |
|
70
|
|
|
if (!empty($this->highlighted)) { |
|
71
|
|
|
//Nothing to do |
|
72
|
|
|
return $this->highlighted; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$this->highlighted = ''; |
|
76
|
|
|
foreach ($this->tokens as $tokenID => $token) { |
|
77
|
|
|
$this->highlighted .= $this->style->highlightToken( |
|
78
|
|
|
$token[TokenizerInterface::TYPE], |
|
79
|
|
|
htmlentities($token[TokenizerInterface::CODE]) |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $this->highlighted; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get only part of php file around specified line. |
|
88
|
|
|
* |
|
89
|
|
|
* @param int|null $line Set as null to avoid line highlighting. |
|
90
|
|
|
* @param int|null $around Set as null to return every line. |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
public function lines($line = null, $around = null) |
|
94
|
|
|
{ |
|
95
|
|
|
//Chinking by lines |
|
96
|
|
|
$lines = explode("\n", str_replace("\r\n", "\n", $this->highlight())); |
|
97
|
|
|
|
|
98
|
|
|
$result = ""; |
|
99
|
|
|
foreach ($lines as $number => $code) { |
|
100
|
|
|
$human = $number + 1; |
|
101
|
|
|
if ( |
|
102
|
|
|
!empty($around) |
|
103
|
|
|
&& ($human <= $line - $around || $human >= $line + $around) |
|
104
|
|
|
) { |
|
105
|
|
|
//Not included in a range |
|
106
|
|
|
continue; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$result .= $this->style->line( |
|
110
|
|
|
$human, |
|
111
|
|
|
mb_convert_encoding($code, 'utf-8'), |
|
112
|
|
|
$human === $line |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $result; |
|
117
|
|
|
} |
|
118
|
|
|
} |