blade.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. * Register the Blade view engine with Laravel.
  19. *
  20. * @return void
  21. */
  22. public static function sharpen()
  23. {
  24. Event::listen(View::engine, function($view)
  25. {
  26. // The Blade view engine should only handle the rendering of views which
  27. // end with the Blade extension. If the given view does not, we will
  28. // return false so the View can be rendered as normal.
  29. if ( ! str_contains($view->path, BLADE_EXT))
  30. {
  31. return false;
  32. }
  33. $compiled = path('storage').'views/'.md5($view->path);
  34. // If the view doesn't exist or has been modified since the last time it
  35. // was compiled, we will recompile the view into pure PHP from it's
  36. // Blade representation, writing it to cached storage.
  37. if ( ! file_exists($compiled) or (filemtime($view->path) > filemtime($compiled)))
  38. {
  39. file_put_contents($compiled, Blade::compile($view->path));
  40. }
  41. $view->path = $compiled;
  42. // Once the view has been compiled, we can simply set the path to the
  43. // compiled view on the view instance and call the typical "get"
  44. // method on the view to evaluate the compiled PHP view.
  45. return $view->get();
  46. });
  47. }
  48. /**
  49. * Compiles the specified file containing Blade pseudo-code into valid PHP.
  50. *
  51. * @param string $path
  52. * @return string
  53. */
  54. public static function compile($path)
  55. {
  56. return static::compile_string(file_get_contents($path));
  57. }
  58. /**
  59. * Compiles the given string containing Blade pseudo-code into valid PHP.
  60. *
  61. * @param string $value
  62. * @return string
  63. */
  64. public static function compile_string($value)
  65. {
  66. foreach (static::$compilers as $compiler)
  67. {
  68. $method = "compile_{$compiler}";
  69. $value = static::$method($value);
  70. }
  71. return $value;
  72. }
  73. /**
  74. * Rewrites Blade echo statements into PHP echo statements.
  75. *
  76. * @param string $value
  77. * @return string
  78. */
  79. protected static function compile_echos($value)
  80. {
  81. return preg_replace('/\{\{(.+?)\}\}/', '<?php echo $1; ?>', $value);
  82. }
  83. /**
  84. * Rewrites Blade structure openings into PHP structure openings.
  85. *
  86. * @param string $value
  87. * @return string
  88. */
  89. protected static function compile_structure_openings($value)
  90. {
  91. $pattern = '/(\s*)@(if|elseif|foreach|for|while)(\s*\(.*\))/';
  92. return preg_replace($pattern, '$1<?php $2$3: ?>', $value);
  93. }
  94. /**
  95. * Rewrites Blade structure closings into PHP structure closings.
  96. *
  97. * @param string $value
  98. * @return string
  99. */
  100. protected static function compile_structure_closings($value)
  101. {
  102. $pattern = '/(\s*)@(endif|endforeach|endfor|endwhile)(\s*)/';
  103. return preg_replace($pattern, '$1<?php $2; ?>$3', $value);
  104. }
  105. /**
  106. * Rewrites Blade else statements into PHP else statements.
  107. *
  108. * @param string $value
  109. * @return string
  110. */
  111. protected static function compile_else($value)
  112. {
  113. return preg_replace('/(\s*)@(else)(\s*)/', '$1<?php $2: ?>$3', $value);
  114. }
  115. /**
  116. * Rewrites Blade @yield statements into Section statements.
  117. *
  118. * The Blade @yield statement is a shortcut to the Section::yield method.
  119. *
  120. * @param string $value
  121. * @return string
  122. */
  123. protected static function compile_yields($value)
  124. {
  125. $pattern = static::matcher('yield');
  126. return preg_replace($pattern, '$1<?php echo \\Laravel\\Section::yield$2; ?>', $value);
  127. }
  128. /**
  129. * Rewrites Blade @section statements into Section statements.
  130. *
  131. * The Blade @section statement is a shortcut to the Section::start method.
  132. *
  133. * @param string $value
  134. * @return string
  135. */
  136. protected static function compile_section_start($value)
  137. {
  138. $pattern = static::matcher('section');
  139. return preg_replace($pattern, '$1<?php \\Laravel\\Section::start$2; ?>', $value);
  140. }
  141. /**
  142. * Rewrites Blade @endsection statements into Section statements.
  143. *
  144. * The Blade @endsection statement is a shortcut to the Section::stop method.
  145. *
  146. * @param string $value
  147. * @return string
  148. */
  149. protected static function compile_section_end($value)
  150. {
  151. return preg_replace('/@endsection/', '<?php \\Laravel\\Section::stop(); ?>', $value);
  152. }
  153. /**
  154. * Get the regular expression for a generic Blade function.
  155. *
  156. * @param string $function
  157. * @return string
  158. */
  159. protected static function matcher($function)
  160. {
  161. return '/(\s*)@'.$function.'(\s*\(.*\))/';
  162. }
  163. }