blade.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <?php namespace Laravel; use FilesystemIterator as fIterator;
  2. class Blade {
  3. /**
  4. * All of the compiler functions used by Blade.
  5. *
  6. * @var array
  7. */
  8. protected static $compilers = array(
  9. 'layouts',
  10. 'echos',
  11. 'forelse',
  12. 'empty',
  13. 'endforelse',
  14. 'structure_openings',
  15. 'structure_closings',
  16. 'else',
  17. 'includes',
  18. 'render_each',
  19. 'render',
  20. 'yields',
  21. 'yield_sections',
  22. 'section_start',
  23. 'section_end',
  24. );
  25. /**
  26. * Register the Blade view engine with Laravel.
  27. *
  28. * @return void
  29. */
  30. public static function sharpen()
  31. {
  32. Event::listen(View::engine, function($view)
  33. {
  34. // The Blade view engine should only handle the rendering of views which
  35. // end with the Blade extension. If the given view does not, we will
  36. // return false so the View can be rendered as normal.
  37. if ( ! str_contains($view->path, BLADE_EXT))
  38. {
  39. return false;
  40. }
  41. $compiled = path('storage').'views/'.md5($view->path);
  42. // If the view doesn't exist or has been modified since the last time it
  43. // was compiled, we will recompile the view into pure PHP from it's
  44. // Blade representation, writing it to cached storage.
  45. if ( ! file_exists($compiled) or Blade::expired($view->view, $view->path))
  46. {
  47. file_put_contents($compiled, Blade::compile($view));
  48. }
  49. $view->path = $compiled;
  50. // Once the view has been compiled, we can simply set the path to the
  51. // compiled view on the view instance and call the typical "get"
  52. // method on the view to evaluate the compiled PHP view.
  53. return $view->get();
  54. });
  55. }
  56. /**
  57. * Determine if a view is "expired" and needs to be re-compiled.
  58. *
  59. * @param string $view
  60. * @param string $path
  61. * @param string $compiled
  62. * @return bool
  63. */
  64. public static function expired($view, $path)
  65. {
  66. $compiled = static::compiled($path);
  67. return filemtime($path) > filemtime(static::compiled($path));
  68. }
  69. /**
  70. * Compiles the specified file containing Blade pseudo-code into valid PHP.
  71. *
  72. * @param string $path
  73. * @return string
  74. */
  75. public static function compile($view)
  76. {
  77. return static::compile_string(file_get_contents($view->path), $view);
  78. }
  79. /**
  80. * Compiles the given string containing Blade pseudo-code into valid PHP.
  81. *
  82. * @param string $value
  83. * @param View $view
  84. * @return string
  85. */
  86. public static function compile_string($value, $view = null)
  87. {
  88. foreach (static::$compilers as $compiler)
  89. {
  90. $method = "compile_{$compiler}";
  91. $value = static::$method($value, $view);
  92. }
  93. return $value;
  94. }
  95. /**
  96. * Rewrites Blade "@layout" expressions into valid PHP.
  97. *
  98. * @param string $value
  99. * @return string
  100. */
  101. protected static function compile_layouts($value)
  102. {
  103. // If the Blade template is not using "layouts", we'll just return it
  104. // it unchanged since there is nothing to do with layouts and we'll
  105. // just let the other Blade compilers handle the rest.
  106. if ( ! starts_with($value, '@layout'))
  107. {
  108. return $value;
  109. }
  110. // First we'll split out the lines of the template so we can get the
  111. // the layout from the top of the template. By convention it must
  112. // be located on the first line of the template contents.
  113. $lines = preg_split("/(\r?\n)/", $value);
  114. $pattern = static::matcher('layout');
  115. $lines[] = preg_replace($pattern, '$1@include$2', $lines[0]);
  116. // We will add a "render" statement to the end of the templates and
  117. // and then slice off the @layout shortcut from the start so the
  118. // sections register before the parent template renders.
  119. return implode(CRLF, array_slice($lines, 1));
  120. }
  121. /**
  122. * Extract a variable value out of a Blade expression.
  123. *
  124. * @param string $value
  125. * @return string
  126. */
  127. protected static function extract($value, $expression)
  128. {
  129. preg_match('/@layout(\s*\(.*\))(\s*)/', $value, $matches);
  130. return str_replace(array("('", "')"), '', $matches[1]);
  131. }
  132. /**
  133. * Rewrites Blade echo statements into PHP echo statements.
  134. *
  135. * @param string $value
  136. * @return string
  137. */
  138. protected static function compile_echos($value)
  139. {
  140. return preg_replace('/\{\{(.+?)\}\}/', '<?php echo $1; ?>', $value);
  141. }
  142. /**
  143. * Rewrites Blade "for else" statements into valid PHP.
  144. *
  145. * @param string $value
  146. * @return string
  147. */
  148. protected static function compile_forelse($value)
  149. {
  150. preg_match_all('/(\s*)@forelse(\s*\(.*\))(\s*)/', $value, $matches);
  151. foreach ($matches[0] as $forelse)
  152. {
  153. preg_match('/\$[^\s]*/', $forelse, $variable);
  154. // Once we have extracted the variable being looped against, we can add
  155. // an if statmeent to the start of the loop that checks if the count
  156. // of the variable being looped against is greater than zero.
  157. $if = "<?php if (count({$variable[0]}) > 0): ?>";
  158. $search = '/(\s*)@forelse(\s*\(.*\))/';
  159. $replace = '$1'.$if.'<?php foreach$2: ?>';
  160. $blade = preg_replace($search, $replace, $forelse);
  161. // Finally, once we have the check prepended to the loop we'll replace
  162. // all instances of this "forelse" syntax in the view content of the
  163. // view being compiled to Blade syntax with real syntax.
  164. $value = str_replace($forelse, $blade, $value);
  165. }
  166. return $value;
  167. }
  168. /**
  169. * Rewrites Blade "empty" statements into valid PHP.
  170. *
  171. * @param string $value
  172. * @return string
  173. */
  174. protected static function compile_empty($value)
  175. {
  176. return str_replace('@empty', '<?php endforeach; ?><?php else: ?>', $value);
  177. }
  178. /**
  179. * Rewrites Blade "forelse" endings into valid PHP.
  180. *
  181. * @param string $value
  182. * @return string
  183. */
  184. protected static function compile_endforelse($value)
  185. {
  186. return str_replace('@endforelse', '<?php endif; ?>', $value);
  187. }
  188. /**
  189. * Rewrites Blade structure openings into PHP structure openings.
  190. *
  191. * @param string $value
  192. * @return string
  193. */
  194. protected static function compile_structure_openings($value)
  195. {
  196. $pattern = '/(\s*)@(if|elseif|foreach|for|while)(\s*\(.*\))/';
  197. return preg_replace($pattern, '$1<?php $2$3: ?>', $value);
  198. }
  199. /**
  200. * Rewrites Blade structure closings into PHP structure closings.
  201. *
  202. * @param string $value
  203. * @return string
  204. */
  205. protected static function compile_structure_closings($value)
  206. {
  207. $pattern = '/(\s*)@(endif|endforeach|endfor|endwhile)(\s*)/';
  208. return preg_replace($pattern, '$1<?php $2; ?>$3', $value);
  209. }
  210. /**
  211. * Rewrites Blade else statements into PHP else statements.
  212. *
  213. * @param string $value
  214. * @return string
  215. */
  216. protected static function compile_else($value)
  217. {
  218. return preg_replace('/(\s*)@(else)(\s*)/', '$1<?php $2: ?>$3', $value);
  219. }
  220. /**
  221. * Rewrites Blade @include statements into valid PHP.
  222. *
  223. * @param string $value
  224. * @return string
  225. */
  226. protected static function compile_includes($value)
  227. {
  228. $pattern = static::matcher('include');
  229. return preg_replace($pattern, '$1<?php echo view$2->with(get_defined_vars())->render(); ?>', $value);
  230. }
  231. /**
  232. * Rewrites Blade @render statements into valid PHP.
  233. *
  234. * @param string $value
  235. * @return string
  236. */
  237. protected static function compile_render($value)
  238. {
  239. $pattern = static::matcher('render');
  240. return preg_replace($pattern, '$1<?php echo render$2; ?>', $value);
  241. }
  242. /**
  243. * Rewrites Blade @render_each statements into valid PHP.
  244. *
  245. * @param string $value
  246. * @return string
  247. */
  248. protected static function compile_render_each($value)
  249. {
  250. $pattern = static::matcher('render_each');
  251. return preg_replace($pattern, '$1<?php echo render_each$2; ?>', $value);
  252. }
  253. /**
  254. * Rewrites Blade @yield statements into Section statements.
  255. *
  256. * The Blade @yield statement is a shortcut to the Section::yield method.
  257. *
  258. * @param string $value
  259. * @return string
  260. */
  261. protected static function compile_yields($value)
  262. {
  263. $pattern = static::matcher('yield');
  264. return preg_replace($pattern, '$1<?php echo \\Laravel\\Section::yield$2; ?>', $value);
  265. }
  266. /**
  267. * Rewrites Blade yield section statements into valid PHP.
  268. *
  269. * @return string
  270. */
  271. protected static function compile_yield_sections($value)
  272. {
  273. $replace = '<?php echo \\Laravel\\Section::yield_section(); ?>';
  274. return str_replace('@yield_section', $replace, $value);
  275. }
  276. /**
  277. * Rewrites Blade @section statements into Section statements.
  278. *
  279. * The Blade @section statement is a shortcut to the Section::start method.
  280. *
  281. * @param string $value
  282. * @return string
  283. */
  284. protected static function compile_section_start($value)
  285. {
  286. $pattern = static::matcher('section');
  287. return preg_replace($pattern, '$1<?php \\Laravel\\Section::start$2; ?>', $value);
  288. }
  289. /**
  290. * Rewrites Blade @endsection statements into Section statements.
  291. *
  292. * The Blade @endsection statement is a shortcut to the Section::stop method.
  293. *
  294. * @param string $value
  295. * @return string
  296. */
  297. protected static function compile_section_end($value)
  298. {
  299. return preg_replace('/@endsection/', '<?php \\Laravel\\Section::stop(); ?>', $value);
  300. }
  301. /**
  302. * Get the regular expression for a generic Blade function.
  303. *
  304. * @param string $function
  305. * @return string
  306. */
  307. protected static function matcher($function)
  308. {
  309. return '/(\s*)@'.$function.'(\s*\(.*\))/';
  310. }
  311. /**
  312. * Get the fully qualified path for a compiled view.
  313. *
  314. * @param string $view
  315. * @return string
  316. */
  317. public static function compiled($path)
  318. {
  319. return path('storage').'views/'.md5($path);
  320. }
  321. }