@@ -105,7 +105,7 @@ public static function expired($view, $path)
105
105
/**
106
106
* Compiles the specified file containing Blade pseudo-code into valid PHP.
107
107
*
108
- * @param string $view
108
+ * @param string $path
109
109
* @return string
110
110
*/
111
111
public static function compile ($ view )
@@ -149,33 +149,31 @@ protected static function compile_layouts($value)
149
149
}
150
150
151
151
// First we'll split out the lines of the template so we can get the
152
- // layout from the top of the template. By convention, it must be
152
+ // layout from the top of the template. By convention it must be
153
153
// located on the first line of the template contents.
154
- preg_replace_callback (
155
- '/^@layout(\s*?\(.+?\))(\r?\n)?/ ' ,
156
- function ($ matches ) use (&$ value )
157
- {
158
- $ value = substr ( $ value , strlen ( $ matches [0 ] ) ).CRLF .'@include ' .$ matches [1 ];
159
- },
160
- $ value
161
- );
154
+ $ lines = preg_split ("/( \r? \n)/ " , $ value );
162
155
163
- return $ value ;
156
+ $ pattern = static ::matcher ('layout ' );
157
+
158
+ $ lines [] = preg_replace ($ pattern , '$1@include$2 ' , $ lines [0 ]);
159
+
160
+ // We will add a "render" statement to the end of the templates and
161
+ // then slice off the "@layout" shortcut from the start so the
162
+ // sections register before the parent template renders.
163
+ return implode (CRLF , array_slice ($ lines , 1 ));
164
164
}
165
165
166
166
/**
167
167
* Extract a variable value out of a Blade expression.
168
168
*
169
169
* @param string $value
170
- * @param string $expression
171
170
* @return string
172
171
*/
173
172
protected static function extract ($ value , $ expression )
174
173
{
175
- if ( preg_match ("/@layout\s*?\(\s*?'(.+?)'\s*?\)/ " , $ value , $ matches ))
176
- {
177
- return trim ( $ matches [1 ] );
178
- }
174
+ preg_match ('/@layout(\s*\(.*\))(\s*)/ ' , $ value , $ matches );
175
+
176
+ return str_replace (array ("(' " , "') " ), '' , $ matches [1 ]);
179
177
}
180
178
181
179
/**
@@ -186,9 +184,9 @@ protected static function extract($value, $expression)
186
184
*/
187
185
protected static function compile_comments ($ value )
188
186
{
189
- $ value = preg_replace ('/\{\{--(.*?) --\}\}/ ' , "<?php // $1 ?> " , $ value );
190
-
191
- return preg_replace ('/\{\{--(. *?)--\}\}/s ' , "<?php /* ?>$1<?php */ ?> " , $ value );
187
+ $ value = preg_replace ('/\{\{--(.+?)( --\}\})?\n / ' , "<?php // $1 ?> " , $ value );
188
+
189
+ return preg_replace ('/\{\{--((.|\s) *?)--\}\}/ ' , "<?php /* $1 */ ?> \n " , $ value );
192
190
}
193
191
194
192
/**
@@ -199,7 +197,7 @@ protected static function compile_comments($value)
199
197
*/
200
198
protected static function compile_echos ($ value )
201
199
{
202
- return preg_replace ('/\{\{(.+?)\}\}/s ' , '<?php echo $1; ?> ' , $ value );
200
+ return preg_replace ('/\{\{(.+?)\}\}/ ' , '<?php echo $1; ?> ' , $ value );
203
201
}
204
202
205
203
/**
@@ -210,21 +208,27 @@ protected static function compile_echos($value)
210
208
*/
211
209
protected static function compile_forelse ($ value )
212
210
{
213
- preg_match_all ('/@forelse\s*?\(\s*?\$(.+?)\s*?as\s*?\$(.+?)\s*?\)/ ' , $ value , $ matches , PREG_SET_ORDER );
214
-
215
- if ( count ($ matches ) < 1 ) return $ value ;
211
+ preg_match_all ('/(\s*)@forelse(\s*\(.*\))(\s*)/ ' , $ value , $ matches );
216
212
217
- foreach ($ matches as $ forelse )
213
+ foreach ($ matches[ 0 ] as $ forelse )
218
214
{
215
+ preg_match ('/\s*\(\s*(\S*)\s/ ' , $ forelse , $ variable );
216
+
219
217
// Once we have extracted the variable being looped against, we can add
220
218
// an if statement to the start of the loop that checks if the count
221
219
// of the variable being looped against is greater than zero.
222
- $ replace = '<?php if (count($ ' .$ forelse [1 ].') > 0): foreach ($ ' .$ forelse [1 ].' as $ ' .$ forelse [2 ].'): ?> ' ;
220
+ $ if = "<?php if (count( {$ variable [1 ]}) > 0): ?> " ;
221
+
222
+ $ search = '/(\s*)@forelse(\s*\(.*\))/ ' ;
223
+
224
+ $ replace = '$1 ' .$ if .'<?php foreach$2: ?> ' ;
225
+
226
+ $ blade = preg_replace ($ search , $ replace , $ forelse );
223
227
224
228
// Finally, once we have the check prepended to the loop we'll replace
225
229
// all instances of this forelse syntax in the view content of the
226
230
// view being compiled to Blade syntax with real PHP syntax.
227
- $ value = str_replace ($ forelse[ 0 ] , $ replace , $ value );
231
+ $ value = str_replace ($ forelse , $ blade , $ value );
228
232
}
229
233
230
234
return $ value ;
@@ -238,7 +242,7 @@ protected static function compile_forelse($value)
238
242
*/
239
243
protected static function compile_empty ($ value )
240
244
{
241
- return str_replace ('@empty ' , '<?php endforeach; else: ?> ' , $ value );
245
+ return str_replace ('@empty ' , '<?php endforeach; ?><?php else: ?> ' , $ value );
242
246
}
243
247
244
248
/**
@@ -260,42 +264,9 @@ protected static function compile_endforelse($value)
260
264
*/
261
265
protected static function compile_structure_openings ($ value )
262
266
{
263
- preg_replace_callback (
264
- '/@(if|elseif|foreach|for|while)(\s*?)(\([^\n\r\t]+\))/ ' ,
265
- function ($ matches ) use (&$ value )
266
- {
267
- if (count ( $ matches ) === 4 )
268
- {
269
- $ open = 0 ;
270
- $ close = 0 ;
271
- $ cut = 0 ;
272
- $ len = strlen ($ matches [3 ]);
273
- for ($ i = 0 ; $ i < $ len ; $ i ++)
274
- {
275
- if ($ matches [3 ][$ i ] === '( ' )
276
- {
277
- $ open ++;
278
- }
279
- if ($ matches [3 ][$ i ] === ') ' )
280
- {
281
- $ close ++;
282
- }
283
- if ($ open !== 0 && ($ open === $ close ))
284
- {
285
- break ;
286
- }
287
- }
288
- $ condition = substr ($ matches [3 ], 0 , ($ i + 1 ));
289
- $ value = str_replace (
290
- '@ ' .$ matches [1 ].$ matches [2 ].$ condition ,
291
- '<?php ' .$ matches [1 ].$ condition .': ?> ' ,
292
- $ value
293
- );
294
- }
295
- },
296
- $ value
297
- );
298
- return $ value ;
267
+ $ pattern = '/(\s*)@(if|elseif|foreach|for|while)(\s*\(.*\))/ ' ;
268
+
269
+ return preg_replace ($ pattern , '$1<?php $2$3: ?> ' , $ value );
299
270
}
300
271
301
272
/**
@@ -306,9 +277,9 @@ function($matches) use (&$value)
306
277
*/
307
278
protected static function compile_structure_closings ($ value )
308
279
{
309
- $ pattern = '/@(endif|endforeach|endfor|endwhile|break|continue )/ ' ;
280
+ $ pattern = '/(\s*) @(endif|endforeach|endfor|endwhile)(\s* )/ ' ;
310
281
311
- return preg_replace ($ pattern , '<?php $1 ; ?> ' , $ value );
282
+ return preg_replace ($ pattern , '$1 <?php $2 ; ?>$3 ' , $ value );
312
283
}
313
284
314
285
/**
@@ -319,7 +290,7 @@ protected static function compile_structure_closings($value)
319
290
*/
320
291
protected static function compile_else ($ value )
321
292
{
322
- return str_replace ( ' @ else ' , '<?php else : ?> ' , $ value );
293
+ return preg_replace ( ' /(\s*)@( else)(\s*)/ ' , '$1 <?php $2 : ?>$3 ' , $ value );
323
294
}
324
295
325
296
/**
@@ -330,9 +301,9 @@ protected static function compile_else($value)
330
301
*/
331
302
protected static function compile_unless ($ value )
332
303
{
333
- $ pattern = static :: matcher ( ' unless ' ) ;
304
+ $ pattern = ' /(\s*)@ unless(\s*\(.*\))/ ' ;
334
305
335
- return preg_replace ($ pattern , '<?php if( ! ($1 )): ?> ' , $ value );
306
+ return preg_replace ($ pattern , '$1 <?php if( ! ($2 )): ?> ' , $ value );
336
307
}
337
308
338
309
/**
@@ -356,7 +327,7 @@ protected static function compile_includes($value)
356
327
{
357
328
$ pattern = static ::matcher ('include ' );
358
329
359
- return preg_replace ($ pattern , '<?php echo view$1 ->with(get_defined_vars())->render(); ?> ' , $ value );
330
+ return preg_replace ($ pattern , '$1 <?php echo view$2 ->with(get_defined_vars())->render(); ?> ' , $ value );
360
331
}
361
332
362
333
/**
@@ -369,7 +340,7 @@ protected static function compile_render($value)
369
340
{
370
341
$ pattern = static ::matcher ('render ' );
371
342
372
- return preg_replace ($ pattern , '<?php echo render$1 ; ?> ' , $ value );
343
+ return preg_replace ($ pattern , '$1 <?php echo render$2 ; ?> ' , $ value );
373
344
}
374
345
375
346
/**
@@ -382,7 +353,7 @@ protected static function compile_render_each($value)
382
353
{
383
354
$ pattern = static ::matcher ('render_each ' );
384
355
385
- return preg_replace ($ pattern , '<?php echo render_each$1 ; ?> ' , $ value );
356
+ return preg_replace ($ pattern , '$1 <?php echo render_each$2 ; ?> ' , $ value );
386
357
}
387
358
388
359
/**
@@ -397,18 +368,19 @@ protected static function compile_yields($value)
397
368
{
398
369
$ pattern = static ::matcher ('yield ' );
399
370
400
- return preg_replace ($ pattern , '<?php echo \\Laravel \\Section::yield$1 ; ?> ' , $ value );
371
+ return preg_replace ($ pattern , '$1 <?php echo \\Laravel \\Section::yield$2 ; ?> ' , $ value );
401
372
}
402
373
403
374
/**
404
375
* Rewrites Blade yield section statements into valid PHP.
405
376
*
406
- * @param string $value
407
377
* @return string
408
378
*/
409
379
protected static function compile_yield_sections ($ value )
410
380
{
411
- return str_replace ('@yield_section ' , '<?php echo \\Laravel \\Section::yield_section(); ?> ' , $ value );
381
+ $ replace = '<?php echo \\Laravel \\Section::yield_section(); ?> ' ;
382
+
383
+ return str_replace ('@yield_section ' , $ replace , $ value );
412
384
}
413
385
414
386
/**
@@ -423,7 +395,7 @@ protected static function compile_section_start($value)
423
395
{
424
396
$ pattern = static ::matcher ('section ' );
425
397
426
- return preg_replace ($ pattern , '<?php \\Laravel \\Section::start$1 ; ?> ' , $ value );
398
+ return preg_replace ($ pattern , '$1 <?php \\Laravel \\Section::start$2 ; ?> ' , $ value );
427
399
}
428
400
429
401
/**
@@ -436,7 +408,7 @@ protected static function compile_section_start($value)
436
408
*/
437
409
protected static function compile_section_end ($ value )
438
410
{
439
- return str_replace ( ' @endsection ' , '<?php \\Laravel \\Section::stop(); ?> ' , $ value );
411
+ return preg_replace ( ' / @endsection/ ' , '<?php \\Laravel \\Section::stop(); ?> ' , $ value );
440
412
}
441
413
442
414
/**
@@ -453,7 +425,7 @@ protected static function compile_extensions($value)
453
425
}
454
426
455
427
return $ value ;
456
- }
428
+ }
457
429
458
430
/**
459
431
* Get the regular expression for a generic Blade function.
@@ -463,18 +435,18 @@ protected static function compile_extensions($value)
463
435
*/
464
436
public static function matcher ($ function )
465
437
{
466
- return '/@ ' .$ function .'\s*?( \(.+? \))/ ' ;
438
+ return '/(\s*) @ ' .$ function .'( \s*\(.* \))/ ' ;
467
439
}
468
440
469
441
/**
470
442
* Get the fully qualified path for a compiled view.
471
443
*
472
- * @param string $path
444
+ * @param string $view
473
445
* @return string
474
446
*/
475
447
public static function compiled ($ path )
476
448
{
477
449
return path ('storage ' ).'views/ ' .md5 ($ path );
478
450
}
479
451
480
- }
452
+ }
0 commit comments