blade.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php namespace Laravel; isset($GLOBALS['APP_PATH']) or die('No direct script access.');
  2. class Blade {
  3. /**
  4. * All of the compiler functions used by Blade.
  5. *
  6. * @var array
  7. */
  8. protected static $compilers = array(
  9. 'echos',
  10. 'structure_openings',
  11. 'structure_closings',
  12. 'else',
  13. 'yields',
  14. 'section_start',
  15. 'section_end',
  16. );
  17. /**
  18. * Compiles the specified file containing Blade pseudo-code into valid PHP.
  19. *
  20. * @param string $path
  21. * @return string
  22. */
  23. public static function compile($path)
  24. {
  25. return static::compile_string(file_get_contents($path));
  26. }
  27. /**
  28. * Compiles the given string containing Blade pseudo-code into valid PHP.
  29. *
  30. * @param string $value
  31. * @return string
  32. */
  33. public static function compile_string($value)
  34. {
  35. foreach (static::$compilers as $compiler)
  36. {
  37. $method = "compile_{$compiler}";
  38. $value = static::$method($value);
  39. }
  40. return $value;
  41. }
  42. /**
  43. * Rewrites Blade echo statements into PHP echo statements.
  44. *
  45. * @param string $value
  46. * @return string
  47. */
  48. protected static function compile_echos($value)
  49. {
  50. return preg_replace('/\{\{(.+?)\}\}/', '<?php echo $1; ?>', $value);
  51. }
  52. /**
  53. * Rewrites Blade structure openings into PHP structure openings.
  54. *
  55. * @param string $value
  56. * @return string
  57. */
  58. protected static function compile_structure_openings($value)
  59. {
  60. $pattern = '/(\s*)@(if|elseif|foreach|for|while)(\s*\(.*\))/';
  61. return preg_replace($pattern, '$1<?php $2$3: ?>', $value);
  62. }
  63. /**
  64. * Rewrites Blade structure closings into PHP structure closings.
  65. *
  66. * @param string $value
  67. * @return string
  68. */
  69. protected static function compile_structure_closings($value)
  70. {
  71. $pattern = '/(\s*)@(endif|endforeach|endfor|endwhile)(\s*)/';
  72. return preg_replace($pattern, '$1<?php $2; ?>$3', $value);
  73. }
  74. /**
  75. * Rewrites Blade else statements into PHP else statements.
  76. *
  77. * @param string $value
  78. * @return string
  79. */
  80. protected static function compile_else($value)
  81. {
  82. return preg_replace('/(\s*)@(else)(\s*)/', '$1<?php $2: ?>$3', $value);
  83. }
  84. /**
  85. * Rewrites Blade @yield statements into Section statements.
  86. *
  87. * The Blade @yield statement is a shortcut to the Section::yield method.
  88. *
  89. * @param string $value
  90. * @return string
  91. */
  92. protected static function compile_yields($value)
  93. {
  94. $pattern = static::matcher('yield');
  95. return preg_replace($pattern, '$1<?php echo \\Laravel\\Section::yield$2; ?>', $value);
  96. }
  97. /**
  98. * Rewrites Blade @section statements into Section statements.
  99. *
  100. * The Blade @section statement is a shortcut to the Section::start method.
  101. *
  102. * @param string $value
  103. * @return string
  104. */
  105. protected static function compile_section_start($value)
  106. {
  107. $pattern = static::matcher('section');
  108. return preg_replace($pattern, '$1<?php \\Laravel\\Section::start$2; ?>', $value);
  109. }
  110. /**
  111. * Rewrites Blade @endsection statements into Section statements.
  112. *
  113. * The Blade @endsection statement is a shortcut to the Section::stop method.
  114. *
  115. * @param string $value
  116. * @return string
  117. */
  118. protected static function compile_section_end($value)
  119. {
  120. return preg_replace('/@endsection/', '<?php \\Laravel\\Section::stop(); ?>', $value);
  121. }
  122. /**
  123. * Get the regular expression for a generic Blade function.
  124. *
  125. * @param string $function
  126. * @return string
  127. */
  128. protected static function matcher($function)
  129. {
  130. return '/(\s*)@'.$function.'(\s*\(.*\))/';
  131. }
  132. }