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
|
38 |
|
public function __construct($style = self::STYLE_JOIN, array $defaults = []) { |
42
|
|
|
$defaults += [ |
43
|
|
|
'baseIndent' => 0 |
44
|
38 |
|
]; |
45
|
|
|
|
46
|
38 |
|
$this->buffers = []; |
|
|
|
|
47
|
38 |
|
$this->nodeProps = new \SplObjectStorage(); |
48
|
38 |
|
$this->style = $style; |
|
|
|
|
49
|
38 |
|
$this->defaults = $defaults; |
50
|
38 |
|
$this->select(''); |
51
|
38 |
|
} |
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) { |
71
|
38 |
|
$this->current->echoLiteral($value); |
72
|
38 |
|
} |
73
|
|
|
|
74
|
26 |
|
public function echoCode($php) { |
75
|
26 |
|
$this->current->echoCode($php); |
76
|
26 |
|
} |
77
|
|
|
|
78
|
38 |
|
public function appendCode($php) { |
79
|
38 |
|
$this->current->appendCode($php); |
80
|
38 |
|
} |
81
|
|
|
|
82
|
38 |
|
public function indent($add) { |
83
|
38 |
|
$this->current->indent($add); |
84
|
38 |
|
} |
85
|
|
|
|
86
|
13 |
|
public function depth($add = 1) { |
87
|
13 |
|
$this->current->depth($add); |
88
|
13 |
|
} |
89
|
|
|
|
90
|
13 |
|
public function depthName($name, $add = 0) { |
|
|
|
|
91
|
13 |
|
return $this->current->depthName($name, $add); |
92
|
|
|
} |
93
|
|
|
|
94
|
38 |
|
public function pushScope(array $vars) { |
95
|
38 |
|
$this->current->pushScope($vars); |
96
|
38 |
|
} |
97
|
|
|
|
98
|
38 |
|
public function popScope() { |
99
|
38 |
|
$this->current->popScope(); |
100
|
38 |
|
} |
101
|
|
|
|
102
|
34 |
|
public function getScopeVariables() { |
103
|
34 |
|
return $this->current->getScopeVariables(); |
104
|
|
|
} |
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() { |
116
|
38 |
|
return implode("\n\n", array_map(function ($buffer) { |
117
|
|
|
/* @var ComponentBuffer $buffer */ |
118
|
38 |
|
return $buffer->flush(); |
119
|
38 |
|
}, $this->buffers)); |
120
|
|
|
} |
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) { |
146
|
2 |
|
return str_repeat(' ', ($this->defaults['baseIndent'] + $add) * 4); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get the style. |
151
|
|
|
* |
152
|
|
|
* @return string Returns the style. |
153
|
|
|
*/ |
154
|
38 |
|
public function getStyle() { |
155
|
38 |
|
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
|
38 |
|
public function setBasename($basename) { |
185
|
38 |
|
$this->basename = $basename; |
186
|
38 |
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
38 |
|
public function getNodeProp(\DOMNode $node, $name, $default = null) { |
|
|
|
|
190
|
38 |
|
if (!$this->nodeProps->contains($node) || !array_key_exists($name, $this->nodeProps[$node])) { |
191
|
38 |
|
return $default; |
192
|
|
|
} |
193
|
4 |
|
return $this->nodeProps[$node][$name]; |
194
|
|
|
} |
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() { |
210
|
9 |
|
return $this->current->getIndent(); |
211
|
|
|
} |
212
|
|
|
|
213
|
9 |
|
public function getDepth() { |
214
|
9 |
|
return $this->current->getDepth(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function getScope() { |
218
|
|
|
return $this->current->getScope(); |
219
|
|
|
} |
220
|
|
|
|
221
|
9 |
|
public function getAllScopes() { |
222
|
9 |
|
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.