Skip to content

Commit e5462bc

Browse files
authored
Merge pull request #20 from Paneon/feature/slots
Feature/slots
2 parents 0233f04 + fef2bfd commit e5462bc

14 files changed

+522
-20
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"php": ">=7.1",
1414
"ext-dom": "*",
1515
"ext-libxml": "*",
16-
"squizlabs/php_codesniffer": "^3.3"
16+
"squizlabs/php_codesniffer": "^3.3",
17+
"ramsey/uuid": "^3.8"
1718
},
1819
"autoload": {
1920
"psr-4": {

composer.lock

Lines changed: 186 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler.php

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
use DOMNode;
99
use DOMText;
1010
use Exception;
11+
use Paneon\VueToTwig\Models\Component;
12+
use Paneon\VueToTwig\Models\Property;
1113
use Paneon\VueToTwig\Models\Replacements;
14+
use Paneon\VueToTwig\Models\Slot;
1215
use Paneon\VueToTwig\Utils\TwigBuilder;
1316
use Psr\Log\LoggerInterface;
1417

@@ -38,8 +41,11 @@ class Compiler
3841
/** @var Property[] */
3942
protected $properties;
4043

41-
protected $stripWhitespace = true;
44+
protected $replaceVariables = [];
45+
46+
protected $variables = [];
4247

48+
protected $stripWhitespace = true;
4349

4450
public function __construct(DOMDocument $document, LoggerInterface $logger)
4551
{
@@ -86,6 +92,7 @@ public function convert(): string
8692
$resultNode = $this->convertNode($rootNode);
8793
$html = $this->document->saveHTML($resultNode);
8894

95+
$html = $this->addVariableBlocks($html);
8996
$html = $this->replacePlaceholders($html);
9097

9198
if ($this->stripWhitespace) {
@@ -106,6 +113,7 @@ public function convertNode(DOMNode $node): DOMNode
106113
case XML_COMMENT_NODE:
107114
return $node;
108115
case XML_TEXT_NODE:
116+
/** @var DOMText $node */
109117
return $this->handleTextNode($node);
110118
case XML_ELEMENT_NODE:
111119
/** @var DOMElement $node */
@@ -121,6 +129,11 @@ public function convertNode(DOMNode $node): DOMNode
121129
$this->stripEventHandlers($node);
122130
//$this->handleRawHtml($node, $data);
123131

132+
$this->handleDefaultSlot($node);
133+
134+
/*
135+
* Registered Component
136+
*/
124137
if (in_array($node->nodeName, array_keys($this->components))) {
125138
$matchedComponent = $this->components[$node->nodeName];
126139
$usedComponent = new Component($matchedComponent->getName(), $matchedComponent->getPath());
@@ -139,6 +152,24 @@ public function convertNode(DOMNode $node): DOMNode
139152
}
140153
}
141154

155+
/*
156+
* Slots (Default)
157+
*/
158+
if ($node->hasChildNodes()) {
159+
$innerHtml = $this->innerHtmlOfNode($node);
160+
$this->logger->debug('Add default slot:', [
161+
'nodeValue' => $node->nodeValue,
162+
'innerHtml' => $innerHtml,
163+
]);
164+
165+
$slot = $usedComponent->addDefaultSlot($innerHtml);
166+
167+
$this->addReplaceVariable($slot->getSlotContentVariableString(), $slot->getValue());
168+
}
169+
170+
/*
171+
* Include Partial
172+
*/
142173
$include = $this->document->createTextNode(
143174
$this->builder->createIncludePartial(
144175
$usedComponent->getPath(),
@@ -147,6 +178,30 @@ public function convertNode(DOMNode $node): DOMNode
147178
);
148179

149180
$node->parentNode->insertBefore($include, $node);
181+
182+
if ($usedComponent->hasSlots()) {
183+
184+
foreach ($usedComponent->getSlots() as $slotName => $slot) {
185+
// Add variable which contains the content (set)
186+
$openSet = $this->document->createTextNode(
187+
$this->builder->createSet($slot->getSlotValueName())
188+
);
189+
$node->parentNode->insertBefore($openSet, $include);
190+
191+
$setContent = $this->document->createTextNode($slot->getSlotContentVariableString());
192+
193+
$node->parentNode->insertBefore($setContent, $include);
194+
195+
// Close variable (endset)
196+
$closeSet = $this->document->createTextNode(
197+
$this->builder->closeSet()
198+
);
199+
$node->parentNode->insertBefore($closeSet, $include);
200+
}
201+
202+
}
203+
204+
// Remove original node
150205
$node->parentNode->removeChild($node);
151206

152207
return $node;
@@ -519,6 +574,10 @@ protected function replacePlaceholders(string $string)
519574
$string = str_replace(Replacements::getSanitizedConstant($constant), $value, $string);
520575
}
521576

577+
foreach ($this->replaceVariables as $safeString => $value) {
578+
$string = str_replace($safeString, $value, $string);
579+
}
580+
522581
return $string;
523582
}
524583

@@ -605,4 +664,53 @@ public function setStripWhitespace(bool $stripWhitespace): Compiler
605664

606665
return $this;
607666
}
667+
668+
protected function addReplaceVariable($safeString, $value)
669+
{
670+
$this->replaceVariables[$safeString] = $value;
671+
}
672+
673+
protected function addVariable($name, $value)
674+
{
675+
if (isset($this->variables[$name])) {
676+
throw new Exception("The variable $name is already registered.", 500);
677+
}
678+
679+
$this->variables[$name] = $value;
680+
}
681+
682+
protected function addVariableBlocks(string $string): string
683+
{
684+
$blocks = [];
685+
686+
foreach ($this->variables as $varName => $varValue) {
687+
$blocks[] = $this->builder->createMultilineVariable($varName, $varValue);
688+
}
689+
690+
return implode('', $blocks). $string;
691+
}
692+
693+
protected function handleDefaultSlot(DOMElement $node)
694+
{
695+
if ($node->nodeName !== 'slot') {
696+
return;
697+
}
698+
699+
$slotFallback = $node->hasChildNodes() ? $this->innerHtmlOfNode($node) : null;
700+
701+
if ($slotFallback) {
702+
$this->addVariable('slot_default_fallback', $slotFallback);
703+
$variable = $this->builder->createVariableOutput(Slot::SLOT_PREFIX.Slot::SLOT_DEFAULT_NAME, 'slot_default_fallback');
704+
}
705+
else {
706+
$variable = $this->builder->createVariableOutput(Slot::SLOT_PREFIX.Slot::SLOT_DEFAULT_NAME);
707+
}
708+
709+
$variableNode = $this->document->createTextNode($variable);
710+
711+
712+
$node->parentNode->insertBefore($variableNode, $node);
713+
$node->parentNode->removeChild($node);
714+
715+
}
608716
}

0 commit comments

Comments
 (0)