PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/system/core/blade.php

https://bitbucket.org/viswanath608/laravel-shared-host
PHP | 454 lines | 184 code | 61 blank | 209 comment | 4 complexity | 7943168ebe3c648911a351ab63a718d2 MD5 | raw file
  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. $value = preg_replace('/\{\{\{(.+?)\}\}\}/', '<?php echo HTML::entities($1); ?>', $value);
  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)(\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. }