1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ILess |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace ILess; |
11
|
|
|
|
12
|
|
|
use ILess\Node\CompilableInterface; |
13
|
|
|
use ILess\Node\GenerateCSSInterface; |
14
|
|
|
use ILess\Node\VisitableInterface; |
15
|
|
|
use ILess\Output\OutputInterface; |
16
|
|
|
use ILess\Output\StandardOutput; |
17
|
|
|
use ILess\Visitor\VisitorInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Base node. |
21
|
|
|
*/ |
22
|
|
|
abstract class Node implements VisitableInterface, |
23
|
|
|
GenerateCSSInterface, CompilableInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The value. |
27
|
|
|
* |
28
|
|
|
* @var Node|string |
29
|
|
|
*/ |
30
|
|
|
public $value; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* ILess\Debug information. |
34
|
|
|
* |
35
|
|
|
* @var DebugInfo |
36
|
|
|
*/ |
37
|
|
|
public $debugInfo; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The node type. Each node should define the type. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $type; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Current file info. |
48
|
|
|
* |
49
|
|
|
* @var FileInfo |
50
|
|
|
*/ |
51
|
|
|
public $currentFileInfo; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var bool |
55
|
|
|
*/ |
56
|
|
|
public $compileFirst = false; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Constructor. |
60
|
|
|
* |
61
|
|
|
* @param mixed $value |
62
|
|
|
*/ |
63
|
|
|
public function __construct($value) |
64
|
|
|
{ |
65
|
|
|
$this->value = $value; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns the node type. |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function getType() |
74
|
|
|
{ |
75
|
|
|
return $this->type; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Checks if given method exists. |
80
|
|
|
* |
81
|
|
|
* @param mixed $var The variable name |
82
|
|
|
* @param string $methodName The method name |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public static function methodExists($var, $methodName) |
87
|
|
|
{ |
88
|
|
|
return is_object($var) && method_exists($var, $methodName); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Checks if given property exists. |
93
|
|
|
* |
94
|
|
|
* @param mixed $var The variable to check |
95
|
|
|
* @param string $property The property name |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public static function propertyExists($var, $property) |
100
|
|
|
{ |
101
|
|
|
return is_object($var) && property_exists($var, $property); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns debug information for the node. |
106
|
|
|
* |
107
|
|
|
* @param Context $context The context |
108
|
|
|
* @param Node $node The context node |
109
|
|
|
* @param string $lineSeparator Line separator |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public static function getDebugInfo(Context $context, Node $node, $lineSeparator = '') |
114
|
|
|
{ |
115
|
|
|
$result = ''; |
116
|
|
|
|
117
|
|
|
if ($node->debugInfo && $context->dumpLineNumbers && !$context->compress) { |
118
|
|
|
switch ((string) $context->dumpLineNumbers) { |
119
|
|
|
case DebugInfo::FORMAT_COMMENT; |
120
|
|
|
$result = $node->debugInfo->getAsComment(); |
121
|
|
|
break; |
122
|
|
|
|
123
|
|
|
case DebugInfo::FORMAT_MEDIA_QUERY; |
124
|
|
|
$result = $node->debugInfo->getAsMediaQuery(); |
125
|
|
|
break; |
126
|
|
|
|
127
|
|
|
case DebugInfo::FORMAT_ALL; |
128
|
|
|
case '1': |
129
|
|
|
$result = sprintf('%s%s%s', |
130
|
|
|
$node->debugInfo->getAsComment(), $lineSeparator, |
131
|
|
|
$node->debugInfo->getAsMediaQuery() |
132
|
|
|
); |
133
|
|
|
break; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $result; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Outputs the ruleset rules. |
142
|
|
|
* |
143
|
|
|
* @param Context $context |
144
|
|
|
* @param OutputInterface $output |
145
|
|
|
* @param array $rules |
146
|
|
|
*/ |
147
|
|
|
public static function outputRuleset(Context $context, OutputInterface $output, array $rules) |
148
|
|
|
{ |
149
|
|
|
++$context->tabLevel; |
150
|
|
|
$rulesCount = count($rules); |
151
|
|
|
|
152
|
|
|
// compression |
153
|
|
|
if ($context->compress) { |
154
|
|
|
$output->add('{'); |
155
|
|
|
for ($i = 0; $i < $rulesCount; ++$i) { |
156
|
|
|
$rules[$i]->generateCSS($context, $output); |
157
|
|
|
} |
158
|
|
|
$output->add('}'); |
159
|
|
|
--$context->tabLevel; |
160
|
|
|
|
161
|
|
|
return; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
// Non-compressed |
165
|
|
|
$tabSetStr = "\n" . str_repeat(' ', $context->tabLevel - 1); |
166
|
|
|
$tabRuleStr = $tabSetStr . ' '; |
167
|
|
|
|
168
|
|
|
// Non-compressed |
169
|
|
|
if (!$rulesCount) { |
170
|
|
|
$output->add(' {' . $tabSetStr . '}'); |
171
|
|
|
} else { |
172
|
|
|
$output->add(' {' . $tabRuleStr); |
173
|
|
|
$rules[0]->generateCSS($context, $output); |
174
|
|
|
for ($i = 1; $i < $rulesCount; ++$i) { |
175
|
|
|
$output->add($tabRuleStr); |
176
|
|
|
$rules[$i]->generateCSS($context, $output); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$output->add($tabSetStr . '}'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
--$context->tabLevel; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Convert to string. |
187
|
|
|
* |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
|
|
public function toString() |
191
|
|
|
{ |
192
|
|
|
return (string) $this->value; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Convert to string. |
197
|
|
|
* |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
|
|
public function __toString() |
201
|
|
|
{ |
202
|
|
|
return $this->toString(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* {@inheritdoc} |
207
|
|
|
*/ |
208
|
|
|
public function toCSS(Context $context) |
209
|
|
|
{ |
210
|
|
|
$output = new StandardOutput(); |
211
|
|
|
$this->generateCSS($context, $output); |
212
|
|
|
|
213
|
|
|
return $output->toString(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* {@inheritdoc} |
218
|
|
|
*/ |
219
|
|
|
public function accept(VisitorInterface $visitor) |
220
|
|
|
{ |
221
|
|
|
$this->value = $visitor->visit($this->value); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Generate the CSS and put it in the output container. |
226
|
|
|
* |
227
|
|
|
* @param Context $context The context |
228
|
|
|
* @param OutputInterface $output The output |
229
|
|
|
*/ |
230
|
|
|
public function generateCSS(Context $context, OutputInterface $output) |
231
|
|
|
{ |
232
|
|
|
$output->add($this->value); |
|
|
|
|
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Compiles the node. |
237
|
|
|
* |
238
|
|
|
* @param Context $context The context |
239
|
|
|
* @param array|null $arguments Array of arguments |
240
|
|
|
* @param bool|null $important Important flag |
241
|
|
|
* |
242
|
|
|
* @return Node |
243
|
|
|
*/ |
244
|
|
|
public function compile(Context $context, $arguments = null, $important = null) |
245
|
|
|
{ |
246
|
|
|
return $this; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @return bool |
251
|
|
|
*/ |
252
|
|
|
public function isRulesetLike() |
253
|
|
|
{ |
254
|
|
|
return false; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @return bool |
259
|
|
|
*/ |
260
|
|
|
public function compileFirst() |
261
|
|
|
{ |
262
|
|
|
return $this->compileFirst; |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.