diff --git a/laravel/blade.php b/laravel/blade.php index 15c8efde..f5bed6f4 100644 --- a/laravel/blade.php +++ b/laravel/blade.php @@ -105,7 +105,7 @@ public static function expired($view, $path) /** * Compiles the specified file containing Blade pseudo-code into valid PHP. * - * @param string $view + * @param string $path * @return string */ public static function compile($view) @@ -149,33 +149,31 @@ protected static function compile_layouts($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 + // layout from the top of the template. 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 - ); + $lines = preg_split("/(\r?\n)/", $value); - return $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)); } /** * Extract a variable value out of a Blade expression. * * @param string $value - * @param string $expression * @return string */ protected static function extract($value, $expression) { - if ( preg_match("/@layout\s*?\(\s*?'(.+?)'\s*?\)/", $value, $matches)) - { - return trim( $matches[1] ); - } + preg_match('/@layout(\s*\(.*\))(\s*)/', $value, $matches); + + return str_replace(array("('", "')"), '', $matches[1]); } /** @@ -186,9 +184,9 @@ protected static function extract($value, $expression) */ protected static function compile_comments($value) { - $value = preg_replace('/\{\{--(.*?)--\}\}/', "", $value); - - return preg_replace('/\{\{--(.*?)--\}\}/s', "$1", $value); + $value = preg_replace('/\{\{--(.+?)(--\}\})?\n/', "", $value); + + return preg_replace('/\{\{--((.|\s)*?)--\}\}/', "\n", $value); } /** @@ -199,7 +197,7 @@ protected static function compile_comments($value) */ protected static function compile_echos($value) { - return preg_replace('/\{\{(.+?)\}\}/s', '', $value); + return preg_replace('/\{\{(.+?)\}\}/', '', $value); } /** @@ -210,21 +208,27 @@ protected static function compile_echos($value) */ protected static function compile_forelse($value) { - preg_match_all('/@forelse\s*?\(\s*?\$(.+?)\s*?as\s*?\$(.+?)\s*?\)/', $value, $matches, PREG_SET_ORDER ); + preg_match_all('/(\s*)@forelse(\s*\(.*\))(\s*)/', $value, $matches); - if ( count($matches) < 1 ) return $value; - - foreach ($matches as $forelse) + foreach ($matches[0] 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. - $replace = ' 0): foreach ($'.$forelse[1].' as $'.$forelse[2].'): ?>'; + $if = " 0): ?>"; + + $search = '/(\s*)@forelse(\s*\(.*\))/'; + + $replace = '$1'.$if.''; + + $blade = preg_replace($search, $replace, $forelse); // 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[0], $replace, $value); + $value = str_replace($forelse, $blade, $value); } return $value; @@ -238,7 +242,7 @@ protected static function compile_forelse($value) */ protected static function compile_empty($value) { - return str_replace('@empty', '', $value); + return str_replace('@empty', '', $value); } /** @@ -260,42 +264,9 @@ protected static function compile_endforelse($value) */ protected static function compile_structure_openings($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; + $pattern = '/(\s*)@(if|elseif|foreach|for|while)(\s*\(.*\))/'; + + return preg_replace($pattern, '$1', $value); } /** @@ -306,9 +277,9 @@ function($matches) use (&$value) */ protected static function compile_structure_closings($value) { - $pattern = '/@(endif|endforeach|endfor|endwhile|break|continue)/'; + $pattern = '/(\s*)@(endif|endforeach|endfor|endwhile)(\s*)/'; - return preg_replace($pattern, '', $value); + return preg_replace($pattern, '$1$3', $value); } /** @@ -319,7 +290,7 @@ protected static function compile_structure_closings($value) */ protected static function compile_else($value) { - return str_replace( '@else', '', $value); + return preg_replace('/(\s*)@(else)(\s*)/', '$1$3', $value); } /** @@ -330,9 +301,9 @@ protected static function compile_else($value) */ protected static function compile_unless($value) { - $pattern = static::matcher('unless'); + $pattern = '/(\s*)@unless(\s*\(.*\))/'; - return preg_replace($pattern, '', $value); + return preg_replace($pattern, '$1', $value); } /** @@ -356,7 +327,7 @@ protected static function compile_includes($value) { $pattern = static::matcher('include'); - return preg_replace($pattern, 'with(get_defined_vars())->render(); ?>', $value); + return preg_replace($pattern, '$1with(get_defined_vars())->render(); ?>', $value); } /** @@ -369,7 +340,7 @@ protected static function compile_render($value) { $pattern = static::matcher('render'); - return preg_replace($pattern, '', $value); + return preg_replace($pattern, '$1', $value); } /** @@ -382,7 +353,7 @@ protected static function compile_render_each($value) { $pattern = static::matcher('render_each'); - return preg_replace($pattern, '', $value); + return preg_replace($pattern, '$1', $value); } /** @@ -397,18 +368,19 @@ protected static function compile_yields($value) { $pattern = static::matcher('yield'); - return preg_replace($pattern, '', $value); + return preg_replace($pattern, '$1', $value); } /** * Rewrites Blade yield section statements into valid PHP. * - * @param string $value * @return string */ protected static function compile_yield_sections($value) { - return str_replace('@yield_section', '', $value); + $replace = ''; + + return str_replace('@yield_section', $replace, $value); } /** @@ -423,7 +395,7 @@ protected static function compile_section_start($value) { $pattern = static::matcher('section'); - return preg_replace($pattern, '', $value); + return preg_replace($pattern, '$1', $value); } /** @@ -436,7 +408,7 @@ protected static function compile_section_start($value) */ protected static function compile_section_end($value) { - return str_replace('@endsection', '', $value); + return preg_replace('/@endsection/', '', $value); } /** @@ -453,7 +425,7 @@ protected static function compile_extensions($value) } return $value; - } + } /** * Get the regular expression for a generic Blade function. @@ -463,13 +435,13 @@ protected static function compile_extensions($value) */ public static function matcher($function) { - return '/@'.$function.'\s*?(\(.+?\))/'; + return '/(\s*)@'.$function.'(\s*\(.*\))/'; } /** * Get the fully qualified path for a compiled view. * - * @param string $path + * @param string $view * @return string */ public static function compiled($path) @@ -477,4 +449,4 @@ public static function compiled($path) return path('storage').'views/'.md5($path); } -} +} \ No newline at end of file