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 ComponentBuffer { |
12
|
|
|
private $buffer = ''; |
|
|
|
|
13
|
|
|
private $literalBuffer = ''; |
14
|
|
|
private $inEcho = false; |
|
|
|
|
15
|
|
|
private $indent; |
16
|
|
|
private $depth; |
17
|
|
|
private $scopes; |
18
|
|
|
|
19
|
78 |
|
public function __construct(array $defaults = []) { |
20
|
|
|
$defaults += [ |
21
|
78 |
|
'depth' => 0, |
22
|
78 |
|
'indent' => 0, |
23
|
78 |
|
'scopes' => [] |
24
|
78 |
|
]; |
25
|
|
|
|
26
|
78 |
|
$this->depth = $defaults['depth']; |
27
|
78 |
|
$this->indent = $defaults['indent']; |
28
|
78 |
|
$this->scopes = $defaults['scopes']; |
29
|
78 |
|
} |
30
|
|
|
|
31
|
71 |
|
public function echoLiteral($value) { |
32
|
71 |
|
$this->literalBuffer .= $value; |
33
|
71 |
|
} |
34
|
|
|
|
35
|
56 |
|
public function echoCode($php) { |
36
|
56 |
|
if (empty($php)) { |
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
56 |
|
$this->flushLiteralBuffer(); |
41
|
56 |
|
$this->ensureEcho(true); |
42
|
56 |
|
$this->buffer .= $php; |
43
|
56 |
|
} |
44
|
|
|
|
45
|
78 |
|
private function flushLiteralBuffer() { |
46
|
78 |
|
if (!empty($this->literalBuffer)) { |
47
|
61 |
|
$this->ensureEcho(true); |
48
|
61 |
|
$this->buffer .= var_export($this->literalBuffer, true); |
|
|
|
|
49
|
61 |
|
$this->literalBuffer = ''; |
50
|
61 |
|
} |
51
|
78 |
|
} |
52
|
|
|
|
53
|
71 |
|
private function ensureEcho($append) { |
54
|
71 |
|
if (!$this->inEcho) { |
55
|
71 |
|
$this->buffer .= $this->px().'echo '; |
56
|
71 |
|
$this->inEcho = true; |
57
|
71 |
|
} elseif ($append) { |
58
|
47 |
|
$this->buffer .= ",\n".$this->px(+1); |
59
|
47 |
|
} |
60
|
71 |
|
} |
61
|
|
|
|
62
|
78 |
|
protected function px($add = 0) { |
63
|
78 |
|
return str_repeat(' ', ($this->indent + $add) * 4); |
64
|
|
|
} |
65
|
|
|
|
66
|
78 |
|
public function appendCode($php) { |
67
|
78 |
|
$this->flushLiteralBuffer(); |
68
|
78 |
|
$this->flushEcho(); |
69
|
|
|
|
70
|
78 |
|
$this->buffer .= $this->px().$php; |
71
|
78 |
|
} |
72
|
|
|
|
73
|
78 |
|
private function flushEcho() { |
74
|
78 |
|
$this->flushLiteralBuffer(); |
75
|
|
|
|
76
|
78 |
|
if ($this->inEcho) { |
77
|
70 |
|
$this->buffer .= ";\n"; |
78
|
70 |
|
$this->inEcho = false; |
79
|
70 |
|
} |
80
|
78 |
|
} |
81
|
|
|
|
82
|
78 |
|
public function indent($add) { |
83
|
78 |
|
$this->flushEcho(); |
84
|
78 |
|
$this->indent += $add; |
85
|
78 |
|
} |
86
|
|
|
|
87
|
26 |
|
public function depth($add = 1) { |
88
|
26 |
|
$this->depth += $add; |
89
|
26 |
|
} |
90
|
|
|
|
91
|
27 |
|
public function depthName($name, $add = 0) { |
92
|
27 |
|
$depth = $this->depth + $add; |
93
|
|
|
|
94
|
27 |
|
if ($depth === 0) { |
95
|
|
|
return $name; |
96
|
|
|
} else { |
97
|
27 |
|
return $name.$depth; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
78 |
|
public function pushScope(array $vars) { |
102
|
78 |
|
$this->scopes[] = $vars; |
103
|
78 |
|
} |
104
|
|
|
|
105
|
70 |
|
public function popScope() { |
106
|
70 |
|
array_pop($this->scopes); |
107
|
70 |
|
} |
108
|
|
|
|
109
|
70 |
|
public function getScopeVariables() { |
110
|
70 |
|
$r = array_replace(...$this->scopes); |
111
|
70 |
|
return $r; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getScope() { |
115
|
|
|
return end($this->scopes); |
116
|
|
|
} |
117
|
|
|
|
118
|
14 |
|
public function getAllScopes() { |
119
|
14 |
|
return $this->scopes; |
120
|
|
|
} |
121
|
|
|
|
122
|
70 |
|
public function flush() { |
123
|
70 |
|
$this->flushEcho(); |
124
|
|
|
|
125
|
70 |
|
return $this->buffer; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get the indent. |
130
|
|
|
* |
131
|
|
|
* @return int Returns the indent. |
132
|
|
|
*/ |
133
|
14 |
|
public function getIndent() { |
134
|
14 |
|
return $this->indent; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get the depth. |
139
|
|
|
* |
140
|
|
|
* @return mixed Returns the depth. |
141
|
|
|
*/ |
142
|
14 |
|
public function getDepth() { |
143
|
14 |
|
return $this->depth; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
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.