blade.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. * @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 $view
  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. preg_replace_callback(
  139. '/^@layout(\s*?\(.+?\))(\r?\n)?/',
  140. function($matches) use (&$value)
  141. {
  142. $value = substr( $value, strlen( $matches[0] ) ).CRLF.'@include'.$matches[1];
  143. },
  144. $value
  145. );
  146. return $value;
  147. }
  148. /**
  149. * Extract a variable value out of a Blade expression.
  150. *
  151. * @param string $value
  152. * @param string $expression
  153. * @return string
  154. */
  155. protected static function extract($value, $expression)
  156. {
  157. if ( preg_match("/@layout\s*?\(\s*?'(.+?)'\s*?\)/", $value, $matches))
  158. {
  159. return trim( $matches[1] );
  160. }
  161. }
  162. /**
  163. * Rewrites Blade comments into PHP comments.
  164. *
  165. * @param string $value
  166. * @return string
  167. */
  168. protected static function compile_comments($value)
  169. {
  170. $value = preg_replace('/\{\{--(.*?)--\}\}/', "<?php // $1 ?>", $value);
  171. return preg_replace('/\{\{--(.*?)--\}\}/s', "<?php /* ?>$1<?php */ ?>", $value);
  172. }
  173. /**
  174. * Rewrites Blade echo statements into PHP echo statements.
  175. *
  176. * @param string $value
  177. * @return string
  178. */
  179. protected static function compile_echos($value)
  180. {
  181. return preg_replace('/\{\{(.+?)\}\}/s', '<?php echo $1; ?>', $value);
  182. }
  183. /**
  184. * Rewrites Blade "for else" statements into valid PHP.
  185. *
  186. * @param string $value
  187. * @return string
  188. */
  189. protected static function compile_forelse($value)
  190. {
  191. preg_match_all('/@forelse\s*?\(\s*?\$(.+?)\s*?as\s*?\$(.+?)\s*?\)/', $value, $matches, PREG_SET_ORDER );
  192. if ( count($matches) < 1 ) return $value;
  193. foreach ($matches as $forelse)
  194. {
  195. // Once we have extracted the variable being looped against, we can add
  196. // an if statement to the start of the loop that checks if the count
  197. // of the variable being looped against is greater than zero.
  198. $replace = '<?php if (count($'.$forelse[1].') > 0): foreach ($'.$forelse[1].' as $'.$forelse[2].'): ?>';
  199. // Finally, once we have the check prepended to the loop we'll replace
  200. // all instances of this forelse syntax in the view content of the
  201. // view being compiled to Blade syntax with real PHP syntax.
  202. $value = str_replace($forelse[0], $replace, $value);
  203. }
  204. return $value;
  205. }
  206. /**
  207. * Rewrites Blade "empty" statements into valid PHP.
  208. *
  209. * @param string $value
  210. * @return string
  211. */
  212. protected static function compile_empty($value)
  213. {
  214. return str_replace('@empty', '<?php endforeach; else: ?>', $value);
  215. }
  216. /**
  217. * Rewrites Blade "forelse" endings into valid PHP.
  218. *
  219. * @param string $value
  220. * @return string
  221. */
  222. protected static function compile_endforelse($value)
  223. {
  224. return str_replace('@endforelse', '<?php endif; ?>', $value);
  225. }
  226. /**
  227. * Rewrites Blade structure openings into PHP structure openings.
  228. *
  229. * @param string $value
  230. * @return string
  231. */
  232. protected static function compile_structure_openings($value)
  233. {
  234. preg_replace_callback(
  235. '/@(if|elseif|foreach|for|while)(\s*?)(\([^\n\r\t]+\))/',
  236. function($matches) use (&$value)
  237. {
  238. if(count( $matches ) === 4)
  239. {
  240. $open = 0;
  241. $close = 0;
  242. $cut = 0;
  243. $len = strlen($matches[3]);
  244. for($i = 0; $i < $len; $i++)
  245. {
  246. if($matches[3][$i] === '(' )
  247. {
  248. $open++;
  249. }
  250. if($matches[3][$i] === ')' )
  251. {
  252. $close++;
  253. }
  254. if($open !== 0 && ($open === $close))
  255. {
  256. break;
  257. }
  258. }
  259. $condition = substr($matches[3], 0, ($i + 1));
  260. $value = str_replace(
  261. '@'.$matches[1].$matches[2].$condition,
  262. '<?php '.$matches[1].$condition.': ?>',
  263. $value
  264. );
  265. }
  266. },
  267. $value
  268. );
  269. return $value;
  270. }
  271. /**
  272. * Rewrites Blade structure closings into PHP structure closings.
  273. *
  274. * @param string $value
  275. * @return string
  276. */
  277. protected static function compile_structure_closings($value)
  278. {
  279. $pattern = '/@(endif|endforeach|endfor|endwhile|break|continue)/';
  280. return preg_replace($pattern, '<?php $1; ?>', $value);
  281. }
  282. /**
  283. * Rewrites Blade else statements into PHP else statements.
  284. *
  285. * @param string $value
  286. * @return string
  287. */
  288. protected static function compile_else($value)
  289. {
  290. return str_replace( '@else', '<?php else: ?>', $value);
  291. }
  292. /**
  293. * Rewrites Blade "unless" statements into valid PHP.
  294. *
  295. * @param string $value
  296. * @return string
  297. */
  298. protected static function compile_unless($value)
  299. {
  300. $pattern = static::matcher('unless');
  301. return preg_replace($pattern, '<?php if( ! ($1)): ?>', $value);
  302. }
  303. /**
  304. * Rewrites Blade "unless" endings into valid PHP.
  305. *
  306. * @param string $value
  307. * @return string
  308. */
  309. protected static function compile_endunless($value)
  310. {
  311. return str_replace('@endunless', '<?php endif; ?>', $value);
  312. }
  313. /**
  314. * Rewrites Blade @include statements into valid PHP.
  315. *
  316. * @param string $value
  317. * @return string
  318. */
  319. protected static function compile_includes($value)
  320. {
  321. $pattern = static::matcher('include');
  322. return preg_replace($pattern, '<?php echo view$1->with(get_defined_vars())->render(); ?>', $value);
  323. }
  324. /**
  325. * Rewrites Blade @render statements into valid PHP.
  326. *
  327. * @param string $value
  328. * @return string
  329. */
  330. protected static function compile_render($value)
  331. {
  332. $pattern = static::matcher('render');
  333. return preg_replace($pattern, '<?php echo render$1; ?>', $value);
  334. }
  335. /**
  336. * Rewrites Blade @render_each statements into valid PHP.
  337. *
  338. * @param string $value
  339. * @return string
  340. */
  341. protected static function compile_render_each($value)
  342. {
  343. $pattern = static::matcher('render_each');
  344. return preg_replace($pattern, '<?php echo render_each$1; ?>', $value);
  345. }
  346. /**
  347. * Rewrites Blade @yield statements into Section statements.
  348. *
  349. * The Blade @yield statement is a shortcut to the Section::yield method.
  350. *
  351. * @param string $value
  352. * @return string
  353. */
  354. protected static function compile_yields($value)
  355. {
  356. $pattern = static::matcher('yield');
  357. return preg_replace($pattern, '<?php echo \\Laravel\\Section::yield$1; ?>', $value);
  358. }
  359. /**
  360. * Rewrites Blade yield section statements into valid PHP.
  361. *
  362. * @param string $value
  363. * @return string
  364. */
  365. protected static function compile_yield_sections($value)
  366. {
  367. return str_replace('@yield_section', '<?php echo \\Laravel\\Section::yield_section(); ?>', $value);
  368. }
  369. /**
  370. * Rewrites Blade @section statements into Section statements.
  371. *
  372. * The Blade @section statement is a shortcut to the Section::start method.
  373. *
  374. * @param string $value
  375. * @return string
  376. */
  377. protected static function compile_section_start($value)
  378. {
  379. $pattern = static::matcher('section');
  380. return preg_replace($pattern, '<?php \\Laravel\\Section::start$1; ?>', $value);
  381. }
  382. /**
  383. * Rewrites Blade @endsection statements into Section statements.
  384. *
  385. * The Blade @endsection statement is a shortcut to the Section::stop method.
  386. *
  387. * @param string $value
  388. * @return string
  389. */
  390. protected static function compile_section_end($value)
  391. {
  392. return str_replace('@endsection', '<?php \\Laravel\\Section::stop(); ?>', $value);
  393. }
  394. /**
  395. * Execute user defined compilers.
  396. *
  397. * @param string $value
  398. * @return string
  399. */
  400. protected static function compile_extensions($value)
  401. {
  402. foreach (static::$extensions as $compiler)
  403. {
  404. $value = $compiler($value);
  405. }
  406. return $value;
  407. }
  408. /**
  409. * Get the regular expression for a generic Blade function.
  410. *
  411. * @param string $function
  412. * @return string
  413. */
  414. public static function matcher($function)
  415. {
  416. return '/@'.$function.'\s*?(\(.+?\))/';
  417. }
  418. /**
  419. * Get the fully qualified path for a compiled view.
  420. *
  421. * @param string $path
  422. * @return string
  423. */
  424. public static function compiled($path)
  425. {
  426. return path('storage').'views/'.md5($path);
  427. }
  428. }