diff --git a/src/Illuminate/View/Compilers/BladeCompiler.php b/src/Illuminate/View/Compilers/BladeCompiler.php index 7911462ebf32..a74f9e828f76 100644 --- a/src/Illuminate/View/Compilers/BladeCompiler.php +++ b/src/Illuminate/View/Compilers/BladeCompiler.php @@ -31,6 +31,7 @@ class BladeCompiler extends Compiler implements CompilerInterface Concerns\CompilesLoops, Concerns\CompilesRawPhp, Concerns\CompilesSessions, + Concerns\CompilesSnippets, Concerns\CompilesStacks, Concerns\CompilesStyles, Concerns\CompilesTranslations, diff --git a/src/Illuminate/View/Compilers/Concerns/CompilesSnippets.php b/src/Illuminate/View/Compilers/Concerns/CompilesSnippets.php new file mode 100644 index 000000000000..710fb408727b --- /dev/null +++ b/src/Illuminate/View/Compilers/Concerns/CompilesSnippets.php @@ -0,0 +1,75 @@ +extractSnippetParts($expression); + + return implode("\n", [ + "', + ]); + } + + /** + * Compile the endsnippet statements into valid PHP. + * + * @param string $expression + * @return string + */ + protected function compileEndSnippet($expression) + { + return implode("\n", [ + '', + '', + ]); + } + + /** + * Compile the renderSnippet statements into valid PHP. + * + * @param string $expression + * @return string + */ + protected function compileRenderSnippet($expression) + { + [$function, $args] = $this->extractSnippetParts($expression); + + return ''; + } + + /** + * Analyse the snippet expression and extract the function and optional arguments. + * + * @param string $expression + * @return array + */ + protected function extractSnippetParts($expression) + { + $functionParts = explode(',', $this->stripParentheses($expression)); + + $function = trim(array_shift($functionParts), "'\" "); + + if (empty($function)) { + $function = 'function'; + } + + $function = '__snippet_'.Str::camel($function); + + $args = trim(implode(',', $functionParts)); + + return [$function, $args]; + } +} diff --git a/tests/View/Blade/BladeSnippetStatementsTest.php b/tests/View/Blade/BladeSnippetStatementsTest.php new file mode 100644 index 000000000000..fb42d66e49d6 --- /dev/null +++ b/tests/View/Blade/BladeSnippetStatementsTest.php @@ -0,0 +1,90 @@ + +default breeze + + + + +breeze with double quotes + + + + +breeze with single quotes + + + + + +breeze without quotes + + + + + +breeze with slugged snippet name + + +'; + $this->assertEquals($expected, $this->compiler->compileString($string)); + } + + public function testRenderSnippetStatementsAreCompiled() + { + $string = '@renderSnippet + +@renderSnippet ("bar") + +@renderSnippet (\'foo\', $bar) + +@renderSnippet (foobar, $barfoo) + +@renderSnippet ("foo-bar", $barfoo)'; + $expected = ' + + + + + + + +'; + $this->assertEquals($expected, $this->compiler->compileString($string)); + } +}