blade.php 9.9 KB

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