Skip to content
Merged
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
39 changes: 23 additions & 16 deletions src/BladeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Illuminate\View\Compilers\ComponentTagCompiler;
use ReflectionClass;
use Livewire\Blaze\Support\LaravelRegex;
use Livewire\Blaze\Support\Utils;
use ReflectionClass;

class BladeService
{
Expand Down Expand Up @@ -222,20 +223,10 @@ public static function earliestPreCompilationHook(callable $callback): void
*/
public static function preStoreUncompiledBlocks(string $input): string
{
$compiler = app('blade.compiler');
$reflection = new \ReflectionClass($compiler);

$storeRawBlock = $reflection->getMethod('storeRawBlock');

$output = $input;

$output = preg_replace_callback('/(?<!@)@verbatim(\s*)(.*?)@endverbatim/s', function ($matches) use ($storeRawBlock, $compiler) {
return $matches[1].$storeRawBlock->invoke($compiler, "@verbatim{$matches[2]}@endverbatim");
}, $output);

$output = preg_replace_callback('/(?<!@)@php(.*?)@endphp/s', function ($matches) use ($storeRawBlock, $compiler) {
return $storeRawBlock->invoke($compiler, "@php{$matches[1]}@endphp");
}, $output);
$output = static::storeVerbatimBlocks($output);
$output = static::storePhpBlocks($output);

return $output;
}
Expand All @@ -245,12 +236,28 @@ public static function preStoreUncompiledBlocks(string $input): string
*/
public static function storeVerbatimBlocks(string $input): string
{
$compiler = app('blade.compiler');
return static::storeRawBlock(LaravelRegex::VERBATIM_BLOCK, $input);
}

/**
* Store only @verbatim blocks as raw block placeholders.
*/
public static function storePhpBlocks(string $input): string
{
return static::storeRawBlock(LaravelRegex::PHP_BLOCK, $input);
}

/**
* Store a raw block placeholder via the Blade compiler.
*/
protected static function storeRawBlock(string $pattern, string $content): string
{
$compiler = app('blade.compiler');
$reflection = new \ReflectionClass($compiler);
$method = $reflection->getMethod('storeVerbatimBlocks');

return $method->invoke($compiler, $input);
return preg_replace_callback($pattern, function ($matches) use ($compiler, $reflection) {
return $reflection->getMethod('storeRawBlock')->invoke($compiler, $matches[0]);
}, $content);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/Support/LaravelRegex.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,18 @@ class LaravelRegex
)
)?
/x';

/**
* Pattern for matching @verbatim...@endverbatim blocks.
*
* @see BladeCompiler::storeVerbatimBlocks() — /(?<!@)@verbatim(\s*)(.*?)@endverbatim/s
*/
const VERBATIM_BLOCK = '/(?<!@)@verbatim(\s*)(.*?)@endverbatim/s';

/**
* Pattern for matching @php...@endphp blocks.
*
* @see BladeCompiler::storePhpBlocks() — /(?<!@)@php(.*?)@endphp/s
*/
const PHP_BLOCK = '/(?<!@)@php(.*?)@endphp/s';
}
8 changes: 7 additions & 1 deletion tests/Compiler/WrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,13 @@
]);

test('preserves php directives', function () {
$input = '@php something @endphp';
$input = '@php /* uncompiled */ @endphp';

expect(app(Wrapper::class)->wrap($input, ''))->toContain($input);
});

test('preserves verbatim directives', function () {
$input = '@verbatim /* uncompiled */ @endverbatim';

expect(app(Wrapper::class)->wrap($input, ''))->toContain($input);
});