Skip to content

Commit e17d239

Browse files
Added "snippet" blade feature
1 parent a57dca1 commit e17d239

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed

src/Illuminate/View/Compilers/BladeCompiler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class BladeCompiler extends Compiler implements CompilerInterface
3131
Concerns\CompilesLoops,
3232
Concerns\CompilesRawPhp,
3333
Concerns\CompilesSessions,
34+
Concerns\CompilesSnippets,
3435
Concerns\CompilesStacks,
3536
Concerns\CompilesStyles,
3637
Concerns\CompilesTranslations,
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Illuminate\View\Compilers\Concerns;
4+
5+
use Illuminate\Support\Str;
6+
7+
trait CompilesSnippets
8+
{
9+
/**
10+
* Compile the snippet statements into valid PHP.
11+
*
12+
* @param string $expression
13+
* @return string
14+
*/
15+
protected function compileSnippet($expression)
16+
{
17+
[$function, $args] = $this->extractSnippetParts($expression);
18+
19+
return implode("\n", [
20+
"<?php if (! isset(\${$function})):",
21+
'$'.$function.' = static function('.$args.') use($__env) {',
22+
'?>',
23+
]);
24+
}
25+
26+
/**
27+
* Compile the endsnippet statements into valid PHP.
28+
*
29+
* @param string $expression
30+
* @return string
31+
*/
32+
protected function compileEndSnippet($expression)
33+
{
34+
return implode("\n", [
35+
'<?php } ?>',
36+
'<?php endif; ?>',
37+
]);
38+
}
39+
40+
/**
41+
* Compile the renderSnippet statements into valid PHP.
42+
*
43+
* @param string $expression
44+
* @return string
45+
*/
46+
protected function compileRenderSnippet($expression)
47+
{
48+
[$function, $args] = $this->extractSnippetParts($expression);
49+
50+
return '<?php echo $'.$function.'('.$args.'); ?>';
51+
}
52+
53+
/**
54+
* Analyse the snippet expression and extract the function and optional arguments.
55+
*
56+
* @param string $expression
57+
* @return array
58+
*/
59+
protected function extractSnippetParts($expression)
60+
{
61+
$functionParts = explode(',', $this->stripParentheses($expression));
62+
63+
$function = trim(array_shift($functionParts), "'\" ");
64+
65+
if (empty($function)) {
66+
$function = 'function';
67+
}
68+
69+
$function = '__snippet_'.Str::camel($function);
70+
71+
$args = trim(implode(',', $functionParts));
72+
73+
return [$function, $args];
74+
}
75+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\View\Blade;
4+
5+
class BladeSnippetStatementsTest extends AbstractBladeTestCase
6+
{
7+
public function testSnippetStatementsAreCompiled()
8+
{
9+
$string = '@snippet
10+
default breeze
11+
@endsnippet
12+
13+
@snippet ("bar")
14+
breeze with double quotes
15+
@endsnippet
16+
17+
@snippet (\'foo\', $bar)
18+
breeze with single quotes {{ $bar }}
19+
@endsnippet
20+
21+
@snippet (foobar, string $barfoo)
22+
breeze without quotes {{ $barfoo }}
23+
@endsnippet
24+
25+
@snippet ("foo-bar", ?string $barfoo = null)
26+
breeze with slugged snippet name {{ $barfoo }}
27+
@endsnippet';
28+
$expected = '<?php if (! isset($__snippet_function)):
29+
$__snippet_function = static function() use($__env) {
30+
?>
31+
default breeze
32+
<?php } ?>
33+
<?php endif; ?>
34+
35+
<?php if (! isset($__snippet_bar)):
36+
$__snippet_bar = static function() use($__env) {
37+
?>
38+
breeze with double quotes
39+
<?php } ?>
40+
<?php endif; ?>
41+
42+
<?php if (! isset($__snippet_foo)):
43+
$__snippet_foo = static function($bar) use($__env) {
44+
?>
45+
breeze with single quotes <?php echo e($bar); ?>
46+
47+
<?php } ?>
48+
<?php endif; ?>
49+
50+
<?php if (! isset($__snippet_foobar)):
51+
$__snippet_foobar = static function(string $barfoo) use($__env) {
52+
?>
53+
breeze without quotes <?php echo e($barfoo); ?>
54+
55+
<?php } ?>
56+
<?php endif; ?>
57+
58+
<?php if (! isset($__snippet_fooBar)):
59+
$__snippet_fooBar = static function(?string $barfoo = null) use($__env) {
60+
?>
61+
breeze with slugged snippet name <?php echo e($barfoo); ?>
62+
63+
<?php } ?>
64+
<?php endif; ?>';
65+
$this->assertEquals($expected, $this->compiler->compileString($string));
66+
}
67+
68+
public function testRenderSnippetStatementsAreCompiled()
69+
{
70+
$string = '@renderSnippet
71+
72+
@renderSnippet ("bar")
73+
74+
@renderSnippet (\'foo\', $bar)
75+
76+
@renderSnippet (foobar, $barfoo)
77+
78+
@renderSnippet ("foo-bar", $barfoo)';
79+
$expected = '<?php echo $__snippet_function(); ?>
80+
81+
<?php echo $__snippet_bar(); ?>
82+
83+
<?php echo $__snippet_foo($bar); ?>
84+
85+
<?php echo $__snippet_foobar($barfoo); ?>
86+
87+
<?php echo $__snippet_fooBar($barfoo); ?>';
88+
$this->assertEquals($expected, $this->compiler->compileString($string));
89+
}
90+
}

0 commit comments

Comments
 (0)