Skip to content

Commit 3118e16

Browse files
author
Alexander Pape
committed
Add possibility to add banner comment to the generated twig templates
1 parent 4812d51 commit 3118e16

File tree

3 files changed

+86
-6
lines changed

3 files changed

+86
-6
lines changed

src/Compiler.php

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,31 @@ class Compiler
2626
/** @var LoggerInterface */
2727
protected $logger;
2828

29+
/** @var string[] */
30+
protected $banner;
31+
2932
public function __construct(DOMDocument $document, LoggerInterface $logger)
3033
{
3134
$this->logger = $logger;
3235
$this->document = $document;
3336
$this->lastCloseIf = null;
37+
$this->banner = [];
3438

3539
$this->logger->debug("\n--------- New Compiler Instance ----------\n");
3640
}
3741

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+
3854
/**
3955
* @throws Exception
4056
*/
@@ -52,6 +68,10 @@ public function convert(): string
5268

5369
$html = $this->replacePlaceholders($html);
5470

71+
if (!empty($this->banner)) {
72+
$html = $this->addBanner($html);
73+
}
74+
5575
return $html;
5676
}
5777

@@ -122,8 +142,8 @@ private function handleAttributeBinding(DOMElement $node)
122142
$this->logger->debug('- setAttribute "'.$name.'" with value');
123143
$node->setAttribute(
124144
$name,
125-
Replacements::getSanitizedConstant('DOUBLE_CURLY_OPEN') .
126-
$value .
145+
Replacements::getSanitizedConstant('DOUBLE_CURLY_OPEN').
146+
$value.
127147
Replacements::getSanitizedConstant('DOUBLE_CURLY_CLOSE')
128148
);
129149
}
@@ -300,7 +320,7 @@ protected function sanitizeCondition(string $condition)
300320
$condition = str_replace('&&', 'and', $condition);
301321
$condition = str_replace('||', 'or', $condition);
302322

303-
foreach(Replacements::getConstants() as $constant => $value) {
323+
foreach (Replacements::getConstants() as $constant => $value) {
304324
$condition = str_replace($value, Replacements::getSanitizedConstant($constant), $condition);
305325
}
306326

@@ -309,10 +329,34 @@ protected function sanitizeCondition(string $condition)
309329

310330
protected function replacePlaceholders(string $string)
311331
{
312-
foreach(Replacements::getConstants() as $constant => $value) {
332+
foreach (Replacements::getConstants() as $constant => $value) {
313333
$string = str_replace(Replacements::getSanitizedConstant($constant), $value, $string);
314334
}
315335

316336
return $string;
317337
}
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+
}

tests/AbstractTestCase.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ protected function createCompiler(string $template): Compiler
1515
$document = $this->createDocumentWithHtml($template);
1616
$compiler = new Compiler($document, $this->createLogger());
1717

18-
1918
return $compiler;
2019
}
2120

tests/CompilerTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,41 @@ public function leavesMustacheVariablesIntact()
1515

1616
$this->assertEqualHtml($expected, $actual);
1717
}
18+
19+
/** @test */
20+
public function setBannerWithSingleLineAddsBannerCommentToTheTopOfTheTwigFile()
21+
{
22+
$html = '<template><div>{{ someVariable }}</div></template>';
23+
$expected = '{# This file was generated using VueToTwig #}<div>{{ someVariable }}</div>';
24+
$compiler = $this->createCompiler($html);
25+
$compiler->setBanner('This file was generated using VueToTwig');
26+
27+
$actual = $compiler->convert();
28+
29+
$this->assertEqualHtml($expected, $actual);
30+
31+
}
32+
33+
/** @test */
34+
public function setBannerAddsMultipleCommentsToTheTopOfTheTwigFile()
35+
{
36+
$html = '<template><div>{{ someVariable }}</div></template>';
37+
$expected = '
38+
{#
39+
# This file was generated using VueToTwig
40+
# Source: assets/js/SomeComponent.vue
41+
#}
42+
<div>{{ someVariable }}</div>';
43+
44+
$compiler = $this->createCompiler($html);
45+
$compiler->setBanner([
46+
'This file was generated using VueToTwig',
47+
'Source: assets/js/SomeComponent.vue',
48+
]);
49+
50+
$actual = $compiler->convert();
51+
52+
$this->assertEqualHtml($expected, $actual);
53+
54+
}
1855
}

0 commit comments

Comments
 (0)