Browse Source

fixing merge issues on blade pull

Dayle Rees 12 years ago
parent
commit
7b5e37a316
1 changed files with 76 additions and 50 deletions
  1. 76 50
      laravel/blade.php

+ 76 - 50
laravel/blade.php

@@ -149,19 +149,19 @@ class Blade {
 			return $value;
 		}
 
-		// First we'll split out the lines of the template so we can get the
-		// layout from the top of the template. By convention it must be
-		// located on the first line of the template contents.
-		$lines = preg_split("/(\r?\n)/", $value);
-
-		$pattern = static::matcher('layout');
-
-		$lines[] = preg_replace($pattern, '$1@include$2', $lines[0]);
+		// First we'll get the layout from the top of the template and remove it.
+		// Then it is replaced with @include and attached to the end.
+		// By convention it must be located on the first line of the template contents.
+		preg_replace_callback(
+			'/^@layout(\s*?\(.+?\))(\r?\n)?/',
+			function($matches) use (&$value)
+			{
+				$value = substr( $value, strlen( $matches[0] ) ).CRLF.'@include'.$matches[1];
+			},
+			$value
+		);
 
-		// We will add a "render" statement to the end of the templates and
-		// then slice off the "@layout" shortcut from the start so the
-		// sections register before the parent template renders.
-		return implode(CRLF, array_slice($lines, 1));
+		return $value;
 	}
 
 	/**
@@ -172,9 +172,10 @@ class Blade {
 	 */
 	protected static function extract($value, $expression)
 	{
-		preg_match('/@layout(\s*\(.*\))(\s*)/', $value, $matches);
-
-		return str_replace(array("('", "')"), '', $matches[1]);
+		if ( preg_match("/@layout\s*?\(\s*?'(.+?)'\s*?\)/", $value, $matches))
+		{
+			return trim( $matches[1] );
+		}
 	}
 
 	/**
@@ -185,9 +186,9 @@ class Blade {
 	 */
 	protected static function compile_comments($value)
 	{
-		$value = preg_replace('/\{\{--(.+?)(--\}\})?\n/', "<?php // $1 ?>", $value);
+		$value = preg_replace('/\{\{--(.+?)(--\}\})?\n/s', "<?php // $1 ?>", $value);
 
-		return preg_replace('/\{\{--((.|\s)*?)--\}\}/', "<?php /* $1 */ ?>\n", $value);
+		return preg_replace('/\{\{--((.|\s)*?)--\}\}/s', "<?php /* $1 */ ?>\n", $value);
 	}
 
 	/**
@@ -198,7 +199,7 @@ class Blade {
 	 */
 	protected static function compile_echos($value)
 	{
-		return preg_replace('/\{\{(.+?)\}\}/', '<?php echo $1; ?>', $value);
+		return preg_replace('/\{\{(.+?)\}\}/s', '<?php echo $1; ?>', $value);
 	}
 
 	/**
@@ -209,27 +210,21 @@ class Blade {
 	 */
 	protected static function compile_forelse($value)
 	{
-		preg_match_all('/(\s*)@forelse(\s*\(.*\))(\s*)/', $value, $matches);
+		preg_match_all('/@forelse\s*?\(\s*?\$(.+?)\s*?as\s*?\$(.+?)\s*?\)/', $value, $matches, PREG_SET_ORDER );
+		
+		if ( count($matches) < 1 ) return $value;
 
-		foreach ($matches[0] as $forelse)
+		foreach ($matches as $forelse)
 		{
-			preg_match('/\s*\(\s*(\S*)\s/', $forelse, $variable);
-
 			// Once we have extracted the variable being looped against, we can add
 			// an if statement to the start of the loop that checks if the count
 			// of the variable being looped against is greater than zero.
-			$if = "<?php if (count({$variable[1]}) > 0): ?>";
-
-			$search = '/(\s*)@forelse(\s*\(.*\))/';
-
-			$replace = '$1'.$if.'<?php foreach$2: ?>';
-
-			$blade = preg_replace($search, $replace, $forelse);
+			$replace = '<?php if (count($'.$forelse[1].') > 0): foreach ($'.$forelse[1].' as $'.$forelse[2].'): ?>';
 
 			// Finally, once we have the check prepended to the loop we'll replace
 			// all instances of this forelse syntax in the view content of the
 			// view being compiled to Blade syntax with real PHP syntax.
-			$value = str_replace($forelse, $blade, $value);
+			$value = str_replace($forelse[0], $replace, $value);
 		}
 
 		return $value;
@@ -243,7 +238,7 @@ class Blade {
 	 */
 	protected static function compile_empty($value)
 	{
-		return str_replace('@empty', '<?php endforeach; ?><?php else: ?>', $value);
+		return str_replace('@empty', '<?php endforeach; else: ?>', $value);
 	}
 
 	/**
@@ -265,9 +260,42 @@ class Blade {
 	 */
 	protected static function compile_structure_openings($value)
 	{
-		$pattern = '/(\s*)@(if|elseif|foreach|for|while)(\s*\(.*\))/';
-
-		return preg_replace($pattern, '$1<?php $2$3: ?>', $value);
+		preg_replace_callback(
+			'/@(if|elseif|foreach|for|while)(\s*?)(\([^\n\r\t]+\))/',
+			function($matches) use (&$value)
+			{
+				if(count( $matches ) === 4)
+				{
+					$open  = 0;
+					$close = 0;
+					$cut   = 0;
+					$len   = strlen($matches[3]);
+					for($i = 0; $i < $len; $i++)
+					{
+						if($matches[3][$i] === '(' )
+						{
+							$open++;
+						}
+						if($matches[3][$i] === ')' )
+						{
+							$close++;
+						}
+						if($open !== 0 && ($open === $close))
+						{
+							break;
+						}
+					}
+					$condition = substr($matches[3], 0, ($i + 1));
+					$value = str_replace(
+						'@'.$matches[1].$matches[2].$condition,
+						'<?php '.$matches[1].$condition.': ?>',
+						$value
+					);
+				}
+			},
+			$value
+		);
+		return $value;
 	}
 
 	/**
@@ -278,9 +306,9 @@ class Blade {
 	 */
 	protected static function compile_structure_closings($value)
 	{
-		$pattern = '/(\s*)@(endif|endforeach|endfor|endwhile|break)(\s*)/';
+		$pattern = '/@(endif|endforeach|endfor|endwhile|break|continue)/';
 
-		return preg_replace($pattern, '$1<?php $2; ?>$3', $value);
+		return preg_replace($pattern, '<?php $1; ?>', $value);
 	}
 
 	/**
@@ -291,7 +319,7 @@ class Blade {
 	 */
 	protected static function compile_else($value)
 	{
-		return preg_replace('/(\s*)@(else)(\s*)/', '$1<?php $2: ?>$3', $value);
+		return str_replace( '@else', '<?php else: ?>', $value);
 	}
 
 	/**
@@ -302,9 +330,9 @@ class Blade {
 	 */
 	protected static function compile_unless($value)
 	{
-		$pattern = '/(\s*)@unless(\s*\(.*\))/';
+		$pattern = static::matcher('unless');
 
-		return preg_replace($pattern, '$1<?php if( ! ($2)): ?>', $value);
+		return preg_replace($pattern, '<?php if( ! ($1)): ?>', $value);
 	}
 
 	/**
@@ -328,7 +356,7 @@ class Blade {
 	{
 		$pattern = static::matcher('include');
 
-		return preg_replace($pattern, '$1<?php echo view$2->with(get_defined_vars())->render(); ?>', $value);
+		return preg_replace($pattern, '<?php echo view$1->with(get_defined_vars())->render(); ?>', $value);
 	}
 
 	/**
@@ -341,7 +369,7 @@ class Blade {
 	{
 		$pattern = static::matcher('render');
 
-		return preg_replace($pattern, '$1<?php echo render$2; ?>', $value);
+		return preg_replace($pattern, '<?php echo render$1; ?>', $value);
 	}
 
 	/**
@@ -354,7 +382,7 @@ class Blade {
 	{
 		$pattern = static::matcher('render_each');
 
-		return preg_replace($pattern, '$1<?php echo render_each$2; ?>', $value);
+		return preg_replace($pattern, '<?php echo render_each$1; ?>', $value);
 	}
 
 	/**
@@ -369,7 +397,7 @@ class Blade {
 	{
 		$pattern = static::matcher('yield');
 
-		return preg_replace($pattern, '$1<?php echo \\Laravel\\Section::yield$2; ?>', $value);
+		return preg_replace($pattern, '<?php echo \\Laravel\\Section::yield$1; ?>', $value);
 	}
 
 	/**
@@ -379,9 +407,7 @@ class Blade {
 	 */
 	protected static function compile_yield_sections($value)
 	{
-		$replace = '<?php echo \\Laravel\\Section::yield_section(); ?>';
-
-		return str_replace('@yield_section', $replace, $value);
+		return str_replace('@yield_section', '<?php echo \\Laravel\\Section::yield_section(); ?>', $value);
 	}
 
 	/**
@@ -396,7 +422,7 @@ class Blade {
 	{
 		$pattern = static::matcher('section');
 
-		return preg_replace($pattern, '$1<?php \\Laravel\\Section::start$2; ?>', $value);
+		return preg_replace($pattern, '<?php \\Laravel\\Section::start$1; ?>', $value);
 	}
 
 	/**
@@ -409,7 +435,7 @@ class Blade {
 	 */
 	protected static function compile_section_end($value)
 	{
-		return preg_replace('/@endsection/', '<?php \\Laravel\\Section::stop(); ?>', $value);
+		return str_replace('@endsection', '<?php \\Laravel\\Section::stop(); ?>', $value);
 	}
 
 	/**
@@ -436,7 +462,7 @@ class Blade {
 	 */
 	public static function matcher($function)
 	{
-		return '/(\s*)@'.$function.'(\s*\(.*\))/';
+		return '/@'.$function.'\s*?(\(.+?\))/';
 	}
 
 	/**
@@ -450,4 +476,4 @@ class Blade {
 		return path('storage').'views/'.md5($path);
 	}
 
-}
+}