@@ -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
@@ -122,8 +142,8 @@ private function handleAttributeBinding(DOMElement $node)
122
142
$ this ->logger ->debug ('- setAttribute " ' .$ name .'" with value ' );
123
143
$ node ->setAttribute (
124
144
$ name ,
125
- Replacements::getSanitizedConstant ('DOUBLE_CURLY_OPEN ' ) .
126
- $ value .
145
+ Replacements::getSanitizedConstant ('DOUBLE_CURLY_OPEN ' ).
146
+ $ value .
127
147
Replacements::getSanitizedConstant ('DOUBLE_CURLY_CLOSE ' )
128
148
);
129
149
}
@@ -300,7 +320,7 @@ protected function sanitizeCondition(string $condition)
300
320
$ condition = str_replace ('&& ' , 'and ' , $ condition );
301
321
$ condition = str_replace ('|| ' , 'or ' , $ condition );
302
322
303
- foreach (Replacements::getConstants () as $ constant => $ value ) {
323
+ foreach (Replacements::getConstants () as $ constant => $ value ) {
304
324
$ condition = str_replace ($ value , Replacements::getSanitizedConstant ($ constant ), $ condition );
305
325
}
306
326
@@ -309,10 +329,34 @@ protected function sanitizeCondition(string $condition)
309
329
310
330
protected function replacePlaceholders (string $ string )
311
331
{
312
- foreach (Replacements::getConstants () as $ constant => $ value ) {
332
+ foreach (Replacements::getConstants () as $ constant => $ value ) {
313
333
$ string = str_replace (Replacements::getSanitizedConstant ($ constant ), $ value , $ string );
314
334
}
315
335
316
336
return $ string ;
317
337
}
318
- }
338
+
339
+ protected function addSingleLineBanner (string $ html )
340
+ {
341
+ return '{# ' .implode ('' , $ this ->banner ).' #} ' ."\n" .$ html ;
342
+ }
343
+
344
+ protected function addBanner (string $ html )
345
+ {
346
+ if (count ($ this ->banner ) === 1 ) {
347
+ return $ this ->addSingleLineBanner ($ html );
348
+ }
349
+
350
+ $ bannerLines = ['{# ' ];
351
+
352
+ foreach ($ this ->banner as $ line ) {
353
+ $ bannerLines [] = ' # ' .$ line ;
354
+ }
355
+
356
+ $ bannerLines [] = ' #} ' ;
357
+
358
+ $ html = implode ("\n" , $ bannerLines )."\n" .$ html ;
359
+
360
+ return $ html ;
361
+ }
362
+ }
0 commit comments