blade.php 1.4 KB

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