added section class. refactored session for ioc usage. improved controller. added yielding and sections to blade. general blade improvements.

This commit is contained in:
Taylor Otwell
2011-11-08 20:08:20 -06:00
parent d05f4fa0db
commit a1e82d2166
17 changed files with 440 additions and 100 deletions

View File

@@ -2,6 +2,21 @@
class Blade {
/**
* All of the compiler functions used by Blade.
*
* @var array
*/
protected static $compilers = array(
'echos',
'structure_openings',
'structure_closings',
'else',
'yields',
'section_start',
'section_end',
);
/**
* Compiles the specified file containing Blade pseudo-code into valid PHP.
*
@@ -10,18 +25,38 @@ class Blade {
*/
public static function compile($path)
{
$value = file_get_contents($path);
return static::compile_string(file_get_contents($path));
}
return static::closings(static::openings(static::echos($value)));
/**
* Compiles the given string containing Blade pseudo-code into valid PHP.
*
* @param string $value
* @return string
*/
public static function compile_string($value)
{
foreach (static::$compilers as $compiler)
{
$method = "compile_{$compiler}";
$value = static::$method($value);
}
return $value;
}
/**
* Rewrites Blade echo statements into PHP echo statements.
*
* Blade echo statements are simply PHP statement enclosed within double curly
* braces. For example, {{$content}} will simply echo out the content variable
* to the output buffer.
*
* @param string $value
* @return string
*/
protected static function echos($value)
protected static function compile_echos($value)
{
return preg_replace('/\{\{(.+?)\}\}/', '<?php echo $1; ?>', $value);
}
@@ -29,12 +64,18 @@ class Blade {
/**
* Rewrites Blade structure openings into PHP structure openings.
*
* By "structures", we mean the if, elseif, foreach, for, and while statements.
* All of these structures essentially have the same format, and can be lumped
* into a single regular expression.
*
* @param string $value
* @return string
*/
protected static function openings($value)
protected static function compile_structure_openings($value)
{
return preg_replace('/@(if|elseif|foreach|for|while)(\s*\(.*?\))\:/', '<?php $1$2: ?>', $value);
$pattern = '/@(if|elseif|foreach|for|while)(\s*\(.*?\))/';
return preg_replace($pattern, '<?php $1$2: ?>', $value);
}
/**
@@ -43,12 +84,65 @@ class Blade {
* @param string $value
* @return string
*/
protected static function closings($value)
protected static function compile_structure_closings($value)
{
$value = preg_replace('/(\s*)@(else)(.*?)\:/', '$1<?php $2$3: ?>', $value);
$value = preg_replace('/(\s*)@(endif|endforeach|endfor|endwhile)(\s*)/', '$1<?php $2; ?> $3', $value);
$pattern = '/@(endif|endforeach|endfor|endwhile)(\s*)/';
return $value;
return preg_replace($pattern, '<?php $1; ?>$2', $value);
}
/**
* Rewrites Blade else statements into PHP else statements.
*
* @param string $value
* @return string
*/
protected static function compile_else($value)
{
return preg_replace('/(\s*)@(else)(\s*)/', '$1<?php $2: ?>$3', $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 = '/(\s*)@yield(\s*\(.*?\))/';
return preg_replace($pattern, '$1<?php echo \\Laravel\\Section::yield$2; ?>', $value);
}
/**
* Rewrites Blade @section statements into Section statements.
*
* The Blade @section statement is a shortcut to the Section::start method.
*
* @param string $value
* @return string
*/
protected static function compile_section_start($value)
{
$pattern = '/(\s*)@section(\s*\(.*?\))/';
return preg_replace($pattern, '$1<?php \\Laravel\\Section::start$2; ?>', $value);
}
/**
* Rewrites Blade @endsection statements into Section statements.
*
* The Blade @endsection statement is a shortcut to the Section::stop method.
*
* @param string $value
* @return string
*/
protected static function compile_section_end($value)
{
return preg_replace('/@endsection/', '<?php \\Laravel\\Section::stop(); ?>', $value);
}
}