1: <?php namespace Laravel; use FilesystemIterator as fIterator; use Closure;
2:
3: class Blade {
4:
5: /**
6: * All of the compiler functions used by Blade.
7: *
8: * @var array
9: */
10: protected static $compilers = array(
11: 'extensions',
12: 'layouts',
13: 'comments',
14: 'echos',
15: 'forelse',
16: 'empty',
17: 'endforelse',
18: 'structure_openings',
19: 'structure_closings',
20: 'else',
21: 'unless',
22: 'endunless',
23: 'includes',
24: 'render_each',
25: 'render',
26: 'yields',
27: 'yield_sections',
28: 'section_start',
29: 'section_end',
30: );
31:
32: /**
33: * An array of user defined compilers.
34: *
35: * @var array
36: */
37: protected static $extensions = array();
38:
39: /**
40: * Register the Blade view engine with Laravel.
41: *
42: * @return void
43: */
44: public static function sharpen()
45: {
46: Event::listen(View::engine, function($view)
47: {
48: // The Blade view engine should only handle the rendering of views which
49: // end with the Blade extension. If the given view does not, we will
50: // return false so the View can be rendered as normal.
51: if ( ! str_contains($view->path, BLADE_EXT))
52: {
53: return;
54: }
55:
56: $compiled = Blade::compiled($view->path);
57:
58: // If the view doesn't exist or has been modified since the last time it
59: // was compiled, we will recompile the view into pure PHP from it's
60: // Blade representation, writing it to cached storage.
61: if ( ! file_exists($compiled) or Blade::expired($view->view, $view->path))
62: {
63: file_put_contents($compiled, Blade::compile($view));
64: }
65:
66: $view->path = $compiled;
67:
68: // Once the view has been compiled, we can simply set the path to the
69: // compiled view on the view instance and call the typical "get"
70: // method on the view to evaluate the compiled PHP view.
71: return ltrim($view->get());
72: });
73: }
74:
75: /**
76: * Register a custom Blade compiler.
77: *
78: * <code>
79: * Blade::extend(function($view)
80: * {
81: * return str_replace('foo', 'bar', $view);
82: * });
83: * </code>
84: *
85: * @param Closure $compiler
86: * @return void
87: */
88: public static function extend(Closure $compiler)
89: {
90: static::$extensions[] = $compiler;
91: }
92:
93: /**
94: * Determine if a view is "expired" and needs to be re-compiled.
95: *
96: * @param string $view
97: * @param string $path
98: * @return bool
99: */
100: public static function expired($view, $path)
101: {
102: return filemtime($path) > filemtime(static::compiled($path));
103: }
104:
105: /**
106: * Compiles the specified file containing Blade pseudo-code into valid PHP.
107: *
108: * @param string $path
109: * @return string
110: */
111: public static function compile($view)
112: {
113: return static::compile_string(file_get_contents($view->path), $view);
114: }
115:
116: /**
117: * Compiles the given string containing Blade pseudo-code into valid PHP.
118: *
119: * @param string $value
120: * @param View $view
121: * @return string
122: */
123: public static function compile_string($value, $view = null)
124: {
125: foreach (static::$compilers as $compiler)
126: {
127: $method = "compile_{$compiler}";
128:
129: $value = static::$method($value, $view);
130: }
131:
132: return $value;
133: }
134:
135: /**
136: * Rewrites Blade "@layout" expressions into valid PHP.
137: *
138: * @param string $value
139: * @return string
140: */
141: protected static function compile_layouts($value)
142: {
143: // If the Blade template is not using "layouts", we'll just return it
144: // unchanged since there is nothing to do with layouts and we will
145: // just let the other Blade compilers handle the rest.
146: if ( ! starts_with($value, '@layout'))
147: {
148: return $value;
149: }
150:
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
153: // located on the first line of the template contents.
154: $lines = preg_split("/(\r?\n)/", $value);
155:
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: }
165:
166: /**
167: * Extract a variable value out of a Blade expression.
168: *
169: * @param string $value
170: * @return string
171: */
172: protected static function extract($value, $expression)
173: {
174: preg_match('/@layout(\s*\(.*\))(\s*)/', $value, $matches);
175:
176: return str_replace(array("('", "')"), '', $matches[1]);
177: }
178:
179: /**
180: * Rewrites Blade comments into PHP comments.
181: *
182: * @param string $value
183: * @return string
184: */
185: protected static function compile_comments($value)
186: {
187: $value = preg_replace('/\{\{--(.+?)(--\}\})?\n/', "<?php // $1 ?>", $value);
188:
189: return preg_replace('/\{\{--((.|\s)*?)--\}\}/', "<?php /* $1 */ ?>\n", $value);
190: }
191:
192: /**
193: * Rewrites Blade echo statements into PHP echo statements.
194: *
195: * @param string $value
196: * @return string
197: */
198: protected static function compile_echos($value)
199: {
200: $value = preg_replace('/\{\{\{(.+?)\}\}\}/', '<?php echo HTML::entities($1); ?>', $value);
201:
202: return preg_replace('/\{\{(.+?)\}\}/', '<?php echo $1; ?>', $value);
203: }
204:
205: /**
206: * Rewrites Blade "for else" statements into valid PHP.
207: *
208: * @param string $value
209: * @return string
210: */
211: protected static function compile_forelse($value)
212: {
213: preg_match_all('/(\s*)@forelse(\s*\(.*\))(\s*)/', $value, $matches);
214:
215: foreach ($matches[0] as $forelse)
216: {
217: preg_match('/\s*\(\s*(\S*)\s/', $forelse, $variable);
218:
219: // Once we have extracted the variable being looped against, we can add
220: // an if statement to the start of the loop that checks if the count
221: // of the variable being looped against is greater than zero.
222: $if = "<?php if (count({$variable[1]}) > 0): ?>";
223:
224: $search = '/(\s*)@forelse(\s*\(.*\))/';
225:
226: $replace = '$1'.$if.'<?php foreach$2: ?>';
227:
228: $blade = preg_replace($search, $replace, $forelse);
229:
230: // Finally, once we have the check prepended to the loop we'll replace
231: // all instances of this forelse syntax in the view content of the
232: // view being compiled to Blade syntax with real PHP syntax.
233: $value = str_replace($forelse, $blade, $value);
234: }
235:
236: return $value;
237: }
238:
239: /**
240: * Rewrites Blade "empty" statements into valid PHP.
241: *
242: * @param string $value
243: * @return string
244: */
245: protected static function compile_empty($value)
246: {
247: return str_replace('@empty', '<?php endforeach; ?><?php else: ?>', $value);
248: }
249:
250: /**
251: * Rewrites Blade "forelse" endings into valid PHP.
252: *
253: * @param string $value
254: * @return string
255: */
256: protected static function compile_endforelse($value)
257: {
258: return str_replace('@endforelse', '<?php endif; ?>', $value);
259: }
260:
261: /**
262: * Rewrites Blade structure openings into PHP structure openings.
263: *
264: * @param string $value
265: * @return string
266: */
267: protected static function compile_structure_openings($value)
268: {
269: $pattern = '/(\s*)@(if|elseif|foreach|for|while)(\s*\(.*\))/';
270:
271: return preg_replace($pattern, '$1<?php $2$3: ?>', $value);
272: }
273:
274: /**
275: * Rewrites Blade structure closings into PHP structure closings.
276: *
277: * @param string $value
278: * @return string
279: */
280: protected static function compile_structure_closings($value)
281: {
282: $pattern = '/(\s*)@(endif|endforeach|endfor|endwhile)(\s*)/';
283:
284: return preg_replace($pattern, '$1<?php $2; ?>$3', $value);
285: }
286:
287: /**
288: * Rewrites Blade else statements into PHP else statements.
289: *
290: * @param string $value
291: * @return string
292: */
293: protected static function compile_else($value)
294: {
295: return preg_replace('/(\s*)@(else)(\s*)/', '$1<?php $2: ?>$3', $value);
296: }
297:
298: /**
299: * Rewrites Blade "unless" statements into valid PHP.
300: *
301: * @param string $value
302: * @return string
303: */
304: protected static function compile_unless($value)
305: {
306: $pattern = '/(\s*)@unless(\s*\(.*\))/';
307:
308: return preg_replace($pattern, '$1<?php if ( ! ($2)): ?>', $value);
309: }
310:
311: /**
312: * Rewrites Blade "unless" endings into valid PHP.
313: *
314: * @param string $value
315: * @return string
316: */
317: protected static function compile_endunless($value)
318: {
319: return str_replace('@endunless', '<?php endif; ?>', $value);
320: }
321:
322: /**
323: * Rewrites Blade @include statements into valid PHP.
324: *
325: * @param string $value
326: * @return string
327: */
328: protected static function compile_includes($value)
329: {
330: $pattern = static::matcher('include');
331:
332: return preg_replace($pattern, '$1<?php echo view$2->with(get_defined_vars())->render(); ?>', $value);
333: }
334:
335: /**
336: * Rewrites Blade @render statements into valid PHP.
337: *
338: * @param string $value
339: * @return string
340: */
341: protected static function compile_render($value)
342: {
343: $pattern = static::matcher('render');
344:
345: return preg_replace($pattern, '$1<?php echo render$2; ?>', $value);
346: }
347:
348: /**
349: * Rewrites Blade @render_each statements into valid PHP.
350: *
351: * @param string $value
352: * @return string
353: */
354: protected static function compile_render_each($value)
355: {
356: $pattern = static::matcher('render_each');
357:
358: return preg_replace($pattern, '$1<?php echo render_each$2; ?>', $value);
359: }
360:
361: /**
362: * Rewrites Blade @yield statements into Section statements.
363: *
364: * The Blade @yield statement is a shortcut to the Section::yield method.
365: *
366: * @param string $value
367: * @return string
368: */
369: protected static function compile_yields($value)
370: {
371: $pattern = static::matcher('yield');
372:
373: return preg_replace($pattern, '$1<?php echo \\Laravel\\Section::yield$2; ?>', $value);
374: }
375:
376: /**
377: * Rewrites Blade yield section statements into valid PHP.
378: *
379: * @return string
380: */
381: protected static function compile_yield_sections($value)
382: {
383: $replace = '<?php echo \\Laravel\\Section::yield_section(); ?>';
384:
385: return str_replace('@yield_section', $replace, $value);
386: }
387:
388: /**
389: * Rewrites Blade @section statements into Section statements.
390: *
391: * The Blade @section statement is a shortcut to the Section::start method.
392: *
393: * @param string $value
394: * @return string
395: */
396: protected static function compile_section_start($value)
397: {
398: $pattern = static::matcher('section');
399:
400: return preg_replace($pattern, '$1<?php \\Laravel\\Section::start$2; ?>', $value);
401: }
402:
403: /**
404: * Rewrites Blade @endsection statements into Section statements.
405: *
406: * The Blade @endsection statement is a shortcut to the Section::stop method.
407: *
408: * @param string $value
409: * @return string
410: */
411: protected static function compile_section_end($value)
412: {
413: return preg_replace('/@endsection/', '<?php \\Laravel\\Section::stop(); ?>', $value);
414: }
415:
416: /**
417: * Execute user defined compilers.
418: *
419: * @param string $value
420: * @return string
421: */
422: protected static function compile_extensions($value)
423: {
424: foreach (static::$extensions as $compiler)
425: {
426: $value = $compiler($value);
427: }
428:
429: return $value;
430: }
431:
432: /**
433: * Get the regular expression for a generic Blade function.
434: *
435: * @param string $function
436: * @return string
437: */
438: public static function matcher($function)
439: {
440: return '/(\s*)@'.$function.'(\s*\(.*\))/';
441: }
442:
443: /**
444: * Get the fully qualified path for a compiled view.
445: *
446: * @param string $view
447: * @return string
448: */
449: public static function compiled($path)
450: {
451: return path('storage').'views/'.md5($path);
452: }
453:
454: }