1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class Highlighter | Highlighter.php |
4
|
|
|
* @package Faulancer\View\Helper |
5
|
|
|
* @author Florian Knapp <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
namespace Faulancer\View\Helper; |
8
|
|
|
|
9
|
|
|
use Faulancer\View\AbstractViewHelper; |
10
|
|
|
use Faulancer\View\ViewController; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Highlighter |
14
|
|
|
*/ |
15
|
|
|
class Highlighter extends AbstractViewHelper |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
private static $keys = [ |
19
|
|
|
'\bif', |
20
|
|
|
'else', |
21
|
|
|
'new', |
22
|
|
|
'throw', |
23
|
|
|
'echo', |
24
|
|
|
'return', |
25
|
|
|
'null', |
26
|
|
|
'public', |
27
|
|
|
'private', |
28
|
|
|
'protected', |
29
|
|
|
'function', |
30
|
|
|
'class[?!class\=|?!\$class]', |
31
|
|
|
'extends', |
32
|
|
|
'implements', |
33
|
|
|
'use', |
34
|
|
|
'namespace', |
35
|
|
|
'unset', |
36
|
|
|
'include_once', |
37
|
|
|
'require_once', |
38
|
|
|
'include', |
39
|
|
|
'require', |
40
|
|
|
'foreach', |
41
|
|
|
'for', |
42
|
|
|
'try', |
43
|
|
|
'catch', |
44
|
|
|
'\bas\b', |
45
|
|
|
'<\?php', |
46
|
|
|
'<\?=', |
47
|
|
|
'\?>\n' |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param ViewController $view |
52
|
|
|
* @param string $data |
53
|
|
|
* @param string $language |
54
|
|
|
* @return mixed|string |
55
|
|
|
* @codeCoverageIgnore |
56
|
|
|
*/ |
57
|
|
|
public function __invoke(ViewController $view, $data = '', $language = 'php') |
58
|
|
|
{ |
59
|
|
|
$data = htmlentities($data, ENT_IGNORE, null, false); |
60
|
|
|
$data = preg_replace('/(' . implode('\b|\b', self::$keys) . ')/', '<span class="base_keys">$1</span>', $data); |
61
|
|
|
$data = preg_replace('/\$(\w+)/', '<span class="variable">$$1</span>', $data); |
62
|
|
|
$data = preg_replace('/\'(.*)\'/', '<span class="string_text">\'$1\'</span>', $data); |
63
|
|
|
$data = preg_replace('/->(\w+)/', '-><span class="methods">$1</span>', $data); |
64
|
|
|
$data = preg_replace('/\/\*\*(.*)\*\//s', '<span class="annotation">/**$1*/</span>', $data); |
65
|
|
|
$data = preg_replace('/(_*)([A-Z]{2,})(_*)/', '<span class="constant">$1$2$3</span>', $data); |
66
|
|
|
|
67
|
|
|
return $data; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
} |