diff --git a/laravel/blade.php b/laravel/blade.php index 0deed20e..14cced8a 100644 --- a/laravel/blade.php +++ b/laravel/blade.php @@ -149,19 +149,19 @@ protected static function compile_layouts($value) 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); + // 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 + ); - $pattern = static::matcher('layout'); - - $lines[] = preg_replace($pattern, '$1@include$2', $lines[0]); - - // 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 @@ protected static function compile_layouts($value) */ 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 @@ protected static function extract($value, $expression) */ protected static function compile_comments($value) { - $value = preg_replace('/\{\{--(.+?)(--\}\})?\n/', "", $value); + $value = preg_replace('/\{\{--(.+?)(--\}\})?\n/s', "", $value); - return preg_replace('/\{\{--((.|\s)*?)--\}\}/', "\n", $value); + return preg_replace('/\{\{--((.|\s)*?)--\}\}/s', "\n", $value); } /** @@ -198,7 +199,7 @@ protected static function compile_comments($value) */ protected static function compile_echos($value) { - return preg_replace('/\{\{(.+?)\}\}/', '', $value); + return preg_replace('/\{\{(.+?)\}\}/s', '', $value); } /** @@ -209,27 +210,21 @@ protected static function compile_echos($value) */ 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 = " 0): ?>"; - - $search = '/(\s*)@forelse(\s*\(.*\))/'; - - $replace = '$1'.$if.''; - - $blade = preg_replace($search, $replace, $forelse); + $replace = ' 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 @@ protected static function compile_forelse($value) */ protected static function compile_empty($value) { - return str_replace('@empty', '', $value); + return str_replace('@empty', '', $value); } /** @@ -265,9 +260,42 @@ protected static function compile_endforelse($value) */ protected static function compile_structure_openings($value) { - $pattern = '/(\s*)@(if|elseif|foreach|for|while)(\s*\(.*\))/'; - - return preg_replace($pattern, '$1', $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, + '', + $value + ); + } + }, + $value + ); + return $value; } /** @@ -278,9 +306,9 @@ protected static function compile_structure_openings($value) */ 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$3', $value); + return preg_replace($pattern, '', $value); } /** @@ -291,7 +319,7 @@ protected static function compile_structure_closings($value) */ protected static function compile_else($value) { - return preg_replace('/(\s*)@(else)(\s*)/', '$1$3', $value); + return str_replace( '@else', '', $value); } /** @@ -302,9 +330,9 @@ protected static function compile_else($value) */ protected static function compile_unless($value) { - $pattern = '/(\s*)@unless(\s*\(.*\))/'; + $pattern = static::matcher('unless'); - return preg_replace($pattern, '$1', $value); + return preg_replace($pattern, '', $value); } /** @@ -328,7 +356,7 @@ protected static function compile_includes($value) { $pattern = static::matcher('include'); - return preg_replace($pattern, '$1with(get_defined_vars())->render(); ?>', $value); + return preg_replace($pattern, 'with(get_defined_vars())->render(); ?>', $value); } /** @@ -341,7 +369,7 @@ protected static function compile_render($value) { $pattern = static::matcher('render'); - return preg_replace($pattern, '$1', $value); + return preg_replace($pattern, '', $value); } /** @@ -354,7 +382,7 @@ protected static function compile_render_each($value) { $pattern = static::matcher('render_each'); - return preg_replace($pattern, '$1', $value); + return preg_replace($pattern, '', $value); } /** @@ -369,7 +397,7 @@ protected static function compile_yields($value) { $pattern = static::matcher('yield'); - return preg_replace($pattern, '$1', $value); + return preg_replace($pattern, '', $value); } /** @@ -379,9 +407,7 @@ protected static function compile_yields($value) */ protected static function compile_yield_sections($value) { - $replace = ''; - - return str_replace('@yield_section', $replace, $value); + return str_replace('@yield_section', '', $value); } /** @@ -396,7 +422,7 @@ protected static function compile_section_start($value) { $pattern = static::matcher('section'); - return preg_replace($pattern, '$1', $value); + return preg_replace($pattern, '', $value); } /** @@ -409,7 +435,7 @@ protected static function compile_section_start($value) */ protected static function compile_section_end($value) { - return preg_replace('/@endsection/', '', $value); + return str_replace('@endsection', '', $value); } /** @@ -436,7 +462,7 @@ protected static function compile_extensions($value) */ public static function matcher($function) { - return '/(\s*)@'.$function.'(\s*\(.*\))/'; + return '/@'.$function.'\s*?(\(.+?\))/'; } /** @@ -450,4 +476,4 @@ public static function compiled($path) return path('storage').'views/'.md5($path); } -} \ No newline at end of file +}