9
9
use DOMText ;
10
10
use Exception ;
11
11
use Paneon \VueToTwig \Models \Replacements ;
12
+ use Paneon \VueToTwig \Utils \TwigBuilder ;
12
13
use Psr \Log \LoggerInterface ;
13
14
14
15
class Compiler
@@ -28,11 +29,16 @@ class Compiler
28
29
29
30
/** @var string[] */
30
31
protected $ banner ;
32
+ /**
33
+ * @var TwigBuilder
34
+ */
35
+ protected $ builder ;
31
36
32
37
public function __construct (DOMDocument $ document , LoggerInterface $ logger )
33
38
{
34
- $ this ->logger = $ logger ;
39
+ $ this ->builder = new TwigBuilder () ;
35
40
$ this ->document = $ document ;
41
+ $ this ->logger = $ logger ;
36
42
$ this ->lastCloseIf = null ;
37
43
$ this ->banner = [];
38
44
@@ -77,10 +83,10 @@ public function convert(): string
77
83
78
84
public function convertNode (DOMNode $ node ): DOMNode
79
85
{
80
- switch ($ node ->nodeType ) {
86
+ switch ($ node ->nodeType ) {
81
87
case XML_TEXT_NODE :
82
88
$ this ->logger ->debug ('Text node found ' , ['name ' => $ node ->nodeName ]);
83
- // fall through to next case, because we don't need to handle either of these node-types
89
+ // fall through to next case, because we don't need to handle either of these node-types
84
90
case XML_COMMENT_NODE :
85
91
$ this ->logger ->debug ('Comment node found ' , ['name ' => $ node ->nodeName ]);
86
92
return $ node ;
@@ -172,11 +178,10 @@ private function handleAttributeBinding(DOMElement $node)
172
178
}
173
179
$ node ->setAttribute ($ name , implode (' ' , $ classes ));
174
180
}
175
- }
176
- /*
181
+ } /*
177
182
* <div :class="`abc ${someDynamicClass}`">
178
183
*/
179
- elseif (preg_match ('/^`(?P<content>.+)`$/ ' , $ value , $ matches )) {
184
+ elseif (preg_match ('/^`(?P<content>.+)`$/ ' , $ value , $ matches )) {
180
185
$ templateStringContent = $ matches ['content ' ];
181
186
182
187
$ templateStringContent = preg_replace (
@@ -186,8 +191,7 @@ private function handleAttributeBinding(DOMElement $node)
186
191
);
187
192
188
193
$ node ->setAttribute ($ name , $ templateStringContent );
189
- }
190
- else {
194
+ } else {
191
195
$ this ->logger ->warning ('- No Handling for: ' .$ value );
192
196
}
193
197
@@ -209,11 +213,11 @@ private function handleIf(DOMElement $node): void
209
213
$ condition = $ this ->sanitizeCondition ($ condition );
210
214
211
215
// Open with if
212
- $ openIf = $ this ->document ->createTextNode (' {% if ' . $ condition. ' %} ' );
216
+ $ openIf = $ this ->document ->createTextNode ($ this -> builder -> createIf ( $ condition) );
213
217
$ node ->parentNode ->insertBefore ($ openIf , $ node );
214
218
215
219
// Close with endif
216
- $ closeIf = $ this ->document ->createTextNode (' {% endif %} ' );
220
+ $ closeIf = $ this ->document ->createTextNode ($ this -> builder -> createEndIf () );
217
221
$ node ->parentNode ->insertBefore ($ closeIf , $ node ->nextSibling );
218
222
219
223
$ this ->lastCloseIf = $ closeIf ;
@@ -224,20 +228,20 @@ private function handleIf(DOMElement $node): void
224
228
$ condition = $ this ->sanitizeCondition ($ condition );
225
229
226
230
// Replace old endif with else
227
- $ this ->lastCloseIf ->textContent = ' {% elseif ' . $ condition. ' %} ' ;
231
+ $ this ->lastCloseIf ->textContent = $ this -> builder -> createElseIf ( $ condition) ;
228
232
229
233
// Close with new endif
230
- $ closeIf = $ this ->document ->createTextNode (' {% endif %} ' );
234
+ $ closeIf = $ this ->document ->createTextNode ($ this -> builder -> createEndIf () );
231
235
$ node ->parentNode ->insertBefore ($ closeIf , $ node ->nextSibling );
232
236
$ this ->lastCloseIf = $ closeIf ;
233
237
234
238
$ node ->removeAttribute ('v-else-if ' );
235
239
} elseif ($ node ->hasAttribute ('v-else ' )) {
236
240
// Replace old endif with else
237
- $ this ->lastCloseIf ->textContent = ' {% else %} ' ;
241
+ $ this ->lastCloseIf ->textContent = $ this -> builder -> createElse () ;
238
242
239
243
// Close with new endif
240
- $ closeIf = $ this ->document ->createTextNode (' {% endif %} ' );
244
+ $ closeIf = $ this ->document ->createTextNode ($ this -> builder -> createEndIf () );
241
245
$ node ->parentNode ->insertBefore ($ closeIf , $ node ->nextSibling );
242
246
$ this ->lastCloseIf = $ closeIf ;
243
247
@@ -268,7 +272,7 @@ private function handleFor(DOMElement $node)
268
272
}
269
273
270
274
// (1)
271
- $ forCommand = ' {% for ' . $ forLeft. ' in ' . $ listName. ' %} ' ;
275
+ $ forCommand = $ this -> builder -> createForItemInList ( $ forLeft, $ listName) ;
272
276
273
277
if (strpos ($ forLeft , ', ' )) {
274
278
$ forLeft = str_replace ('( ' , '' , $ forLeft );
@@ -281,19 +285,19 @@ private function handleFor(DOMElement $node)
281
285
$ forIndex = $ forLeftArray [2 ] ?? null ;
282
286
283
287
// (3)
284
- $ forCommand = ' {% for ' . $ forKey . ' , ' . $ forValue. ' in ' . $ listName . ' %} ' ;
288
+ $ forCommand = $ this -> builder -> createFor ( $ listName , $ forValue, $ forKey ) ;
285
289
286
290
if ($ forIndex ) {
287
291
// (4)
288
- $ forCommand .= ' {% set ' . $ forIndex. ' = loop.index0 %} ' ;
292
+ $ forCommand .= $ this -> builder -> createVariable ( $ forIndex, ' loop.index0 ' ) ;
289
293
}
290
294
}
291
295
292
296
$ startFor = $ this ->document ->createTextNode ($ forCommand );
293
297
$ node ->parentNode ->insertBefore ($ startFor , $ node );
294
298
295
299
// End For
296
- $ endFor = $ this ->document ->createTextNode (' {% endfor %} ' );
300
+ $ endFor = $ this ->document ->createTextNode ($ this -> builder -> createEndFor () );
297
301
$ node ->parentNode ->insertBefore ($ endFor , $ node ->nextSibling );
298
302
299
303
$ node ->removeAttribute ('v-for ' );
@@ -324,11 +328,9 @@ private function getRootNode(DOMElement $element): \DOMNode
324
328
foreach ($ nodes as $ node ) {
325
329
if ($ node ->nodeType === XML_TEXT_NODE ) {
326
330
continue ;
327
- }
328
- elseif (in_array ($ node ->nodeName , ['script ' , 'style ' ])) {
331
+ } elseif (in_array ($ node ->nodeName , ['script ' , 'style ' ])) {
329
332
continue ;
330
- }
331
- else {
333
+ } else {
332
334
$ tagNodes ++;
333
335
$ firstTagNode = $ node ;
334
336
}
@@ -364,7 +366,7 @@ protected function replacePlaceholders(string $string)
364
366
365
367
protected function addSingleLineBanner (string $ html )
366
368
{
367
- return ' {# ' . implode ('' , $ this ->banner ). ' #} ' ."\n" .$ html ;
369
+ return $ this -> builder -> createComment ( implode ('' , $ this ->banner )) ."\n" .$ html ;
368
370
}
369
371
370
372
protected function addBanner (string $ html )
0 commit comments