@@ -13,6 +13,7 @@ class Parser
1313 * Leaf Veins config
1414 */
1515 protected $ config = [];
16+ public static $ configuration = [];
1617
1718 /**
1819 * Built in tags
@@ -78,8 +79,12 @@ public function __construct(array $config)
7879 $ this ->config = $ config ;
7980 }
8081
81- public static function checkTemplate (array $ config , string $ template )
82+ public static function checkTemplate ($ config , string $ template )
8283 {
84+ if (is_string ($ config )) {
85+ $ config = json_decode ($ config , true );
86+ }
87+
8388 if (strpos ($ template , '.vein.html ' ) === false ) {
8489 $ template .= '.vein.html ' ;
8590 }
@@ -99,13 +104,13 @@ public static function checkTemplate(array $config, string $template)
99104 ) {
100105 $ parser = new self ($ config );
101106
102- return $ parser ->parse ($ template , $ parsedTemplate );
107+ return $ parser ->parse ($ template , $ parsedTemplate, $ config );
103108 }
104109
105110 return $ parsedTemplate ;
106111 }
107112
108- public function parse (string $ template , string $ parsedTemplate ): string
113+ public function parse (string $ template , string $ parsedTemplate, array $ config ): string
109114 {
110115 $ template = file_get_contents ($ template );
111116 $ templateDir = dirname ($ template );
@@ -117,26 +122,24 @@ public function parse(string $template, string $parsedTemplate): string
117122 }
118123
119124 $ keys = array_keys ($ this ->config ['customTags ' ]);
120- $ tagSplit + = array_merge ($ tagSplit , $ keys );
125+ $ tagSplit = array_merge ($ tagSplit , $ keys );
121126
122127 if ($ this ->config ['removeComments ' ]) {
123128 $ template = preg_replace ('/<!--(.*)-->/Uis ' , '' , $ template );
124129 }
125130
126- //split the code with the tags regexp
131+ // split the code with the tags regexp
127132 $ codeSplit = preg_split ("/ " . implode ("| " , $ tagSplit ) . "/ " , $ template , -1 , PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
128133
129- //variables initialization
134+ // variables initialization
130135 $ parsedCode = $ commentIsOpen = $ ignoreIsOpen = null ;
131136 $ openIf = $ loopLevel = 0 ;
132137
133138 // if the template is not empty
134139 if ($ codeSplit ) {
135-
136- //read all parsed code
140+ // read all parsed code
137141 foreach ($ codeSplit as $ html ) {
138-
139- //close ignore tag
142+ // close ignore tag
140143 if (!$ commentIsOpen && preg_match ($ tagMatch ['ignore_close ' ], $ html )) {
141144 $ ignoreIsOpen = false ;
142145 }
@@ -168,29 +171,17 @@ public function parse(string $template, string $parsedTemplate): string
168171
169172 //include tag
170173 elseif (preg_match ($ tagMatch ['include ' ], $ html , $ matches )) {
171-
172- //get the folder of the actual template
173- if (substr ($ templateDir , 0 , strlen ($ this ->config ['templateDir ' ])) == $ this ->config ['templateDir ' ]) {
174- $ templateDir = substr ($ templateDir , strlen ($ this ->config ['templateDir ' ]));
175- }
176-
177- //get the included template
174+ // get the included template
178175 if (strpos ($ matches [1 ], '$ ' ) !== false ) {
179- $ includeTemplate = " ' $ templateDir '. " . $ this ->varReplace ($ matches [1 ], $ loopLevel );
176+ $ includeTemplate = $ this ->varReplace ($ matches [1 ], $ loopLevel );
180177 } else {
181- $ includeTemplate = $ templateDir . $ this ->varReplace ($ matches [1 ], $ loopLevel );
178+ $ includeTemplate = $ this ->varReplace ($ matches [1 ], $ loopLevel );
182179 }
183180
184181 // reduce the path
185182 $ includeTemplate = Parser::reducePath ($ includeTemplate );
186183
187- if (strpos ($ matches [1 ], '$ ' ) !== false ) {
188- //dynamic include
189- $ parsedCode .= '<?php require $this->checkTemplate( ' . $ includeTemplate . ');?> ' ;
190- } else {
191- //dynamic include
192- $ parsedCode .= '<?php require $this->checkTemplate(" ' . $ includeTemplate . '");?> ' ;
193- }
184+ $ parsedCode .= '<?php require \Leaf\Veins\Parser::checkTemplate(" ' . str_replace ('" ' , '\" ' , json_encode ($ config )) . '", " ' . $ includeTemplate . '");?> ' ;
194185 }
195186
196187 //loop
0 commit comments