1 | <?php |
||
11 | class CompilerBuffer { |
||
12 | const STYLE_JOIN = 'join'; |
||
13 | const STYLE_ARRAY = 'array'; |
||
14 | |||
15 | /** |
||
16 | * @var ComponentBuffer[] |
||
17 | */ |
||
18 | private $buffers; |
||
19 | |||
20 | /** |
||
21 | * @var ComponentBuffer |
||
22 | */ |
||
23 | private $current; |
||
24 | |||
25 | private $currentName; |
||
26 | |||
27 | private $basename; |
||
28 | |||
29 | private $style = self::STYLE_JOIN; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | private $defaults; |
||
35 | |||
36 | /** |
||
37 | * @var \SplObjectStorage |
||
38 | */ |
||
39 | private $nodeProps; |
||
40 | |||
41 | 38 | public function __construct($style = self::STYLE_JOIN, array $defaults = []) { |
|
52 | |||
53 | /** |
||
54 | * Select a specific component buffer. |
||
55 | * @param $component |
||
56 | */ |
||
57 | 38 | public function select($component) { |
|
58 | 38 | $previous = $this->currentName; |
|
59 | 38 | $this->currentName = $component; |
|
60 | |||
61 | 38 | if (!array_key_exists($component, $this->buffers)) { |
|
62 | 38 | $this->buffers[$component] = $buffer = new ComponentBuffer($this->defaults); |
|
63 | 38 | } |
|
64 | |||
65 | 38 | $this->current =& $this->buffers[$component]; |
|
66 | |||
67 | 38 | return $previous; |
|
68 | } |
||
69 | |||
70 | 38 | public function echoLiteral($value) { |
|
73 | |||
74 | 26 | public function echoCode($php) { |
|
77 | |||
78 | 38 | public function appendCode($php) { |
|
81 | |||
82 | 38 | public function indent($add) { |
|
85 | |||
86 | 13 | public function depth($add = 1) { |
|
89 | |||
90 | 13 | public function depthName($name, $add = 0) { |
|
93 | |||
94 | 38 | public function pushScope(array $vars) { |
|
97 | |||
98 | 38 | public function popScope() { |
|
101 | |||
102 | 34 | public function getScopeVariables() { |
|
105 | |||
106 | 38 | public function flush() { |
|
107 | 38 | switch ($this->getStyle()) { |
|
108 | 38 | case self::STYLE_ARRAY: |
|
109 | 9 | return $this->flushArray(); |
|
110 | 38 | default: |
|
111 | 38 | return $this->flushJoin(); |
|
112 | 38 | } |
|
113 | } |
||
114 | |||
115 | private function flushJoin() { |
||
121 | |||
122 | 9 | private function flushArray() { |
|
123 | 9 | $result = []; |
|
124 | |||
125 | 9 | foreach ($this->buffers as $name => $buffer) { |
|
126 | 9 | $flushed = $buffer->flush(); |
|
127 | 9 | if (empty($flushed)) { |
|
128 | 7 | continue; |
|
129 | } |
||
130 | |||
131 | 2 | if ($name === '') { |
|
132 | 2 | $result[] = $flushed; |
|
133 | 2 | } else { |
|
134 | 1 | $result[] = var_export($name, true).' => '.$flushed; |
|
135 | } |
||
136 | 9 | } |
|
137 | |||
138 | 9 | if (empty($result)) { |
|
139 | 7 | return '[]'; |
|
140 | } else { |
||
141 | 2 | return "[\n".implode(",\n\n", $result)."\n".$this->px().']'; |
|
142 | } |
||
143 | } |
||
144 | |||
145 | 2 | protected function px($add = 0) { |
|
148 | |||
149 | /** |
||
150 | * Get the style. |
||
151 | * |
||
152 | * @return string Returns the style. |
||
153 | */ |
||
154 | 38 | public function getStyle() { |
|
157 | |||
158 | /** |
||
159 | * Set the style. |
||
160 | * |
||
161 | * @param string $style One of the **STYLE_*** constants. |
||
162 | * @return $this |
||
163 | */ |
||
164 | public function setStyle($style) { |
||
168 | |||
169 | /** |
||
170 | * Get the basename. |
||
171 | * |
||
172 | * @return string Returns the basename. |
||
173 | */ |
||
174 | public function getBasename() { |
||
177 | |||
178 | /** |
||
179 | * Set the basename. |
||
180 | * |
||
181 | * @param string $basename |
||
182 | * @return $this |
||
183 | */ |
||
184 | 38 | public function setBasename($basename) { |
|
188 | |||
189 | 38 | public function getNodeProp(\DOMNode $node, $name, $default = null) { |
|
195 | |||
196 | 19 | public function setNodeProp(\DOMNode $node = null, $name, $value) { |
|
197 | 19 | if ($node === null) { |
|
198 | 15 | return $this; |
|
199 | } |
||
200 | |||
201 | 4 | if (!$this->nodeProps->contains($node)) { |
|
202 | 4 | $this->nodeProps->attach($node, [$name => $value]); |
|
203 | 4 | } |
|
204 | |||
205 | 4 | $this->nodeProps[$node] = [$name => $value] + $this->nodeProps[$node]; |
|
206 | 4 | return $this; |
|
207 | } |
||
208 | |||
209 | 9 | public function getIndent() { |
|
212 | |||
213 | 9 | public function getDepth() { |
|
216 | |||
217 | public function getScope() { |
||
220 | |||
221 | 9 | public function getAllScopes() { |
|
224 | } |
||
225 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.