8
8
use DOMNode ;
9
9
use DOMText ;
10
10
use Exception ;
11
+ use Paneon \VueToTwig \Models \Component ;
12
+ use Paneon \VueToTwig \Models \Property ;
11
13
use Paneon \VueToTwig \Models \Replacements ;
14
+ use Paneon \VueToTwig \Models \Slot ;
12
15
use Paneon \VueToTwig \Utils \TwigBuilder ;
13
16
use Psr \Log \LoggerInterface ;
14
17
@@ -38,8 +41,11 @@ class Compiler
38
41
/** @var Property[] */
39
42
protected $ properties ;
40
43
41
- protected $ stripWhitespace = true ;
44
+ protected $ replaceVariables = [];
45
+
46
+ protected $ variables = [];
42
47
48
+ protected $ stripWhitespace = true ;
43
49
44
50
public function __construct (DOMDocument $ document , LoggerInterface $ logger )
45
51
{
@@ -86,6 +92,7 @@ public function convert(): string
86
92
$ resultNode = $ this ->convertNode ($ rootNode );
87
93
$ html = $ this ->document ->saveHTML ($ resultNode );
88
94
95
+ $ html = $ this ->addVariableBlocks ($ html );
89
96
$ html = $ this ->replacePlaceholders ($ html );
90
97
91
98
if ($ this ->stripWhitespace ) {
@@ -106,6 +113,7 @@ public function convertNode(DOMNode $node): DOMNode
106
113
case XML_COMMENT_NODE :
107
114
return $ node ;
108
115
case XML_TEXT_NODE :
116
+ /** @var DOMText $node */
109
117
return $ this ->handleTextNode ($ node );
110
118
case XML_ELEMENT_NODE :
111
119
/** @var DOMElement $node */
@@ -121,6 +129,11 @@ public function convertNode(DOMNode $node): DOMNode
121
129
$ this ->stripEventHandlers ($ node );
122
130
//$this->handleRawHtml($node, $data);
123
131
132
+ $ this ->handleDefaultSlot ($ node );
133
+
134
+ /*
135
+ * Registered Component
136
+ */
124
137
if (in_array ($ node ->nodeName , array_keys ($ this ->components ))) {
125
138
$ matchedComponent = $ this ->components [$ node ->nodeName ];
126
139
$ usedComponent = new Component ($ matchedComponent ->getName (), $ matchedComponent ->getPath ());
@@ -139,6 +152,24 @@ public function convertNode(DOMNode $node): DOMNode
139
152
}
140
153
}
141
154
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
+ */
142
173
$ include = $ this ->document ->createTextNode (
143
174
$ this ->builder ->createIncludePartial (
144
175
$ usedComponent ->getPath (),
@@ -147,6 +178,30 @@ public function convertNode(DOMNode $node): DOMNode
147
178
);
148
179
149
180
$ 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
150
205
$ node ->parentNode ->removeChild ($ node );
151
206
152
207
return $ node ;
@@ -519,6 +574,10 @@ protected function replacePlaceholders(string $string)
519
574
$ string = str_replace (Replacements::getSanitizedConstant ($ constant ), $ value , $ string );
520
575
}
521
576
577
+ foreach ($ this ->replaceVariables as $ safeString => $ value ) {
578
+ $ string = str_replace ($ safeString , $ value , $ string );
579
+ }
580
+
522
581
return $ string ;
523
582
}
524
583
@@ -605,4 +664,53 @@ public function setStripWhitespace(bool $stripWhitespace): Compiler
605
664
606
665
return $ this ;
607
666
}
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
+ }
608
716
}
0 commit comments