Skip to content

[11.x] Added "snippet" blade feature with >13x speed improvement #51268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class BladeCompiler extends Compiler implements CompilerInterface
Concerns\CompilesLoops,
Concerns\CompilesRawPhp,
Concerns\CompilesSessions,
Concerns\CompilesSnippets,
Concerns\CompilesStacks,
Concerns\CompilesStyles,
Concerns\CompilesTranslations,
Expand Down
75 changes: 75 additions & 0 deletions src/Illuminate/View/Compilers/Concerns/CompilesSnippets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Illuminate\View\Compilers\Concerns;

use Illuminate\Support\Str;

trait CompilesSnippets
{
/**
* Compile the snippet statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileSnippet($expression)
{
[$function, $args] = $this->extractSnippetParts($expression);

return implode("\n", [
"<?php if (! isset(\${$function})):",
'$'.$function.' = static function('.$args.') use($__env) {',
'?>',
]);
}

/**
* Compile the endsnippet statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEndSnippet($expression)
{
return implode("\n", [
'<?php } ?>',
'<?php endif; ?>',
]);
}

/**
* Compile the renderSnippet statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileRenderSnippet($expression)
{
[$function, $args] = $this->extractSnippetParts($expression);

return '<?php echo $'.$function.'('.$args.'); ?>';
}

/**
* 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];
}
}
90 changes: 90 additions & 0 deletions tests/View/Blade/BladeSnippetStatementsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Illuminate\Tests\View\Blade;

class BladeSnippetStatementsTest extends AbstractBladeTestCase
{
public function testSnippetStatementsAreCompiled()
{
$string = '@snippet
default breeze
@endsnippet

@snippet ("bar")
breeze with double quotes
@endsnippet

@snippet (\'foo\', $bar)
breeze with single quotes {{ $bar }}
@endsnippet

@snippet (foobar, string $barfoo)
breeze without quotes {{ $barfoo }}
@endsnippet

@snippet ("foo-bar", ?string $barfoo = null)
breeze with slugged snippet name {{ $barfoo }}
@endsnippet';
$expected = '<?php if (! isset($__snippet_function)):
$__snippet_function = static function() use($__env) {
?>
default breeze
<?php } ?>
<?php endif; ?>

<?php if (! isset($__snippet_bar)):
$__snippet_bar = static function() use($__env) {
?>
breeze with double quotes
<?php } ?>
<?php endif; ?>

<?php if (! isset($__snippet_foo)):
$__snippet_foo = static function($bar) use($__env) {
?>
breeze with single quotes <?php echo e($bar); ?>

<?php } ?>
<?php endif; ?>

<?php if (! isset($__snippet_foobar)):
$__snippet_foobar = static function(string $barfoo) use($__env) {
?>
breeze without quotes <?php echo e($barfoo); ?>

<?php } ?>
<?php endif; ?>

<?php if (! isset($__snippet_fooBar)):
$__snippet_fooBar = static function(?string $barfoo = null) use($__env) {
?>
breeze with slugged snippet name <?php echo e($barfoo); ?>

<?php } ?>
<?php endif; ?>';
$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 = '<?php echo $__snippet_function(); ?>

<?php echo $__snippet_bar(); ?>

<?php echo $__snippet_foo($bar); ?>

<?php echo $__snippet_foobar($barfoo); ?>

<?php echo $__snippet_fooBar($barfoo); ?>';
$this->assertEquals($expected, $this->compiler->compileString($string));
}
}