20
20
21
21
class Compiler
22
22
{
23
+ const DOT_DISABLED = 1 ;
24
+
23
25
use DyiadeTrait;
24
26
use InterpolationTrait;
25
27
@@ -205,12 +207,12 @@ protected function visitDyiade(Dyiade $dyiade, $indent)
205
207
return $ leftHand . ' ' . $ dyiade ->operator . ' ' . $ rightHand ;
206
208
}
207
209
208
- protected function mapNodesArray ($ array , $ indent , $ pattern = null , $ dotDisabled = false )
210
+ protected function mapNodesArray ($ array , $ indent , $ pattern = null , $ options = 0 )
209
211
{
210
212
$ visitNode = [$ this , 'visitNode ' ];
211
213
212
- return array_map (function ($ value ) use ($ visitNode , $ indent , $ pattern , $ dotDisabled ) {
213
- $ value = $ visitNode ($ value , $ indent , $ dotDisabled );
214
+ return array_map (function ($ value ) use ($ visitNode , $ indent , $ pattern , $ options ) {
215
+ $ value = $ visitNode ($ value , $ indent , $ options );
214
216
215
217
if ($ pattern ) {
216
218
$ value = sprintf ($ pattern , $ value );
@@ -220,17 +222,23 @@ protected function mapNodesArray($array, $indent, $pattern = null, $dotDisabled
220
222
}, $ array );
221
223
}
222
224
223
- protected function visitNodesArray ($ array , $ indent , $ glue = '' , $ pattern = null , $ dotDisabled = false )
225
+ protected function visitNodesArray ($ array , $ indent , $ glue = '' , $ pattern = null , $ options = 0 )
224
226
{
225
- return implode ($ glue , $ this ->mapNodesArray ($ array , $ indent , $ pattern , $ dotDisabled ));
227
+ return implode ($ glue , $ this ->mapNodesArray ($ array , $ indent , $ pattern , $ options ));
226
228
}
227
229
228
230
protected function visitFunctionCall (FunctionCall $ functionCall , $ indent )
229
231
{
230
232
$ function = $ functionCall ->function ;
231
233
$ arguments = $ functionCall ->arguments ;
232
234
$ applicant = $ functionCall ->applicant ;
233
- $ arguments = $ this ->visitNodesArray ($ arguments , $ indent , ', ' , null , $ function instanceof Variable && $ function ->name === 'isset ' );
235
+ $ arguments = $ this ->visitNodesArray (
236
+ $ arguments ,
237
+ $ indent ,
238
+ ', ' ,
239
+ null ,
240
+ $ function instanceof Variable && $ function ->name === 'isset ' ? static ::DOT_DISABLED : 0
241
+ );
234
242
$ dynamicCall = $ this ->visitNode ($ function , $ indent ) . '( ' . $ arguments . ') ' ;
235
243
236
244
if ($ function instanceof Variable && count ($ function ->children ) === 0 ) {
@@ -286,14 +294,14 @@ protected function visitInstruction(Instruction $group, $indent)
286
294
}, $ group ->instructions ));
287
295
}
288
296
289
- public function visitNode (Node $ node , $ indent , $ dotDisabled = false )
297
+ public function visitNode (Node $ node , $ indent , $ options = 0 )
290
298
{
291
299
$ method = preg_replace (
292
300
'/^(.+ \\\\)?([^ \\\\]+)$/ ' ,
293
301
'visit$2 ' ,
294
302
get_class ($ node )
295
303
);
296
- $ php = method_exists ($ this , $ method ) ? $ this ->$ method ($ node , $ indent , $ dotDisabled ) : '' ;
304
+ $ php = method_exists ($ this , $ method ) ? $ this ->$ method ($ node , $ indent , $ options ) : '' ;
297
305
298
306
if ($ node instanceof Value) {
299
307
$ php = $ node ->getBefore () . $ php . $ node ->getAfter ();
@@ -314,33 +322,41 @@ protected function visitTernary(Ternary $ternary, $indent)
314
322
' : ' . $ this ->visitNode ($ ternary ->falseValue , $ indent );
315
323
}
316
324
317
- protected function handleVariableChildren (DynamicValue $ dynamicValue , $ indent , $ php , $ dotDisabled = false )
325
+ protected function handleVariableChildren (DynamicValue $ dynamicValue , $ indent , $ php , $ options = 0 )
318
326
{
319
327
$ children = $ dynamicValue ->children ;
320
328
321
329
if (count ($ children )) {
322
- $ arguments = $ this ->mapNodesArray ($ children , $ indent , null , $ dotDisabled );
323
- array_unshift ($ arguments , $ php );
324
- $ dot = $ this ->engine ->getHelperName ('dot ' );
325
-
326
- if ($ dotDisabled ) {
327
- $ lastChild = end ($ children );
328
- $ dotChild = $ lastChild instanceof Constant && $ lastChild ->dotChild ;
329
- $ lastChild = array_pop ($ arguments );
330
- }
330
+ return $ this ->wrapVariableChildren ($ children , $ indent , $ php , $ options );
331
+ }
331
332
332
- $ php = $ this ->helperWrap ($ dot , $ arguments );
333
+ return $ php ;
334
+ }
333
335
334
- if ($ dotDisabled ) {
335
- $ pattern = $ dotChild ? '%s->{%s} ' : '%s[%s] ' ;
336
- $ php = sprintf ($ pattern , $ php , $ lastChild );
337
- }
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 );
338
354
}
339
355
340
356
return $ php ;
341
357
}
342
358
343
- protected function visitVariable (Variable $ variable , $ indent , $ dotDisabled = false )
359
+ protected function visitVariable (Variable $ variable , $ indent , $ options = 0 )
344
360
{
345
361
$ name = $ variable ->name ;
346
362
if (in_array ($ name , ['Math ' , 'RegExp ' ])) {
@@ -353,7 +369,7 @@ protected function visitVariable(Variable $variable, $indent, $dotDisabled = fal
353
369
$ name = '$ ' . $ name ;
354
370
}
355
371
356
- return $ this ->handleVariableChildren ($ variable , $ indent , $ name , $ dotDisabled );
372
+ return $ this ->handleVariableChildren ($ variable , $ indent , $ name , $ options );
357
373
}
358
374
359
375
public function compile (Block $ block , $ indent = '' )
0 commit comments