Skip to content

Commit 43a0e94

Browse files
author
Pia Sinzig
committed
SHODEVS2-122 add first test case (child component without properties)
1 parent 4812d51 commit 43a0e94

File tree

5 files changed

+79
-26
lines changed

5 files changed

+79
-26
lines changed

src/Compiler.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
class Compiler
1515
{
1616

17-
/** @var String[] */
17+
/** @var Component[] */
1818
protected $components;
1919

2020
/** @var DOMDocument */
@@ -31,6 +31,7 @@ public function __construct(DOMDocument $document, LoggerInterface $logger)
3131
$this->logger = $logger;
3232
$this->document = $document;
3333
$this->lastCloseIf = null;
34+
$this->components = [];
3435

3536
$this->logger->debug("\n--------- New Compiler Instance ----------\n");
3637
}
@@ -70,6 +71,13 @@ public function convertNode(DOMNode $node): DOMNode
7071
$this->logger->warning("Document node found.");
7172
}
7273

74+
if (in_array($node->nodeName, array_keys($this->components))) {
75+
$currentComponent = $this->components[$node->nodeName];
76+
$include = $this->document->createTextNode('{% include "'.$currentComponent->getPath().'" %}');
77+
$node->parentNode->insertBefore($include, $node);
78+
$node->parentNode->removeChild($node);
79+
}
80+
7381
$this->stripEventHandlers($node);
7482
$this->handleFor($node);
7583
//$this->handleRawHtml($node, $data);
@@ -282,14 +290,16 @@ private function getRootNode(DOMElement $element): \DOMNode
282290
foreach ($nodes as $node) {
283291
if ($node->nodeType === XML_TEXT_NODE) {
284292
continue;
293+
} else if ($node->nodeName === 'script' || $node->nodeName === 'style') {
294+
continue;
285295
} else {
286296
$tagNodes++;
287297
$firstTagNode = $node;
288298
}
289299
}
290300

291301
if ($tagNodes > 1) {
292-
throw new Exception('Template should have only one root node');
302+
//throw new Exception('Template should have only one root node');
293303
}
294304

295305
return $firstTagNode;
@@ -315,4 +325,9 @@ protected function replacePlaceholders(string $string)
315325

316326
return $string;
317327
}
318-
}
328+
329+
public function registerComponent(string $componentName, string $componentPath)
330+
{
331+
$this->components[strtolower($componentName)] = new Component($componentName, $componentPath);
332+
}
333+
}

src/Component.php

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,29 @@
88

99
class Component
1010
{
11-
/** @var string */
12-
protected $assetPath;
13-
14-
/** @var string */
15-
protected $targetPath;
16-
17-
/** @var string */
18-
protected $fileName;
19-
20-
/** @var DOMDocument */
21-
protected $document;
22-
23-
protected $templateHtml;
24-
protected $data;
25-
protected $templateElement;
26-
protected $rootElement;
27-
2811
/** @var String[] */
2912
protected $components = [];
13+
/**
14+
* @var string
15+
*/
16+
protected $name;
17+
/**
18+
* @var string
19+
*/
20+
protected $path;
3021

31-
public function __construct(string $assetPath = '', string $targetPath = '')
22+
public function __construct(string $name = '', string $path = '')
3223
{
33-
$this->assetPath = $assetPath;
34-
$this->targetPath = $targetPath;
24+
$this->name = $name;
25+
$this->path = $path;
26+
}
27+
28+
public function getName(){
29+
return $this->name;
30+
}
3531

36-
$this->fileName = '';
37-
$this->document = new DOMDocument();
32+
public function getPath(){
33+
return $this->path;
3834
}
3935

4036
/**
@@ -59,4 +55,4 @@ public function registerComponents($name, $path)
5955
{
6056
$this->components[$name] = $path;
6157
}
62-
}
58+
}

tests/VueComponentTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Paneon\VueToTwig\Tests;
4+
5+
class VueComponentTest extends AbstractTestCase
6+
{
7+
/**
8+
* @dataProvider dataProvider
9+
* @throws \Exception
10+
*/
11+
public function testComponent($html, $expected)
12+
{
13+
$compiler = $this->createCompiler($html);
14+
15+
$compiler->registerComponent('ChildComponent', '/templates/ChildComponent.twig');
16+
17+
$actual = $compiler->convert();
18+
19+
$this->assertEqualHtml($expected, $actual);
20+
}
21+
22+
public function dataProvider()
23+
{
24+
return $this->loadFixturesFromDir('vue-component');
25+
}
26+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
{% include "/templates/ChildComponent.twig" %}
3+
</div>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<template>
2+
<div>
3+
<ChildComponent/>
4+
</div>
5+
</template>
6+
<script>
7+
export default {
8+
name: 'component-with-child.vue',
9+
component: {
10+
ChildComponent,
11+
}
12+
};
13+
</script>

0 commit comments

Comments
 (0)