Commit 93e21e92 authored by Taylor Otwell's avatar Taylor Otwell

Added support for true tempting.

Signed-off-by: 's avatarTaylor Otwell <taylorotwell@gmail.com>
parent 8e8b0b8c
...@@ -2,35 +2,13 @@ ...@@ -2,35 +2,13 @@
class Blade { class Blade {
/**
* The cache key for the extension tree.
*
* @var string
*/
const cache = 'laravel.blade.extensions';
/**
* An array containing the template extension tree.
*
* @var array
*/
public static $extensions;
/**
* The original extension tree loaded from the cache.
*
* @var array
*/
public static $original;
/** /**
* All of the compiler functions used by Blade. * All of the compiler functions used by Blade.
* *
* @var array * @var array
*/ */
protected static $compilers = array( protected static $compilers = array(
'extends', 'layouts',
'includes',
'echos', 'echos',
'forelse', 'forelse',
'empty', 'empty',
...@@ -38,6 +16,9 @@ class Blade { ...@@ -38,6 +16,9 @@ class Blade {
'structure_openings', 'structure_openings',
'structure_closings', 'structure_closings',
'else', 'else',
'includes',
'yields',
'yield_sections',
'section_start', 'section_start',
'section_end', 'section_end',
); );
...@@ -49,8 +30,6 @@ class Blade { ...@@ -49,8 +30,6 @@ class Blade {
*/ */
public static function sharpen() public static function sharpen()
{ {
static::extensions();
Event::listen(View::engine, function($view) Event::listen(View::engine, function($view)
{ {
// The Blade view engine should only handle the rendering of views which // The Blade view engine should only handle the rendering of views which
...@@ -80,42 +59,6 @@ class Blade { ...@@ -80,42 +59,6 @@ class Blade {
}); });
} }
/**
* Load the extension tree so we can correctly invalidate caches.
*
* @return void
*/
protected static function extensions()
{
// The entire view extension tree is cached so we can check for expired
// views anywhere in the tree. This allows us to recompile a child
// view if any of its parent views change throughout the tree.
static::$extensions = Cache::get(Blade::cache);
static::$original = static::$extensions;
// If no extension tree was present, we need to invalidate every cache
// since we have no way of knowing which views needs to be compiled
// since we don't know any of their parent views.
if (is_null(static::$extensions))
{
static::flush();
static::$extensions = array();
}
// We'll hook into the "done" event of Laravel and write out the tree
// of extensions if it was changed during the course of the request.
// The tree would change if new templates were rendered, etc.
Event::listen('laravel.done', function()
{
if (Blade::$extensions !== Blade::$original)
{
Cache::forever(Blade::cache, Blade::$extensions);
}
});
}
/** /**
* Determine if a view is "expired" and needs to be re-compiled. * Determine if a view is "expired" and needs to be re-compiled.
* *
...@@ -128,39 +71,7 @@ class Blade { ...@@ -128,39 +71,7 @@ class Blade {
{ {
$compiled = static::compiled($path); $compiled = static::compiled($path);
return filemtime($path) > filemtime($compiled) or static::expired_parent($view); return filemtime($path) > filemtime(static::compiled($path));
}
/**
* Determine if the given view has an expired parent view.
*
* @param string $view
* @return bool
*/
protected static function expired_parent($view)
{
// If the view is extending another view, we need to recursively check
// whether any of the extended views have expired, all the way up to
// the top most parent view of the extension chain.
if (isset(static::$extensions[$view]))
{
$e = static::$extensions[$view];
return static::expired($e['view'], $e['path']);
}
return false;
}
/**
* Get the fully qualified path for a compiled view.
*
* @param string $view
* @return string
*/
public static function compiled($path)
{
return path('storage').'views/'.md5($path);
} }
/** /**
...@@ -193,101 +104,44 @@ class Blade { ...@@ -193,101 +104,44 @@ class Blade {
return $value; return $value;
} }
/** protected static function compile_layouts($value)
* Rewrites Blade extended templates into valid PHP.
*
* @param string $value
* @param View $view
* @return string
*/
protected static function compile_extends($value, $view)
{ {
// If the view doesn't begin with @extends, we don't need to do anything // If the Blade template is not using "layouts", we'll just return it
// and can simply return the view to be parsed by the rest of Blade's // it unchanged since there is nothing to do with layouts and we'll
// compilers like any other normal Blade view would be compiled. // just let the other Blade compilers handle it.
if (is_null($view) or ! starts_with($value, '@extends')) if ( ! starts_with($value, '@layout'))
{ {
return $value; return $value;
} }
// First we need to parse the parent template from the extends keyword // First we'll split out the lines of the template so we can get the
// so we know which parent to render. We will remove the extends // the layout from the top of the template. By convention it must
// from the template after we have extracted the parent. // be located on the first line of the template contents.
$template = static::extract_template($value); $lines = preg_split("/(\r?\n)/", $value);
$path = static::store_extended($value, $view); $layout = static::extract($lines[0], '@layout');
// Once we have stored a copy of the view without the "extends" clause // We will add a "render" statement to the end of the templates and
// we can load up that stored view and render it. The extending view // and then slice off the @layout shortcut from the start so the
// should only be using "sections", so we don't need the output. // sections register before the parent template renders.
View::make("path: {$path}", $view->data())->render(); $lines[] = "<?php echo render('{$layout}'); ?>";
$parent = View::make($template); return implode(CRLF, array_slice($lines, 1));
// Finally we will make and return the parent view as the output of
// the compilation. We'll touch the parent to force it to compile
// when it is rendered so we can make sure we're all fresh.
touch($parent->path);
static::log_extension($view, $parent);
return $parent->render();
} }
/** /**
* Extract the parent template name from an extending view. * Extract a variable value out of a Blade expression.
* *
* @param string $value * @param string $value
* @return string * @return string
*/ */
protected static function extract_template($value) protected static function extract($value, $expression)
{ {
preg_match('/@extends(\s*\(.*\))(\s*)/', $value, $matches); preg_match('/'.$expression.'(\s*\(.*\))(\s*)/', $value, $matches);
return str_replace(array("('", "')"), '', $matches[1]); return str_replace(array("('", "')"), '', $matches[1]);
} }
/**
* Store an extended view in the view storage.
*
* @param string $value
* @param View $view
* @return array
*/
protected static function store_extended($value, $view)
{
$value = preg_replace('/@extends(\s*\(.*\))(\s*)/', '', $value);
file_put_contents($path = static::compiled($view->path.'_extended').BLADE_EXT, $value);
return $path;
}
/**
* Log a view extension for a given view in the extension tree.
*
* @param View $view
* @param View $parent
* @return void
*/
protected static function log_extension($view, $parent)
{
static::$extensions[$view->view] = array('view' => $parent->view, 'path' => $parent->path);
}
/**
* Rewrites Blade "include" statements to valid PHP.
*
* @param string $value
* @return string
*/
protected static function compile_includes($value)
{
$pattern = '/\{\{(\s*)include(\s*\(.*\))(\s*)\}\}/';
return preg_replace($pattern, '<?php echo render$2->with(get_defined_vars()); ?>', $value);
}
/** /**
* Rewrites Blade echo statements into PHP echo statements. * Rewrites Blade echo statements into PHP echo statements.
* *
...@@ -309,18 +163,13 @@ class Blade { ...@@ -309,18 +163,13 @@ class Blade {
{ {
preg_match_all('/(\s*)@forelse(\s*\(.*\))(\s*)/', $value, $matches); preg_match_all('/(\s*)@forelse(\s*\(.*\))(\s*)/', $value, $matches);
// First we'll loop through all of the "@forelse" lines. We need to
// wrap each loop in an if/else statement that checks the count
// of the variable that is being iterated by the loop.
if (isset($matches[0]))
{
foreach ($matches[0] as $forelse) foreach ($matches[0] as $forelse)
{ {
preg_match('/\$[^\s]*/', $forelse, $variable); preg_match('/\$[^\s]*/', $forelse, $variable);
// Once we have extracted the variable being looped against, we can // Once we have extracted the variable being looped against, we can add
// prepend an "if" statmeent to the start of the loop that checks // an if statmeent to the start of the loop that checks if the count
// that the count of the variable is greater than zero. // of the variable being looped against is greater than zero.
$if = "<?php if (count({$variable[0]}) > 0): ?>"; $if = "<?php if (count({$variable[0]}) > 0): ?>";
$search = '/(\s*)@forelse(\s*\(.*\))/'; $search = '/(\s*)@forelse(\s*\(.*\))/';
...@@ -329,12 +178,11 @@ class Blade { ...@@ -329,12 +178,11 @@ class Blade {
$blade = preg_replace($search, $replace, $forelse); $blade = preg_replace($search, $replace, $forelse);
// Finally, once we have the check prepended to the loop, we will // Finally, once we have the check prepended to the loop we'll replace
// replace all instances of this "forelse" syntax in the view // all instances of this "forelse" syntax in the view content of the
// content of the view being compiled to Blade syntax. // view being compiled to Blade syntax with real syntax.
$value = str_replace($forelse, $blade, $value); $value = str_replace($forelse, $blade, $value);
} }
}
return $value; return $value;
} }
...@@ -398,6 +246,46 @@ class Blade { ...@@ -398,6 +246,46 @@ class Blade {
return preg_replace('/(\s*)@(else)(\s*)/', '$1<?php $2: ?>$3', $value); return preg_replace('/(\s*)@(else)(\s*)/', '$1<?php $2: ?>$3', $value);
} }
/**
* Rewrites Blade @include statements into valid PHP.
*
* @param string $value
* @return string
*/
protected static function compile_includes($value)
{
$pattern = static::matcher('include');
return preg_replace($pattern, '$1<?php echo render$2->with(get_defined_vars()); ?>', $value);
}
/**
* Rewrites Blade @yield statements into Section statements.
*
* The Blade @yield statement is a shortcut to the Section::yield method.
*
* @param string $value
* @return string
*/
protected static function compile_yields($value)
{
$pattern = static::matcher('yield');
return preg_replace($pattern, '$1<?php echo \\Laravel\\Section::yield$2; ?>', $value);
}
/**
* Rewrites Blade yield section statements into valid PHP.
*
* @return string
*/
protected static function compile_yield_sections($value)
{
$replace = '<?php echo \\Laravel\\Section::yield_section(); ?>';
return str_replace('@yield_section', $replace, $value);
}
/** /**
* Rewrites Blade @section statements into Section statements. * Rewrites Blade @section statements into Section statements.
* *
...@@ -438,21 +326,14 @@ class Blade { ...@@ -438,21 +326,14 @@ class Blade {
} }
/** /**
* Remove all of the cached views from storage. * Get the fully qualified path for a compiled view.
* *
* @return void * @param string $view
* @return string
*/ */
protected static function flush() public static function compiled($path)
{
$items = new fIterator(path('storage').'views');
foreach ($items as $item)
{
if ($item->isFile() and $item->getBasename() !== '.gitignore')
{ {
@unlink($item->getRealPath()); return path('storage').'views/'.md5($path);
}
}
} }
} }
\ No newline at end of file
...@@ -14,7 +14,7 @@ class Section { ...@@ -14,7 +14,7 @@ class Section {
* *
* @var array * @var array
*/ */
protected static $last = array(); public static $last = array();
/** /**
* Start injecting content into a section. * Start injecting content into a section.
...@@ -33,10 +33,15 @@ class Section { ...@@ -33,10 +33,15 @@ class Section {
*/ */
public static function start($section, $content = '') public static function start($section, $content = '')
{ {
if ($content === '') ob_start() and static::$last[] = $section; if ($content === '')
{
ob_start() and static::$last[] = $section;
}
else
{
static::append($section, $content); static::append($section, $content);
} }
}
/** /**
* Inject inline content into a section. * Inject inline content into a section.
...@@ -57,14 +62,26 @@ class Section { ...@@ -57,14 +62,26 @@ class Section {
static::start($section, $content); static::start($section, $content);
} }
/**
* Stop injecting content into a section and return its contents.
*
* @return string
*/
public static function yield_section()
{
return static::yield(static::stop());
}
/** /**
* Stop injecting content into a section. * Stop injecting content into a section.
* *
* @return void * @return string
*/ */
public static function stop() public static function stop()
{ {
static::append(array_pop(static::$last), ob_get_clean()); static::append($last = array_pop(static::$last), ob_get_clean());
return $last;
} }
/** /**
...@@ -78,11 +95,13 @@ class Section { ...@@ -78,11 +95,13 @@ class Section {
{ {
if (isset(static::$sections[$section])) if (isset(static::$sections[$section]))
{ {
$content = static::$sections[$section].PHP_EOL.$content; static::$sections[$section] = str_replace('@parent', $content, static::$sections[$section]);
} }
else
{
static::$sections[$section] = $content; static::$sections[$section] = $content;
} }
}
/** /**
* Get the string contents of a section. * Get the string contents of a section.
......
...@@ -349,11 +349,11 @@ class View implements ArrayAccess { ...@@ -349,11 +349,11 @@ class View implements ArrayAccess {
// All nested views and responses are evaluated before the main view. // All nested views and responses are evaluated before the main view.
// This allows the assets used by nested views to be added to the // This allows the assets used by nested views to be added to the
// asset container before the main view is evaluated. // asset container before the main view is evaluated.
foreach ($data as &$value) foreach ($data as $key => $value)
{ {
if ($value instanceof View or $value instanceof Response) if ($value instanceof View or $value instanceof Response)
{ {
$value = $value->render(); $data[$key] = $value->render();
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment