blade.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php namespace Laravel;
  2. class Blade {
  3. /**
  4. * Compiles the specified file containing Blade pseudo-code into valid PHP.
  5. *
  6. * @param string $path
  7. * @return string
  8. */
  9. public static function compile($path)
  10. {
  11. $value = file_get_contents($path);
  12. return static::closings(static::openings(static::echos($value)));
  13. }
  14. /**
  15. * Rewrites Blade echo statements into PHP echo statements.
  16. *
  17. * @param string $value
  18. * @return string
  19. */
  20. protected static function echos($value)
  21. {
  22. return preg_replace('/\{\{(.+)\}\}/', '<?php echo $1; ?>', $value);
  23. }
  24. /**
  25. * Rewrites Blade structure openings into PHP structure openings.
  26. *
  27. * @param string $value
  28. * @return string
  29. */
  30. protected static function openings($value)
  31. {
  32. return preg_replace('/@(if|elseif|foreach|for|while)(\s*\(.*?\))\:/', '<?php $1$2: ?>', $value);
  33. }
  34. /**
  35. * Rewrites Blade structure closings into PHP structure closings.
  36. *
  37. * @param string $value
  38. * @return string
  39. */
  40. protected static function closings($value)
  41. {
  42. $value = preg_replace('/(\s*)@(else)(.*?)\:/', '$1<?php $2$3: ?>', $value);
  43. $value = preg_replace('/(\s*)@(endif|endforeach|endfor|endwhile)(\s*)/', '$1<?php $2; ?> $3', $value);
  44. return $value;
  45. }
  46. }