Skip to content

Commit 118f3fb

Browse files
author
Oliver Baumann
committed
Merge remote-tracking branch 'upstream/master' into bugfix/handle-comment-nodes
2 parents bece353 + c3a1e36 commit 118f3fb

File tree

3 files changed

+85
-5
lines changed

3 files changed

+85
-5
lines changed

src/Compiler.php

Lines changed: 48 additions & 4 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

@@ -126,8 +146,8 @@ private function handleAttributeBinding(DOMElement $node)
126146
$this->logger->debug('- setAttribute "'.$name.'" with value');
127147
$node->setAttribute(
128148
$name,
129-
Replacements::getSanitizedConstant('DOUBLE_CURLY_OPEN') .
130-
$value .
149+
Replacements::getSanitizedConstant('DOUBLE_CURLY_OPEN').
150+
$value.
131151
Replacements::getSanitizedConstant('DOUBLE_CURLY_CLOSE')
132152
);
133153
}
@@ -304,7 +324,7 @@ protected function sanitizeCondition(string $condition)
304324
$condition = str_replace('&&', 'and', $condition);
305325
$condition = str_replace('||', 'or', $condition);
306326

307-
foreach(Replacements::getConstants() as $constant => $value) {
327+
foreach (Replacements::getConstants() as $constant => $value) {
308328
$condition = str_replace($value, Replacements::getSanitizedConstant($constant), $condition);
309329
}
310330

@@ -313,10 +333,34 @@ protected function sanitizeCondition(string $condition)
313333

314334
protected function replacePlaceholders(string $string)
315335
{
316-
foreach(Replacements::getConstants() as $constant => $value) {
336+
foreach (Replacements::getConstants() as $constant => $value) {
317337
$string = str_replace(Replacements::getSanitizedConstant($constant), $value, $string);
318338
}
319339

320340
return $string;
321341
}
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+
}
322366
}

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)