blade.php 9.9 KB

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