Completed
Push — master ( c4e3cd...c5cae6 )
by Anton
05:38
created

Style::highlightToken()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.439
cc 5
eloc 13
nc 5
nop 2
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
namespace Spiral\Tokenizer\Highlighter;
9
10
/**
11
 * Highlight code tokens. Attention, you have to specify container and container colors manually.
12
 */
13
class Style
14
{
15
    /**
16
     * Style templates.
17
     *
18
     * @var array
19
     */
20
    protected $templates = [
21
        'token'       => "<span style=\"{style}\">{code}</span>",
22
        'line'        => "<div><span class=\"number\">{number}</span>{code}</div>\n",
23
        'highlighted' => "<div class=\"highlighted\"><span class=\"number\">{number}</span>{code}</div>\n"
24
    ];
25
26
    /**
27
     * Styles associated with token types.
28
     *
29
     * @var array
30
     */
31
    protected $styles = [
32
        'color: blue; font-weight: bold;'   => [
33
            T_STATIC,
34
            T_PUBLIC,
35
            T_PRIVATE,
36
            T_PROTECTED,
37
            T_CLASS,
38
            T_NEW,
39
            T_FINAL,
40
            T_ABSTRACT,
41
            T_IMPLEMENTS,
42
            T_CONST,
43
            T_ECHO,
44
            T_CASE,
45
            T_FUNCTION,
46
            T_GOTO,
47
            T_INCLUDE,
48
            T_INCLUDE_ONCE,
49
            T_REQUIRE,
50
            T_REQUIRE_ONCE,
51
            T_VAR,
52
            T_INSTANCEOF,
53
            T_INTERFACE,
54
            T_THROW,
55
            T_ARRAY,
56
            T_IF,
57
            T_ELSE,
58
            T_ELSEIF,
59
            T_TRY,
60
            T_CATCH,
61
            T_CLONE,
62
            T_WHILE,
63
            T_FOR,
64
            T_DO,
65
            T_UNSET,
66
            T_FOREACH,
67
            T_RETURN,
68
            T_EXIT
69
        ],
70
        'color: blue'                       => [
71
            T_DNUMBER,
72
            T_LNUMBER
73
        ],
74
        'color: black; font: weight: bold;' => [
75
            T_OPEN_TAG,
76
            T_CLOSE_TAG,
77
            T_OPEN_TAG_WITH_ECHO
78
        ],
79
        'color: gray;'                      => [
80
            T_COMMENT,
81
            T_DOC_COMMENT
82
        ],
83
        'color: green; font-weight: bold;'  => [
84
            T_CONSTANT_ENCAPSED_STRING,
85
            T_ENCAPSED_AND_WHITESPACE
86
        ],
87
        'color: #660000;'                   => [
88
            T_VARIABLE
89
        ]
90
    ];
91
92
    /**
93
     * Highlight given token.
94
     *
95
     * @param int    $tokenType
96
     * @param string $code
97
     * @return string
98
     */
99
    public function highlightToken($tokenType, $code)
100
    {
101
        foreach ($this->styles as $style => $tokens) {
102
            if (!in_array($tokenType, $tokens)) {
103
                //Nothing to highlight
104
                continue;
105
            }
106
107
            if (strpos($code, "\n") === false) {
108
                return \Spiral\interpolate($this->templates['token'], compact('style', 'code'));
109
            }
110
111
            $lines = [];
112
            foreach (explode("\n", $code) as $line) {
113
                $lines[] = \Spiral\interpolate($this->templates['token'], [
114
                    'style' => $style,
115
                    'code'  => $line
116
                ]);
117
            }
118
119
            return join("\n", $lines);
120
        }
121
122
        return $code;
123
    }
124
125
    /**
126
     * Highlight one line.
127
     *
128
     * @param int    $number
129
     * @param string $code
130
     * @param bool   $highlighted
131
     * @return string
132
     */
133
    public function line($number, $code, $highlighted = false)
134
    {
135
        return \Spiral\interpolate(
136
            $this->templates[$highlighted ? 'highlighted' : 'line'],
137
            compact('number', 'code')
138
        );
139
    }
140
}