blade.php 11 KB

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