|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Todd Burry <[email protected]> |
|
4
|
|
|
* @copyright 2009-2017 Vanilla Forums Inc. |
|
5
|
|
|
* @license MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Ebi; |
|
9
|
|
|
|
|
10
|
|
|
|
|
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
|
54 |
|
public function __construct($style = self::STYLE_JOIN, array $defaults = []) { |
|
42
|
|
|
$defaults += [ |
|
43
|
|
|
'baseIndent' => 0 |
|
44
|
54 |
|
]; |
|
45
|
|
|
|
|
46
|
54 |
|
$this->buffers = []; |
|
|
|
|
|
|
47
|
54 |
|
$this->nodeProps = new \SplObjectStorage(); |
|
48
|
54 |
|
$this->style = $style; |
|
|
|
|
|
|
49
|
54 |
|
$this->defaults = $defaults; |
|
50
|
54 |
|
$this->select(''); |
|
51
|
54 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Select a specific component buffer. |
|
55
|
|
|
* @param $component |
|
56
|
|
|
*/ |
|
57
|
54 |
|
public function select($component) { |
|
|
|
|
|
|
58
|
54 |
|
$previous = $this->currentName; |
|
|
|
|
|
|
59
|
54 |
|
$this->currentName = $component; |
|
60
|
|
|
|
|
61
|
54 |
|
if (!array_key_exists($component, $this->buffers)) { |
|
62
|
54 |
|
$this->buffers[$component] = $buffer = new ComponentBuffer($this->defaults); |
|
|
|
|
|
|
63
|
54 |
|
} |
|
64
|
|
|
|
|
65
|
54 |
|
$this->current =& $this->buffers[$component]; |
|
66
|
|
|
|
|
67
|
54 |
|
return $previous; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
52 |
|
public function echoLiteral($value) { |
|
71
|
52 |
|
$this->current->echoLiteral($value); |
|
72
|
52 |
|
} |
|
73
|
|
|
|
|
74
|
42 |
|
public function echoCode($php) { |
|
75
|
42 |
|
$this->current->echoCode($php); |
|
76
|
42 |
|
} |
|
77
|
|
|
|
|
78
|
54 |
|
public function appendCode($php) { |
|
79
|
54 |
|
$this->current->appendCode($php); |
|
80
|
54 |
|
} |
|
81
|
|
|
|
|
82
|
54 |
|
public function indent($add) { |
|
83
|
54 |
|
$this->current->indent($add); |
|
84
|
54 |
|
} |
|
85
|
|
|
|
|
86
|
18 |
|
public function depth($add = 1) { |
|
87
|
18 |
|
$this->current->depth($add); |
|
88
|
18 |
|
} |
|
89
|
|
|
|
|
90
|
18 |
|
public function depthName($name, $add = 0) { |
|
|
|
|
|
|
91
|
18 |
|
return $this->current->depthName($name, $add); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
54 |
|
public function pushScope(array $vars) { |
|
95
|
54 |
|
$this->current->pushScope($vars); |
|
96
|
54 |
|
} |
|
97
|
|
|
|
|
98
|
54 |
|
public function popScope() { |
|
99
|
54 |
|
$this->current->popScope(); |
|
100
|
54 |
|
} |
|
101
|
|
|
|
|
102
|
49 |
|
public function getScopeVariables() { |
|
103
|
49 |
|
return $this->current->getScopeVariables(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
54 |
|
public function flush() { |
|
107
|
54 |
|
switch ($this->getStyle()) { |
|
108
|
54 |
|
case self::STYLE_ARRAY: |
|
109
|
11 |
|
return $this->flushArray(); |
|
110
|
54 |
|
default: |
|
111
|
54 |
|
return $this->flushJoin(); |
|
112
|
54 |
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
private function flushJoin() { |
|
116
|
54 |
|
return implode("\n\n", array_map(function ($buffer) { |
|
117
|
|
|
/* @var ComponentBuffer $buffer */ |
|
118
|
54 |
|
return $buffer->flush(); |
|
119
|
54 |
|
}, $this->buffers)); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
11 |
|
private function flushArray() { |
|
123
|
11 |
|
$result = []; |
|
124
|
|
|
|
|
125
|
11 |
|
foreach ($this->buffers as $name => $buffer) { |
|
126
|
11 |
|
$flushed = $buffer->flush(); |
|
127
|
11 |
|
if (empty($flushed)) { |
|
128
|
9 |
|
continue; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
3 |
|
if ($name === '') { |
|
|
|
|
|
|
132
|
3 |
|
$result[] = $flushed; |
|
133
|
3 |
|
} else { |
|
134
|
1 |
|
$result[] = var_export($name, true).' => '.$flushed; |
|
135
|
|
|
} |
|
136
|
11 |
|
} |
|
137
|
|
|
|
|
138
|
11 |
|
if (empty($result)) { |
|
139
|
9 |
|
return '[]'; |
|
140
|
|
|
} else { |
|
141
|
3 |
|
return "[\n".implode(",\n\n", $result)."\n".$this->px().']'; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
3 |
|
protected function px($add = 0) { |
|
146
|
3 |
|
return str_repeat(' ', ($this->defaults['baseIndent'] + $add) * 4); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Get the style. |
|
151
|
|
|
* |
|
152
|
|
|
* @return string Returns the style. |
|
153
|
|
|
*/ |
|
154
|
54 |
|
public function getStyle() { |
|
155
|
54 |
|
return $this->style; |
|
156
|
|
|
} |
|
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) { |
|
165
|
|
|
$this->style = $style; |
|
166
|
|
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Get the basename. |
|
171
|
|
|
* |
|
172
|
|
|
* @return string Returns the basename. |
|
173
|
|
|
*/ |
|
174
|
|
|
public function getBasename() { |
|
175
|
|
|
return $this->basename; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Set the basename. |
|
180
|
|
|
* |
|
181
|
|
|
* @param string $basename |
|
182
|
|
|
* @return $this |
|
183
|
|
|
*/ |
|
184
|
54 |
|
public function setBasename($basename) { |
|
185
|
54 |
|
$this->basename = $basename; |
|
186
|
54 |
|
return $this; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
54 |
|
public function getNodeProp(\DOMNode $node, $name, $default = null) { |
|
|
|
|
|
|
190
|
54 |
|
if (!$this->nodeProps->contains($node) || !array_key_exists($name, $this->nodeProps[$node])) { |
|
191
|
54 |
|
return $default; |
|
192
|
|
|
} |
|
193
|
4 |
|
return $this->nodeProps[$node][$name]; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
21 |
|
public function setNodeProp(\DOMNode $node = null, $name, $value) { |
|
197
|
21 |
|
if ($node === null) { |
|
198
|
17 |
|
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
|
11 |
|
public function getIndent() { |
|
210
|
11 |
|
return $this->current->getIndent(); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
11 |
|
public function getDepth() { |
|
214
|
11 |
|
return $this->current->getDepth(); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
public function getScope() { |
|
218
|
|
|
return $this->current->getScope(); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
11 |
|
public function getAllScopes() { |
|
222
|
11 |
|
return $this->current->getAllScopes(); |
|
223
|
|
|
} |
|
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.