Skip to content

Commit 1a1f044

Browse files
authored
Merge pull request #13 from Paneon/develop
Develop
2 parents ec1b371 + 25deaab commit 1a1f044

11 files changed

+85
-52
lines changed

src/Compiler.php

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,39 +101,41 @@ public function convertNode(DOMNode $node): DOMNode
101101
break;
102102
}
103103

104+
$this->handleFor($node);
105+
$this->stripEventHandlers($node);
106+
//$this->handleRawHtml($node, $data);
107+
104108
if (in_array($node->nodeName, array_keys($this->components))) {
105-
$currentComponent = $this->components[$node->nodeName];
106-
$this->handleIf($node);
107-
$this->handleFor($node);
109+
$matchedComponent = $this->components[$node->nodeName];
110+
$usedComponent = new Component($matchedComponent->getName(), $matchedComponent->getPath());
108111

109112
if ($node->hasAttributes()) {
110113
/** @var DOMAttr $attribute */
111114
foreach ($node->attributes as $attribute) {
112115
if (strpos($attribute->name, 'v-bind:') === 0 || strpos($attribute->name, ':') === 0) {
113116
$name = substr($attribute->name, strpos($attribute->name, ':') + 1);
114-
$currentComponent->addProperty($name, $attribute->value, true);
117+
$value = $this->refactorTemplateString($attribute->value);
118+
119+
$usedComponent->addProperty($name, $value, true);
115120
} else {
116-
$currentComponent->addProperty($attribute->name, '"'.$attribute->value.'"', false);
121+
$usedComponent->addProperty($attribute->name, '"'.$attribute->value.'"', false);
117122
}
118123
}
119124
}
120125

121126
$include = $this->document->createTextNode(
122127
$this->builder->createIncludePartial(
123-
$currentComponent->getPath(),
124-
$currentComponent->getProperties()
128+
$usedComponent->getPath(),
129+
$usedComponent->getProperties()
125130
)
126131
);
127132

128133
$node->parentNode->insertBefore($include, $node);
129134
$node->parentNode->removeChild($node);
135+
130136
return $node;
131137
}
132138

133-
$this->stripEventHandlers($node);
134-
$this->handleFor($node);
135-
//$this->handleRawHtml($node, $data);
136-
137139
$this->handleAttributeBinding($node);
138140

139141
foreach (iterator_to_array($node->childNodes) as $childNode) {
@@ -222,7 +224,7 @@ private function handleAttributeBinding(DOMElement $node)
222224

223225
$node->setAttribute($name, $templateStringContent);
224226
} else {
225-
$this->logger->warning('- No Handling for: '.$value);
227+
$this->logger->debug('- No Handling for: '.$value);
226228
}
227229

228230
$this->logger->debug('=> remove original '.$attribute->name);
@@ -422,4 +424,18 @@ protected function addBanner(string $html)
422424

423425
return $html;
424426
}
427+
428+
public function refactorTemplateString($value)
429+
{
430+
if (preg_match('/^`(?P<content>.+)`$/', $value, $matches)) {
431+
$templateStringContent = '"'.$matches['content'].'"';
432+
$value = preg_replace(
433+
'/\$\{(.+)\}/',
434+
'{{ $1 }}',
435+
$templateStringContent
436+
);
437+
}
438+
439+
return $value;
440+
}
425441
}

src/Component.php

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,31 +44,27 @@ public function getProperties(): array
4444
return $this->properties;
4545
}
4646

47-
/**
48-
* @param string $fileName
49-
*
50-
* @return Component
51-
*/
52-
public function loadFile(string $fileName): self
53-
{
54-
$this->fileName = $fileName;
55-
56-
@$this->document->loadHTMLFile($this->assetPath . $fileName);
57-
$this->templateElement = $this->document->getElementsByTagName('template')->item(0);
58-
59-
$this->rootElement = $this->getRootNode($this->templateElement);
60-
$this->templateHtml = $this->getInnerHtml($this->templateElement);
61-
62-
return $this;
63-
}
64-
6547
public function registerComponents(string $name, string $path)
6648
{
6749
$this->components[$name] = $path;
6850
}
6951

7052
public function addProperty(string $name, string $value, bool $isBinding = false) {
71-
$this->properties[] = new Property($name, $value, $isBinding);
53+
$this->properties[] = new Property(
54+
$this->kebabToCamelCase($name),
55+
$value,
56+
$isBinding
57+
);
7258
}
7359

60+
public function kebabToCamelCase($string, $capitalizeFirstCharacter = false)
61+
{
62+
$str = str_replace('-', '', ucwords($string, '-'));
63+
64+
if (!$capitalizeFirstCharacter) {
65+
$str = lcfirst($str);
66+
}
67+
68+
return $str;
69+
}
7470
}

src/Utils/TwigBuilder.php

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ public function createVariable($name, $assignment)
2929

3030
public function createIf(string $condition)
3131
{
32+
$condition = $this->refactorCondition($condition);
33+
3234
return $this->createBlock('if '.$condition);
3335
}
3436

3537
public function createElseIf(string $condition)
3638
{
39+
$condition = $this->refactorCondition($condition);
40+
3741
return $this->createBlock('elseif '.$condition);
3842
}
3943

@@ -87,7 +91,7 @@ public function createMultilineComment(array $comments)
8791

8892
public function createBlock($content)
8993
{
90-
return $this->options['tag_block'][self::OPEN].' '.$content.' '.$this->options['tag_block'][self::CLOSE];
94+
return "\n".$this->options['tag_block'][self::OPEN].' '.$content.' '.$this->options['tag_block'][self::CLOSE];
9195
}
9296

9397
/**
@@ -102,30 +106,36 @@ public function createIncludePartial(string $partialPath, array $variables = [])
102106
return $this->createBlock('include "'.$partialPath.'"');
103107
}
104108

109+
$serializedProperties = $this->serializeComponentProperties($variables);
110+
111+
return $this->createBlock('include "'.$partialPath.'" with '.$serializedProperties);
112+
}
113+
114+
/**
115+
* @param Property[] $properties
116+
* @return string
117+
*/
118+
public function serializeComponentProperties(array $properties): string
119+
{
105120
$props = [];
106121

107122
/** @var Property $property */
108-
foreach ($variables as $property) {
109-
if($property->getName() !== 'key') {
110-
$value = $this->checkPropertyValue($property->getValue());
111-
$props[] = $property->getName().': '.$value;
123+
foreach ($properties as $property) {
124+
if($property->getName() === 'key') {
125+
continue;
112126
}
127+
128+
$props[] = '\''.$property->getName().'\''.': '.$property->getValue();
113129
}
114130

115-
return $this->createBlock('include "'.$partialPath.'" with { '.implode(', ', $props).' }');
131+
return '{ '.implode(', ', $props).' }';
116132
}
117133

118-
public function checkPropertyValue($value)
134+
public function refactorCondition(string $condition): string
119135
{
120-
if (preg_match('/^`(?P<content>.+)`$/', $value, $matches)) {
121-
$templateStringContent = '"'.$matches['content'].'"';
122-
$value = preg_replace(
123-
'/\$\{(.+)\}/',
124-
'{{ $1 }}',
125-
$templateStringContent
126-
);
127-
}
136+
$condition = str_replace('===', '==', $condition);
137+
$condition = str_replace('!==', '!=', $condition);
128138

129-
return $value;
139+
return $condition;
130140
}
131141
}

tests/AbstractTestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ protected function createDocumentWithHtml(string $html): DOMDocument
4545
protected function normalizeHtml($html): string
4646
{
4747
$html = preg_replace('/(\s)+/s', '\\1', $html);
48+
$html = str_replace("\n", '', $html);
4849

4950
// Trim node text
5051
$html = preg_replace('/\>[^\S ]+/s', ">", $html);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<div>
2-
{% include "/templates/ChildComponent.twig" with { style: "fill:{{ color }};", test: "test" } %}
2+
{% include "/templates/ChildComponent.twig" with { 'style': "fill:{{ color }};", 'test': "test" } %}
33
</div>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<div>
2-
{% for a in listOfProducts %}{% include "/templates/ChildComponent.twig" with { string: "string", product: productA } %}{% endfor %}
2+
{% for a in listOfProducts %}{% include "/templates/ChildComponent.twig" with { 'string': "string", 'product': productA } %}{% endfor %}
33
</div>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<div>
2-
{% if a > 5 %}{% include "/templates/ChildComponent.twig" with { string: "string", product: productA } %}{% endif %}
2+
{% if a > 5 %}{% include "/templates/ChildComponent.twig" with { 'string': "string", 'product': productA } %}{% endif %}
33
</div>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<div>
2-
{% include "/templates/ChildComponent.twig" with { product: testProduct, string: "Test" } %}
2+
{% include "/templates/ChildComponent.twig" with { 'product': testProduct, 'testA': "Hello", 'string': "Test" } %}
33
</div>

tests/fixtures/vue-component/component-with-prop.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div>
3-
<ChildComponent :product="testProduct" string="Test" />
3+
<ChildComponent :product="testProduct" test-a="Hello" string="Test" />
44
</div>
55
</template>
66

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div>
2+
{% if a == 1 %}
3+
<div>Text</div>
4+
{% endif %}
5+
</div>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<template>
2+
<div>
3+
<div v-if="a === 1">Text</div>
4+
</div>
5+
</template>

0 commit comments

Comments
 (0)