blade.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php namespace Laravel;
  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. * Blade echo statements are simply PHP statement enclosed within double curly
  46. * braces. For example, {{$content}} will simply echo out the content variable
  47. * to the output buffer.
  48. *
  49. * @param string $value
  50. * @return string
  51. */
  52. protected static function compile_echos($value)
  53. {
  54. return preg_replace('/\{\{(.+?)\}\}/', '<?php echo $1; ?>', $value);
  55. }
  56. /**
  57. * Rewrites Blade structure openings into PHP structure openings.
  58. *
  59. * By "structures", we mean the if, elseif, foreach, for, and while statements.
  60. * All of these structures essentially have the same format, and can be lumped
  61. * into a single regular expression.
  62. *
  63. * @param string $value
  64. * @return string
  65. */
  66. protected static function compile_structure_openings($value)
  67. {
  68. $pattern = '/(\s*)@(if|elseif|foreach|for|while)(\s*\(.*?\))/';
  69. return preg_replace($pattern, '$1<?php $2$3: ?>', $value);
  70. }
  71. /**
  72. * Rewrites Blade structure closings into PHP structure closings.
  73. *
  74. * @param string $value
  75. * @return string
  76. */
  77. protected static function compile_structure_closings($value)
  78. {
  79. $pattern = '/(\s*)@(endif|endforeach|endfor|endwhile)(\s*)/';
  80. return preg_replace($pattern, '$1<?php $2; ?>$3', $value);
  81. }
  82. /**
  83. * Rewrites Blade else statements into PHP else statements.
  84. *
  85. * @param string $value
  86. * @return string
  87. */
  88. protected static function compile_else($value)
  89. {
  90. return preg_replace('/(\s*)@(else)(\s*)/', '$1<?php $2: ?>$3', $value);
  91. }
  92. /**
  93. * Rewrites Blade @yield statements into Section statements.
  94. *
  95. * The Blade @yield statement is a shortcut to the Section::yield method.
  96. *
  97. * @param string $value
  98. * @return string
  99. */
  100. protected static function compile_yields($value)
  101. {
  102. $pattern = static::matcher('yield');
  103. return preg_replace($pattern, '$1<?php echo \\Laravel\\Section::yield$2; ?>', $value);
  104. }
  105. /**
  106. * Rewrites Blade @section statements into Section statements.
  107. *
  108. * The Blade @section statement is a shortcut to the Section::start method.
  109. *
  110. * @param string $value
  111. * @return string
  112. */
  113. protected static function compile_section_start($value)
  114. {
  115. $pattern = static::matcher('section');
  116. return preg_replace($pattern, '$1<?php \\Laravel\\Section::start$2; ?>', $value);
  117. }
  118. /**
  119. * Rewrites Blade @endsection statements into Section statements.
  120. *
  121. * The Blade @endsection statement is a shortcut to the Section::stop method.
  122. *
  123. * @param string $value
  124. * @return string
  125. */
  126. protected static function compile_section_end($value)
  127. {
  128. return preg_replace('/@endsection/', '<?php \\Laravel\\Section::stop(); ?>', $value);
  129. }
  130. /**
  131. * Get the regular expression for a generic Blade function.
  132. *
  133. * @param string $function
  134. * @return string
  135. */
  136. protected static function matcher($function)
  137. {
  138. return '/(\s*)@'.$function.'(\s*\(.*?\))/';
  139. }
  140. }