From ddee5b7199cbb291916595078bcb3c44c6efba44 Mon Sep 17 00:00:00 2001 From: Tobias Orterer Date: Sat, 16 Jun 2012 20:02:03 -0700 Subject: [PATCH 1/5] Blade cleanup sanitized most functions and regular expressions. made the bigger functions a bit more solid, flexible in what they ignore and simplified various processes. --- laravel/blade.php | 83 ++++++++++++++++++++++------------------------- 1 file changed, 38 insertions(+), 45 deletions(-) diff --git a/laravel/blade.php b/laravel/blade.php index c362d57b..31273fe1 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] ); + } } /** @@ -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,9 @@ protected static function compile_endforelse($value) */ protected static function compile_structure_openings($value) { - $pattern = '/(\s*)@(if|elseif|foreach|for|while)(\s*\(.*\))/'; + $pattern = '/@(if|elseif|foreach|for|while)\s*?(\(.+?\))/'; - return preg_replace($pattern, '$1', $value); + return preg_replace($pattern, '', $value); } /** @@ -278,9 +273,9 @@ protected static function compile_structure_openings($value) */ protected static function compile_structure_closings($value) { - $pattern = '/(\s*)@(endif|endforeach|endfor|endwhile)(\s*)/'; + $pattern = '/@(endif|endforeach|endfor|endwhile)/'; - return preg_replace($pattern, '$1$3', $value); + return preg_replace($pattern, '', $value); } /** @@ -291,7 +286,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 +297,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 +323,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 +336,7 @@ protected static function compile_render($value) { $pattern = static::matcher('render'); - return preg_replace($pattern, '$1', $value); + return preg_replace($pattern, '', $value); } /** @@ -354,7 +349,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 +364,7 @@ protected static function compile_yields($value) { $pattern = static::matcher('yield'); - return preg_replace($pattern, '$1', $value); + return preg_replace($pattern, '', $value); } /** @@ -379,9 +374,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 +389,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 +402,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 +429,7 @@ protected static function compile_extensions($value) */ public static function matcher($function) { - return '/(\s*)@'.$function.'(\s*\(.*\))/'; + return '/@'.$function.'\s*?(\(.+?\))/'; } /** From be264ab181e32478fc6ed6bb989c0a28766038fd Mon Sep 17 00:00:00 2001 From: Tobias Orterer Date: Thu, 21 Jun 2012 00:09:08 -0700 Subject: [PATCH 2/5] added break function to closing structure --- laravel/blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/laravel/blade.php b/laravel/blade.php index 31273fe1..7e4e2fdb 100644 --- a/laravel/blade.php +++ b/laravel/blade.php @@ -273,7 +273,7 @@ protected static function compile_structure_openings($value) */ protected static function compile_structure_closings($value) { - $pattern = '/@(endif|endforeach|endfor|endwhile)/'; + $pattern = '/@(endif|endforeach|endfor|endwhile|break)/'; return preg_replace($pattern, '', $value); } From a04f38c2625deb7e26a52147c6e8fd45581cffe2 Mon Sep 17 00:00:00 2001 From: Tobsn Date: Wed, 27 Jun 2012 03:46:31 -0700 Subject: [PATCH 3/5] Now with Balanced Parentheses! MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit it's not regex, its not posix, but it can fly… finally balanced parentheses to fix all the () issue. it's ugly but it's fast and works with all kinds of combinations. it basically just counts open vs. closed and if they are even again it knows exactly where the condition is and does not cut into html or cut off too much. --- laravel/blade.php | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/laravel/blade.php b/laravel/blade.php index 7e4e2fdb..f224614a 100644 --- a/laravel/blade.php +++ b/laravel/blade.php @@ -260,9 +260,42 @@ protected static function compile_endforelse($value) */ protected static function compile_structure_openings($value) { - $pattern = '/@(if|elseif|foreach|for|while)\s*?(\(.+?\))/'; - - return preg_replace($pattern, '', $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; } /** From 6cb2ddad641d39f97d1834245b8630ac46698c08 Mon Sep 17 00:00:00 2001 From: Tobsn Date: Wed, 25 Jul 2012 00:21:32 +0200 Subject: [PATCH 4/5] multiline echo and comments for issue #647 --- laravel/blade.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/laravel/blade.php b/laravel/blade.php index f224614a..efc88aa5 100644 --- a/laravel/blade.php +++ b/laravel/blade.php @@ -186,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); } /** @@ -199,7 +199,7 @@ protected static function compile_comments($value) */ protected static function compile_echos($value) { - return preg_replace('/\{\{(.+?)\}\}/', '', $value); + return preg_replace('/\{\{(.+?)\}\}/s', '', $value); } /** From 34c746b4f8901a6b7ac61077d3074f1ff9d33798 Mon Sep 17 00:00:00 2001 From: Tobsn Date: Wed, 25 Jul 2012 00:23:31 +0200 Subject: [PATCH 5/5] added @continue like @break - as of request issue #1001 --- laravel/blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/laravel/blade.php b/laravel/blade.php index efc88aa5..b95bf6c2 100644 --- a/laravel/blade.php +++ b/laravel/blade.php @@ -306,7 +306,7 @@ function($matches) use (&$value) */ protected static function compile_structure_closings($value) { - $pattern = '/@(endif|endforeach|endfor|endwhile|break)/'; + $pattern = '/@(endif|endforeach|endfor|endwhile|break|continue)/'; return preg_replace($pattern, '', $value); }