blade.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 ltrim($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. * @return bool
  89. */
  90. public static function expired($view, $path)
  91. {
  92. return filemtime($path) > filemtime(static::compiled($path));
  93. }
  94. /**
  95. * Compiles the specified file containing Blade pseudo-code into valid PHP.
  96. *
  97. * @param string $path
  98. * @return string
  99. */
  100. public static function compile($view)
  101. {
  102. return static::compile_string(file_get_contents($view->path), $view);
  103. }
  104. /**
  105. * Compiles the given string containing Blade pseudo-code into valid PHP.
  106. *
  107. * @param string $value
  108. * @param View $view
  109. * @return string
  110. */
  111. public static function compile_string($value, $view = null)
  112. {
  113. foreach (static::$compilers as $compiler)
  114. {
  115. $method = "compile_{$compiler}";
  116. $value = static::$method($value, $view);
  117. }
  118. return $value;
  119. }
  120. /**
  121. * Rewrites Blade "@layout" expressions into valid PHP.
  122. *
  123. * @param string $value
  124. * @return string
  125. */
  126. protected static function compile_layouts($value)
  127. {
  128. // If the Blade template is not using "layouts", we'll just return it
  129. // unchanged since there is nothing to do with layouts and we will
  130. // just let the other Blade compilers handle the rest.
  131. if ( ! starts_with($value, '@layout'))
  132. {
  133. return $value;
  134. }
  135. // First we'll split out the lines of the template so we can get the
  136. // layout from the top of the template. By convention it must be
  137. // located on the first line of the template contents.
  138. $lines = preg_split("/(\r?\n)/", $value);
  139. $pattern = static::matcher('layout');
  140. $lines[] = preg_replace($pattern, '$1@include$2', $lines[0]);
  141. // We will add a "render" statement to the end of the templates and
  142. // then slice off the "@layout" shortcut from the start so the
  143. // sections register before the parent template renders.
  144. return implode(CRLF, array_slice($lines, 1));
  145. }
  146. /**
  147. * Extract a variable value out of a Blade expression.
  148. *
  149. * @param string $value
  150. * @return string
  151. */
  152. protected static function extract($value, $expression)
  153. {
  154. preg_match('/@layout(\s*\(.*\))(\s*)/', $value, $matches);
  155. return str_replace(array("('", "')"), '', $matches[1]);
  156. }
  157. /**
  158. * Rewrites Blade comments into PHP comments.
  159. *
  160. * @param string $value
  161. * @return string
  162. */
  163. protected static function compile_comments($value)
  164. {
  165. $value = preg_replace('/\{\{--(.+?)(--\}\})?\n/', "<?php // $1 ?>", $value);
  166. return preg_replace('/\{\{--((.|\s)*?)--\}\}/', "<?php /* $1 */ ?>\n", $value);
  167. }
  168. /**
  169. * Rewrites Blade echo statements into PHP echo statements.
  170. *
  171. * @param string $value
  172. * @return string
  173. */
  174. protected static function compile_echos($value)
  175. {
  176. return preg_replace('/\{\{(.+?)\}\}/', '<?php echo $1; ?>', $value);
  177. }
  178. /**
  179. * Rewrites Blade "for else" statements into valid PHP.
  180. *
  181. * @param string $value
  182. * @return string
  183. */
  184. protected static function compile_forelse($value)
  185. {
  186. preg_match_all('/(\s*)@forelse(\s*\(.*\))(\s*)/', $value, $matches);
  187. foreach ($matches[0] as $forelse)
  188. {
  189. preg_match('/\s*\(\s*(\S*)\s/', $forelse, $variable);
  190. // Once we have extracted the variable being looped against, we can add
  191. // an if statement to the start of the loop that checks if the count
  192. // of the variable being looped against is greater than zero.
  193. $if = "<?php if (count({$variable[1]}) > 0): ?>";
  194. $search = '/(\s*)@forelse(\s*\(.*\))/';
  195. $replace = '$1'.$if.'<?php foreach$2: ?>';
  196. $blade = preg_replace($search, $replace, $forelse);
  197. // Finally, once we have the check prepended to the loop we'll replace
  198. // all instances of this forelse syntax in the view content of the
  199. // view being compiled to Blade syntax with real PHP syntax.
  200. $value = str_replace($forelse, $blade, $value);
  201. }
  202. return $value;
  203. }
  204. /**
  205. * Rewrites Blade "empty" statements into valid PHP.
  206. *
  207. * @param string $value
  208. * @return string
  209. */
  210. protected static function compile_empty($value)
  211. {
  212. return str_replace('@empty', '<?php endforeach; ?><?php else: ?>', $value);
  213. }
  214. /**
  215. * Rewrites Blade "forelse" endings into valid PHP.
  216. *
  217. * @param string $value
  218. * @return string
  219. */
  220. protected static function compile_endforelse($value)
  221. {
  222. return str_replace('@endforelse', '<?php endif; ?>', $value);
  223. }
  224. /**
  225. * Rewrites Blade structure openings into PHP structure openings.
  226. *
  227. * @param string $value
  228. * @return string
  229. */
  230. protected static function compile_structure_openings($value)
  231. {
  232. $pattern = '/(\s*)@(if|elseif|foreach|for|while)(\s*\(.*\))/';
  233. return preg_replace($pattern, '$1<?php $2$3: ?>', $value);
  234. }
  235. /**
  236. * Rewrites Blade structure closings into PHP structure closings.
  237. *
  238. * @param string $value
  239. * @return string
  240. */
  241. protected static function compile_structure_closings($value)
  242. {
  243. $pattern = '/(\s*)@(endif|endforeach|endfor|endwhile)(\s*)/';
  244. return preg_replace($pattern, '$1<?php $2; ?>$3', $value);
  245. }
  246. /**
  247. * Rewrites Blade else statements into PHP else statements.
  248. *
  249. * @param string $value
  250. * @return string
  251. */
  252. protected static function compile_else($value)
  253. {
  254. return preg_replace('/(\s*)@(else)(\s*)/', '$1<?php $2: ?>$3', $value);
  255. }
  256. /**
  257. * Rewrites Blade "unless" statements into valid PHP.
  258. *
  259. * @param string $value
  260. * @return string
  261. */
  262. protected static function compile_unless($value)
  263. {
  264. $pattern = '/(\s*)@unless(\s*\(.*\))/';
  265. return preg_replace($pattern, '$1<?php if ( ! ($2)): ?>', $value);
  266. }
  267. /**
  268. * Rewrites Blade "unless" endings into valid PHP.
  269. *
  270. * @param string $value
  271. * @return string
  272. */
  273. protected static function compile_endunless($value)
  274. {
  275. return str_replace('@endunless', '<?php endif; ?>', $value);
  276. }
  277. /**
  278. * Rewrites Blade @include statements into valid PHP.
  279. *
  280. * @param string $value
  281. * @return string
  282. */
  283. protected static function compile_includes($value)
  284. {
  285. $pattern = static::matcher('include');
  286. return preg_replace($pattern, '$1<?php echo view$2->with(get_defined_vars())->render(); ?>', $value);
  287. }
  288. /**
  289. * Rewrites Blade @render statements into valid PHP.
  290. *
  291. * @param string $value
  292. * @return string
  293. */
  294. protected static function compile_render($value)
  295. {
  296. $pattern = static::matcher('render');
  297. return preg_replace($pattern, '$1<?php echo render$2; ?>', $value);
  298. }
  299. /**
  300. * Rewrites Blade @render_each statements into valid PHP.
  301. *
  302. * @param string $value
  303. * @return string
  304. */
  305. protected static function compile_render_each($value)
  306. {
  307. $pattern = static::matcher('render_each');
  308. return preg_replace($pattern, '$1<?php echo render_each$2; ?>', $value);
  309. }
  310. /**
  311. * Rewrites Blade @yield statements into Section statements.
  312. *
  313. * The Blade @yield statement is a shortcut to the Section::yield method.
  314. *
  315. * @param string $value
  316. * @return string
  317. */
  318. protected static function compile_yields($value)
  319. {
  320. $pattern = static::matcher('yield');
  321. return preg_replace($pattern, '$1<?php echo \\Laravel\\Section::yield$2; ?>', $value);
  322. }
  323. /**
  324. * Rewrites Blade yield section statements into valid PHP.
  325. *
  326. * @return string
  327. */
  328. protected static function compile_yield_sections($value)
  329. {
  330. $replace = '<?php echo \\Laravel\\Section::yield_section(); ?>';
  331. return str_replace('@yield_section', $replace, $value);
  332. }
  333. /**
  334. * Rewrites Blade @section statements into Section statements.
  335. *
  336. * The Blade @section statement is a shortcut to the Section::start method.
  337. *
  338. * @param string $value
  339. * @return string
  340. */
  341. protected static function compile_section_start($value)
  342. {
  343. $pattern = static::matcher('section');
  344. return preg_replace($pattern, '$1<?php \\Laravel\\Section::start$2; ?>', $value);
  345. }
  346. /**
  347. * Rewrites Blade @endsection statements into Section statements.
  348. *
  349. * The Blade @endsection statement is a shortcut to the Section::stop method.
  350. *
  351. * @param string $value
  352. * @return string
  353. */
  354. protected static function compile_section_end($value)
  355. {
  356. return preg_replace('/@endsection/', '<?php \\Laravel\\Section::stop(); ?>', $value);
  357. }
  358. /**
  359. * Execute user defined compilers.
  360. *
  361. * @param string $value
  362. * @return string
  363. */
  364. protected static function compile_extensions($value)
  365. {
  366. foreach (static::$extensions as $compiler)
  367. {
  368. $value = $compiler($value);
  369. }
  370. return $value;
  371. }
  372. /**
  373. * Get the regular expression for a generic Blade function.
  374. *
  375. * @param string $function
  376. * @return string
  377. */
  378. public static function matcher($function)
  379. {
  380. return '/(\s*)@'.$function.'(\s*\(.*\))/';
  381. }
  382. /**
  383. * Get the fully qualified path for a compiled view.
  384. *
  385. * @param string $view
  386. * @return string
  387. */
  388. public static function compiled($path)
  389. {
  390. return path('storage').'views/'.md5($path);
  391. }
  392. }