blade.php 3.34 KB
Newer Older
1
<?php namespace Laravel; defined('APP_PATH') or die('No direct script access.');
2 3 4 5

class Blade {

	/**
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
	 * 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',
	);

	/**
21
	 * Compiles the specified file containing Blade pseudo-code into valid PHP.
22 23 24 25
	 *
	 * @param  string  $path
	 * @return string
	 */
26
	public static function compile($path)
27
	{
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
		return static::compile_string(file_get_contents($path));
	}

	/**
	 * 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);
		}
45

46
		return $value;
47 48 49 50 51 52 53 54
	}

	/**
	 * Rewrites Blade echo statements into PHP echo statements.
	 *
	 * @param  string  $value
	 * @return string
	 */
55
	protected static function compile_echos($value)
56
	{
57
		return preg_replace('/\{\{(.+?)\}\}/', '<?php echo $1; ?>', $value);
58 59 60 61 62 63 64 65
	}

	/**
	 * Rewrites Blade structure openings into PHP structure openings.
	 *
	 * @param  string  $value
	 * @return string
	 */
66
	protected static function compile_structure_openings($value)
67
	{
Taylor Otwell committed
68
		$pattern = '/(\s*)@(if|elseif|foreach|for|while)(\s*\(.*\))/';
69

70
		return preg_replace($pattern, '$1<?php $2$3: ?>', $value);
71 72 73 74 75 76 77 78
	}

	/**
	 * Rewrites Blade structure closings into PHP structure closings.
	 *
	 * @param  string  $value
	 * @return string
	 */
79
	protected static function compile_structure_closings($value)
80
	{
81
		$pattern = '/(\s*)@(endif|endforeach|endfor|endwhile)(\s*)/';
82

83
		return preg_replace($pattern, '$1<?php $2; ?>$3', $value);
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
	}

	/**
	 * 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)
	{
107
		$pattern = static::matcher('yield');
108 109 110 111 112 113 114 115 116 117 118 119 120 121

		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)
	{
122
		$pattern = static::matcher('section');
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

		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);
138 139
	}

140 141 142 143 144 145 146 147
	/**
	 * Get the regular expression for a generic Blade function.
	 *
	 * @param  string  $function
	 * @return string
	 */
	protected static function matcher($function)
	{
Taylor Otwell committed
148
		return '/(\s*)@'.$function.'(\s*\(.*\))/';
149 150
	}

151
}