@@ -26,15 +26,31 @@ class Compiler
26
26
/** @var LoggerInterface */
27
27
protected $ logger ;
28
28
29
+ /** @var string[] */
30
+ protected $ banner ;
31
+
29
32
public function __construct (DOMDocument $ document , LoggerInterface $ logger )
30
33
{
31
34
$ this ->logger = $ logger ;
32
35
$ this ->document = $ document ;
33
36
$ this ->lastCloseIf = null ;
37
+ $ this ->banner = [];
34
38
35
39
$ this ->logger ->debug ("\n--------- New Compiler Instance ---------- \n" );
36
40
}
37
41
42
+ /**
43
+ * @param string|string[] $strings
44
+ */
45
+ public function setBanner ($ strings ): void
46
+ {
47
+ if (!is_array ($ strings )) {
48
+ $ strings = [$ strings ];
49
+ }
50
+
51
+ $ this ->banner = $ strings ;
52
+ }
53
+
38
54
/**
39
55
* @throws Exception
40
56
*/
@@ -52,6 +68,10 @@ public function convert(): string
52
68
53
69
$ html = $ this ->replacePlaceholders ($ html );
54
70
71
+ if (!empty ($ this ->banner )) {
72
+ $ html = $ this ->addBanner ($ html );
73
+ }
74
+
55
75
return $ html ;
56
76
}
57
77
@@ -126,8 +146,8 @@ private function handleAttributeBinding(DOMElement $node)
126
146
$ this ->logger ->debug ('- setAttribute " ' .$ name .'" with value ' );
127
147
$ node ->setAttribute (
128
148
$ name ,
129
- Replacements::getSanitizedConstant ('DOUBLE_CURLY_OPEN ' ) .
130
- $ value .
149
+ Replacements::getSanitizedConstant ('DOUBLE_CURLY_OPEN ' ).
150
+ $ value .
131
151
Replacements::getSanitizedConstant ('DOUBLE_CURLY_CLOSE ' )
132
152
);
133
153
}
@@ -304,7 +324,7 @@ protected function sanitizeCondition(string $condition)
304
324
$ condition = str_replace ('&& ' , 'and ' , $ condition );
305
325
$ condition = str_replace ('|| ' , 'or ' , $ condition );
306
326
307
- foreach (Replacements::getConstants () as $ constant => $ value ) {
327
+ foreach (Replacements::getConstants () as $ constant => $ value ) {
308
328
$ condition = str_replace ($ value , Replacements::getSanitizedConstant ($ constant ), $ condition );
309
329
}
310
330
@@ -313,10 +333,34 @@ protected function sanitizeCondition(string $condition)
313
333
314
334
protected function replacePlaceholders (string $ string )
315
335
{
316
- foreach (Replacements::getConstants () as $ constant => $ value ) {
336
+ foreach (Replacements::getConstants () as $ constant => $ value ) {
317
337
$ string = str_replace (Replacements::getSanitizedConstant ($ constant ), $ value , $ string );
318
338
}
319
339
320
340
return $ string ;
321
341
}
342
+
343
+ protected function addSingleLineBanner (string $ html )
344
+ {
345
+ return '{# ' .implode ('' , $ this ->banner ).' #} ' ."\n" .$ html ;
346
+ }
347
+
348
+ protected function addBanner (string $ html )
349
+ {
350
+ if (count ($ this ->banner ) === 1 ) {
351
+ return $ this ->addSingleLineBanner ($ html );
352
+ }
353
+
354
+ $ bannerLines = ['{# ' ];
355
+
356
+ foreach ($ this ->banner as $ line ) {
357
+ $ bannerLines [] = ' # ' .$ line ;
358
+ }
359
+
360
+ $ bannerLines [] = ' #} ' ;
361
+
362
+ $ html = implode ("\n" , $ bannerLines )."\n" .$ html ;
363
+
364
+ return $ html ;
365
+ }
322
366
}
0 commit comments