20
20
21
21
class Compiler
22
22
{
23
+ const DOT_DISABLED = 1 ;
24
+
23
25
use DyiadeTrait;
24
26
use InterpolationTrait;
25
27
@@ -99,7 +101,8 @@ protected function getBlockHead(Block $block, $indent)
99
101
' => $__current_value) ' ;
100
102
}
101
103
102
- return $ block ->type . ($ block ->value
104
+ return $ block ->type . (
105
+ $ block ->value
103
106
? ' ' . $ this ->visitNode ($ block ->value , $ indent )
104
107
: ''
105
108
);
@@ -204,12 +207,12 @@ protected function visitDyiade(Dyiade $dyiade, $indent)
204
207
return $ leftHand . ' ' . $ dyiade ->operator . ' ' . $ rightHand ;
205
208
}
206
209
207
- protected function mapNodesArray ($ array , $ indent , $ pattern = null )
210
+ protected function mapNodesArray ($ array , $ indent , $ pattern = null , $ options = 0 )
208
211
{
209
212
$ visitNode = [$ this , 'visitNode ' ];
210
213
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 );
213
216
214
217
if ($ pattern ) {
215
218
$ value = sprintf ($ pattern , $ value );
@@ -219,17 +222,23 @@ protected function mapNodesArray($array, $indent, $pattern = null)
219
222
}, $ array );
220
223
}
221
224
222
- protected function visitNodesArray ($ array , $ indent , $ glue = '' , $ pattern = null )
225
+ protected function visitNodesArray ($ array , $ indent , $ glue = '' , $ pattern = null , $ options = 0 )
223
226
{
224
- return implode ($ glue , $ this ->mapNodesArray ($ array , $ indent , $ pattern ));
227
+ return implode ($ glue , $ this ->mapNodesArray ($ array , $ indent , $ pattern, $ options ));
225
228
}
226
229
227
230
protected function visitFunctionCall (FunctionCall $ functionCall , $ indent )
228
231
{
229
232
$ function = $ functionCall ->function ;
230
233
$ arguments = $ functionCall ->arguments ;
231
234
$ 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
+ );
233
242
$ dynamicCall = $ this ->visitNode ($ function , $ indent ) . '( ' . $ arguments . ') ' ;
234
243
235
244
if ($ function instanceof Variable && count ($ function ->children ) === 0 ) {
@@ -272,9 +281,11 @@ protected function visitInstruction(Instruction $group, $indent)
272
281
$ value = $ visitNode ($ instruction , $ indent );
273
282
274
283
return $ indent .
275
- ($ instruction instanceof Block && $ instruction ->handleInstructions ()
284
+ (
285
+ $ instruction instanceof Block && $ instruction ->handleInstructions ()
276
286
? $ value
277
- : ($ isReturnPrepended && !preg_match ('/^\s*return(?![a-zA-Z0-9_])/ ' , $ value )
287
+ : (
288
+ $ isReturnPrepended && !preg_match ('/^\s*return(?![a-zA-Z0-9_])/ ' , $ value )
278
289
? ' return '
279
290
: ''
280
291
) . $ value . '; '
@@ -283,14 +294,14 @@ protected function visitInstruction(Instruction $group, $indent)
283
294
}, $ group ->instructions ));
284
295
}
285
296
286
- public function visitNode (Node $ node , $ indent )
297
+ public function visitNode (Node $ node , $ indent, $ options = 0 )
287
298
{
288
299
$ method = preg_replace (
289
300
'/^(.+ \\\\)?([^ \\\\]+)$/ ' ,
290
301
'visit$2 ' ,
291
302
get_class ($ node )
292
303
);
293
- $ php = method_exists ($ this , $ method ) ? $ this ->$ method ($ node , $ indent ) : '' ;
304
+ $ php = method_exists ($ this , $ method ) ? $ this ->$ method ($ node , $ indent, $ options ) : '' ;
294
305
295
306
if ($ node instanceof Value) {
296
307
$ php = $ node ->getBefore () . $ php . $ node ->getAfter ();
@@ -311,19 +322,41 @@ protected function visitTernary(Ternary $ternary, $indent)
311
322
' : ' . $ this ->visitNode ($ ternary ->falseValue , $ indent );
312
323
}
313
324
314
- protected function handleVariableChildren (DynamicValue $ dynamicValue , $ indent , $ php )
325
+ protected function handleVariableChildren (DynamicValue $ dynamicValue , $ indent , $ php, $ options = 0 )
315
326
{
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 );
321
331
}
322
332
323
333
return $ php ;
324
334
}
325
335
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 )
327
360
{
328
361
$ name = $ variable ->name ;
329
362
if (in_array ($ name , ['Math ' , 'RegExp ' ])) {
@@ -332,8 +365,11 @@ protected function visitVariable(Variable $variable, $indent)
332
365
if ($ variable ->scope ) {
333
366
$ name = '__let_ ' . spl_object_hash ($ variable ->scope ) . $ name ;
334
367
}
368
+ if (!$ this ->engine ->getOption ('ignoreDollarVariable ' ) || mb_substr ($ name , 0 , 1 ) !== '$ ' ) {
369
+ $ name = '$ ' . $ name ;
370
+ }
335
371
336
- return $ this ->handleVariableChildren ($ variable , $ indent , ' $ ' . $ name );
372
+ return $ this ->handleVariableChildren ($ variable , $ indent , $ name , $ options );
337
373
}
338
374
339
375
public function compile (Block $ block , $ indent = '' )
0 commit comments