15
15
class Compiler
16
16
{
17
17
18
- /** @var String [] */
18
+ /** @var Component [] */
19
19
protected $ components ;
20
20
21
21
/** @var DOMDocument */
@@ -40,6 +40,7 @@ public function __construct(DOMDocument $document, LoggerInterface $logger)
40
40
$ this ->document = $ document ;
41
41
$ this ->logger = $ logger ;
42
42
$ this ->lastCloseIf = null ;
43
+ $ this ->components = [];
43
44
$ this ->banner = [];
44
45
45
46
$ this ->logger ->debug ("\n--------- New Compiler Instance ---------- \n" );
@@ -100,6 +101,41 @@ public function convertNode(DOMNode $node): DOMNode
100
101
break ;
101
102
}
102
103
104
+ if (in_array ($ node ->nodeName , array_keys ($ this ->components ))) {
105
+ $ currentComponent = $ this ->components [$ node ->nodeName ];
106
+ $ this ->handleIf ($ node );
107
+ $ this ->handleFor ($ node );
108
+ if ($ node ->hasAttributes ()) {
109
+ /** @var DOMAttr $attribute */
110
+ foreach ($ node ->attributes as $ attribute ) {
111
+ if (strpos ($ attribute ->name , 'v-bind: ' ) === 0 || strpos ($ attribute ->name , ': ' ) === 0 ) {
112
+ $ currentComponent ->addProperty ($ attribute ->name , $ attribute ->value , true );
113
+ } else {
114
+ $ currentComponent ->addProperty ($ attribute ->name , $ attribute ->value , false );
115
+ }
116
+ }
117
+ }
118
+ $ props = [];
119
+ if (count ($ currentComponent ->getProperties ()) > 0 ) {
120
+ foreach ($ currentComponent ->getProperties () as $ property ) {
121
+ if ($ property ->isBinding ()){
122
+ $ propName = substr ($ property ->getName (), 1 ); //delete ':'
123
+ $ props [] = $ propName .': ' .$ property ->getValue ();
124
+ } else {
125
+ $ props [] = $ property ->getName ().': " ' .$ property ->getValue ().'" ' ;
126
+ }
127
+ }
128
+ }
129
+ $ propsString = '' ;
130
+ if (count ($ props ) > 0 ) {
131
+ $ propsString = 'with { ' .implode (', ' , $ props ).' } ' ;
132
+ }
133
+ $ include = $ this ->document ->createTextNode ('{% include " ' .$ currentComponent ->getPath ().'" ' .$ propsString .'%} ' );
134
+ $ node ->parentNode ->insertBefore ($ include , $ node );
135
+ $ node ->parentNode ->removeChild ($ node );
136
+ return $ node ;
137
+ }
138
+
103
139
$ this ->stripEventHandlers ($ node );
104
140
$ this ->handleFor ($ node );
105
141
//$this->handleRawHtml($node, $data);
@@ -364,6 +400,11 @@ protected function replacePlaceholders(string $string)
364
400
return $ string ;
365
401
}
366
402
403
+ public function registerComponent (string $ componentName , string $ componentPath )
404
+ {
405
+ $ this ->components [strtolower ($ componentName )] = new Component ($ componentName , $ componentPath );
406
+ }
407
+
367
408
protected function addSingleLineBanner (string $ html )
368
409
{
369
410
return $ this ->builder ->createComment (implode ('' , $ this ->banner ))."\n" .$ html ;
0 commit comments