blade.php 11 KB

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