123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- <?php namespace Laravel; use FilesystemIterator as fIterator; use Closure;
- class Blade {
-
- protected static $compilers = array(
- 'extensions',
- 'layouts',
- 'comments',
- 'echos',
- 'forelse',
- 'empty',
- 'endforelse',
- 'structure_openings',
- 'structure_closings',
- 'else',
- 'unless',
- 'endunless',
- 'includes',
- 'render_each',
- 'render',
- 'yields',
- 'yield_sections',
- 'section_start',
- 'section_end',
- );
-
- protected static $extensions = array();
-
- public static function sharpen()
- {
- Event::listen(View::engine, function($view)
- {
-
-
-
- if ( ! str_contains($view->path, BLADE_EXT))
- {
- return;
- }
- $compiled = Blade::compiled($view->path);
-
-
-
- if ( ! file_exists($compiled) or Blade::expired($view->view, $view->path))
- {
- file_put_contents($compiled, Blade::compile($view));
- }
- $view->path = $compiled;
-
-
-
- return $view->get();
- });
- }
-
- public static function extend(Closure $compiler)
- {
- static::$extensions[] = $compiler;
- }
-
- public static function expired($view, $path)
- {
- return filemtime($path) > filemtime(static::compiled($path));
- }
-
- public static function compile($view)
- {
- return static::compile_string(file_get_contents($view->path), $view);
- }
-
- public static function compile_string($value, $view = null)
- {
- foreach (static::$compilers as $compiler)
- {
- $method = "compile_{$compiler}";
- $value = static::$method($value, $view);
- }
- return $value;
- }
-
- protected static function compile_layouts($value)
- {
-
-
-
- if ( ! starts_with($value, '@layout'))
- {
- return $value;
- }
-
-
-
- $lines = preg_split("/(\r?\n)/", $value);
- $pattern = static::matcher('layout');
- $lines[] = preg_replace($pattern, '$1@include$2', $lines[0]);
-
-
-
- return implode(CRLF, array_slice($lines, 1));
- }
-
- protected static function extract($value, $expression)
- {
- preg_match('/@layout(\s*\(.*\))(\s*)/', $value, $matches);
- return str_replace(array("('", "')"), '', $matches[1]);
- }
-
- protected static function compile_comments($value)
- {
- $value = preg_replace('/\{\{--(.+?)(--\}\})?\n/', "<?php // $1 ?>", $value);
- return preg_replace('/\{\{--((.|\s)*?)--\}\}/', "<?php /* $1 */ ?>\n", $value);
- }
-
- protected static function compile_echos($value)
- {
- return preg_replace('/\{\{(.+?)\}\}/', '<?php echo $1; ?>', $value);
- }
-
- protected static function compile_forelse($value)
- {
- preg_match_all('/(\s*)@forelse(\s*\(.*\))(\s*)/', $value, $matches);
- foreach ($matches[0] as $forelse)
- {
- preg_match('/\s*\(\s*(\S*)\s/', $forelse, $variable);
-
-
-
- $if = "<?php if (count({$variable[1]}) > 0): ?>";
- $search = '/(\s*)@forelse(\s*\(.*\))/';
- $replace = '$1'.$if.'<?php foreach$2: ?>';
- $blade = preg_replace($search, $replace, $forelse);
-
-
-
- $value = str_replace($forelse, $blade, $value);
- }
- return $value;
- }
-
- protected static function compile_empty($value)
- {
- return str_replace('@empty', '<?php endforeach; ?><?php else: ?>', $value);
- }
-
- protected static function compile_endforelse($value)
- {
- return str_replace('@endforelse', '<?php endif; ?>', $value);
- }
-
- protected static function compile_structure_openings($value)
- {
- $pattern = '/(\s*)@(if|elseif|foreach|for|while)(\s*\(.*\))/';
- return preg_replace($pattern, '$1<?php $2$3: ?>', $value);
- }
-
- protected static function compile_structure_closings($value)
- {
- $pattern = '/(\s*)@(endif|endforeach|endfor|endwhile|break)(\s*)/';
- return preg_replace($pattern, '$1<?php $2; ?>$3', $value);
- }
-
- protected static function compile_else($value)
- {
- return preg_replace('/(\s*)@(else)(\s*)/', '$1<?php $2: ?>$3', $value);
- }
-
- protected static function compile_unless($value)
- {
- $pattern = '/(\s*)@unless(\s*\(.*\))/';
- return preg_replace($pattern, '$1<?php if( ! ($2)): ?>', $value);
- }
-
- protected static function compile_endunless($value)
- {
- return str_replace('@endunless', '<?php endif; ?>', $value);
- }
-
- protected static function compile_includes($value)
- {
- $pattern = static::matcher('include');
- return preg_replace($pattern, '$1<?php echo view$2->with(get_defined_vars())->render(); ?>', $value);
- }
-
- protected static function compile_render($value)
- {
- $pattern = static::matcher('render');
- return preg_replace($pattern, '$1<?php echo render$2; ?>', $value);
- }
-
- protected static function compile_render_each($value)
- {
- $pattern = static::matcher('render_each');
- return preg_replace($pattern, '$1<?php echo render_each$2; ?>', $value);
- }
-
- protected static function compile_yields($value)
- {
- $pattern = static::matcher('yield');
- return preg_replace($pattern, '$1<?php echo \\Laravel\\Section::yield$2; ?>', $value);
- }
-
- protected static function compile_yield_sections($value)
- {
- $replace = '<?php echo \\Laravel\\Section::yield_section(); ?>';
- return str_replace('@yield_section', $replace, $value);
- }
-
- protected static function compile_section_start($value)
- {
- $pattern = static::matcher('section');
- return preg_replace($pattern, '$1<?php \\Laravel\\Section::start$2; ?>', $value);
- }
-
- protected static function compile_section_end($value)
- {
- return preg_replace('/@endsection/', '<?php \\Laravel\\Section::stop(); ?>', $value);
- }
-
- protected static function compile_extensions($value)
- {
- foreach (static::$extensions as $compiler)
- {
- $value = $compiler($value);
- }
- return $value;
- }
-
- public static function matcher($function)
- {
- return '/(\s*)@'.$function.'(\s*\(.*\))/';
- }
-
- public static function compiled($path)
- {
- return path('storage').'views/'.md5($path);
- }
- }
|