Completed
Branch develop (c2aa4c)
by Anton
05:17
created

Highlighter::setStyle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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