2020
2121class Compiler
2222{
23+ const DOT_DISABLED = 1 ;
24+
2325 use DyiadeTrait;
2426 use InterpolationTrait;
2527
@@ -99,7 +101,8 @@ protected function getBlockHead(Block $block, $indent)
99101 ' => $__current_value) ' ;
100102 }
101103
102- return $ block ->type . ($ block ->value
104+ return $ block ->type . (
105+ $ block ->value
103106 ? ' ' . $ this ->visitNode ($ block ->value , $ indent )
104107 : ''
105108 );
@@ -204,12 +207,12 @@ protected function visitDyiade(Dyiade $dyiade, $indent)
204207 return $ leftHand . ' ' . $ dyiade ->operator . ' ' . $ rightHand ;
205208 }
206209
207- protected function mapNodesArray ($ array , $ indent , $ pattern = null )
210+ protected function mapNodesArray ($ array , $ indent , $ pattern = null , $ options = 0 )
208211 {
209212 $ visitNode = [$ this , 'visitNode ' ];
210213
211- return array_map (function ($ value ) use ($ visitNode , $ indent , $ pattern ) {
212- $ value = $ visitNode ($ value , $ indent );
214+ return array_map (function ($ value ) use ($ visitNode , $ indent , $ pattern, $ options ) {
215+ $ value = $ visitNode ($ value , $ indent, $ options );
213216
214217 if ($ pattern ) {
215218 $ value = sprintf ($ pattern , $ value );
@@ -219,17 +222,23 @@ protected function mapNodesArray($array, $indent, $pattern = null)
219222 }, $ array );
220223 }
221224
222- protected function visitNodesArray ($ array , $ indent , $ glue = '' , $ pattern = null )
225+ protected function visitNodesArray ($ array , $ indent , $ glue = '' , $ pattern = null , $ options = 0 )
223226 {
224- return implode ($ glue , $ this ->mapNodesArray ($ array , $ indent , $ pattern ));
227+ return implode ($ glue , $ this ->mapNodesArray ($ array , $ indent , $ pattern, $ options ));
225228 }
226229
227230 protected function visitFunctionCall (FunctionCall $ functionCall , $ indent )
228231 {
229232 $ function = $ functionCall ->function ;
230233 $ arguments = $ functionCall ->arguments ;
231234 $ applicant = $ functionCall ->applicant ;
232- $ arguments = $ this ->visitNodesArray ($ arguments , $ indent , ', ' );
235+ $ arguments = $ this ->visitNodesArray (
236+ $ arguments ,
237+ $ indent ,
238+ ', ' ,
239+ null ,
240+ $ function instanceof Variable && $ function ->name === 'isset ' ? static ::DOT_DISABLED : 0
241+ );
233242 $ dynamicCall = $ this ->visitNode ($ function , $ indent ) . '( ' . $ arguments . ') ' ;
234243
235244 if ($ function instanceof Variable && count ($ function ->children ) === 0 ) {
@@ -272,9 +281,11 @@ protected function visitInstruction(Instruction $group, $indent)
272281 $ value = $ visitNode ($ instruction , $ indent );
273282
274283 return $ indent .
275- ($ instruction instanceof Block && $ instruction ->handleInstructions ()
284+ (
285+ $ instruction instanceof Block && $ instruction ->handleInstructions ()
276286 ? $ value
277- : ($ isReturnPrepended && !preg_match ('/^\s*return(?![a-zA-Z0-9_])/ ' , $ value )
287+ : (
288+ $ isReturnPrepended && !preg_match ('/^\s*return(?![a-zA-Z0-9_])/ ' , $ value )
278289 ? ' return '
279290 : ''
280291 ) . $ value . '; '
@@ -283,14 +294,14 @@ protected function visitInstruction(Instruction $group, $indent)
283294 }, $ group ->instructions ));
284295 }
285296
286- public function visitNode (Node $ node , $ indent )
297+ public function visitNode (Node $ node , $ indent, $ options = 0 )
287298 {
288299 $ method = preg_replace (
289300 '/^(.+ \\\\)?([^ \\\\]+)$/ ' ,
290301 'visit$2 ' ,
291302 get_class ($ node )
292303 );
293- $ php = method_exists ($ this , $ method ) ? $ this ->$ method ($ node , $ indent ) : '' ;
304+ $ php = method_exists ($ this , $ method ) ? $ this ->$ method ($ node , $ indent, $ options ) : '' ;
294305
295306 if ($ node instanceof Value) {
296307 $ php = $ node ->getBefore () . $ php . $ node ->getAfter ();
@@ -311,19 +322,41 @@ protected function visitTernary(Ternary $ternary, $indent)
311322 ' : ' . $ this ->visitNode ($ ternary ->falseValue , $ indent );
312323 }
313324
314- protected function handleVariableChildren (DynamicValue $ dynamicValue , $ indent , $ php )
325+ protected function handleVariableChildren (DynamicValue $ dynamicValue , $ indent , $ php, $ options = 0 )
315326 {
316- if (count ($ dynamicValue ->children )) {
317- $ arguments = $ this ->mapNodesArray ($ dynamicValue ->children , $ indent );
318- array_unshift ($ arguments , $ php );
319- $ dot = $ this ->engine ->getHelperName ('dot ' );
320- $ php = $ this ->helperWrap ($ dot , $ arguments );
327+ $ children = $ dynamicValue ->children ;
328+
329+ if (count ($ children )) {
330+ return $ this ->wrapVariableChildren ($ children , $ indent , $ php , $ options );
321331 }
322332
323333 return $ php ;
324334 }
325335
326- protected function visitVariable (Variable $ variable , $ indent )
336+ protected function wrapVariableChildren ($ children , $ indent , $ php , $ options )
337+ {
338+ $ arguments = $ this ->mapNodesArray ($ children , $ indent );
339+ array_unshift ($ arguments , $ php );
340+ $ dot = $ this ->engine ->getHelperName ('dot ' );
341+ $ dotDisabled = $ options & static ::DOT_DISABLED ;
342+
343+ if ($ dotDisabled ) {
344+ $ lastChild = end ($ children );
345+ $ dotChild = $ lastChild instanceof Constant && $ lastChild ->dotChild ;
346+ $ lastChild = array_pop ($ arguments );
347+ }
348+
349+ $ php = $ this ->helperWrap ($ dot , $ arguments );
350+
351+ if ($ dotDisabled ) {
352+ $ pattern = $ dotChild ? '%s->{%s} ' : '%s[%s] ' ;
353+ $ php = sprintf ($ pattern , $ php , $ lastChild );
354+ }
355+
356+ return $ php ;
357+ }
358+
359+ protected function visitVariable (Variable $ variable , $ indent , $ options = 0 )
327360 {
328361 $ name = $ variable ->name ;
329362 if (in_array ($ name , ['Math ' , 'RegExp ' ])) {
@@ -332,8 +365,11 @@ protected function visitVariable(Variable $variable, $indent)
332365 if ($ variable ->scope ) {
333366 $ name = '__let_ ' . spl_object_hash ($ variable ->scope ) . $ name ;
334367 }
368+ if (!$ this ->engine ->getOption ('ignoreDollarVariable ' ) || mb_substr ($ name , 0 , 1 ) !== '$ ' ) {
369+ $ name = '$ ' . $ name ;
370+ }
335371
336- return $ this ->handleVariableChildren ($ variable , $ indent , ' $ ' . $ name );
372+ return $ this ->handleVariableChildren ($ variable , $ indent , $ name , $ options );
337373 }
338374
339375 public function compile (Block $ block , $ indent = '' )
0 commit comments