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
58 changes: 54 additions & 4 deletions src/BladeRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

namespace Livewire\Blaze;

use Closure;
use Illuminate\Contracts\View\Factory;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\File;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Component;
use Illuminate\View\ComponentSlot;
use Livewire\Blaze\Exceptions\InvalidBlazeFoldUsageException;
use Livewire\Blaze\Parser\Attribute;
use Livewire\Blaze\Parser\Nodes\ComponentNode;
use Livewire\Blaze\Parser\Nodes\SlotNode;
use Livewire\Blaze\Runtime\BlazeRuntime;
use Livewire\Blaze\Support\ComponentSource;
use Livewire\Blaze\Support\Utils;
use ReflectionClass;
use ReflectionFunction;

use function Livewire\invade;

/**
* Handles isolated Blade rendering used during compile-time folding.
Expand Down Expand Up @@ -79,6 +82,16 @@ function ($input) {
return $input;
},
],
'precompilers' => fn (array $precompilers) => [
...$this->withoutLivewirePrecompilers($precompilers),
function (string $input) use ($path) {
if (preg_match('~<\s*livewire[-:]|(?<![@\w])@livewire\b(?=\s*\()~', $input)) {
throw InvalidBlazeFoldUsageException::forLivewire($path);
}

return $input;
}
],
'path' => null,
'forElseCounter' => 0,
'firstCaseInSwitch' => true,
Expand Down Expand Up @@ -173,10 +186,12 @@ protected function freezeObjectProperties(object|string $object, array $properti

$property = $reflection->getProperty($name);

$frozen[$name] = $property->getValue(is_object($object) ? $object : null);
$currentValue = $property->getValue(is_object($object) ? $object : null);

$frozen[$name] = $currentValue;

if (! is_numeric($key)) {
$property->setValue($object, $value);
$property->setValue($object, $value instanceof Closure ? $value($currentValue) : $value);
}
}

Expand All @@ -187,4 +202,39 @@ protected function freezeObjectProperties(object|string $object, array $properti
}
};
}

/**
* Filter out Livewire-specific precompilers.
*/
protected function withoutLivewirePrecompilers(array $precompilers): array
{
if (! class_exists(\Livewire\Livewire::class)) {
return $precompilers;
}

$livewireOnlyPrecompilers = class_exists(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::class)
? invade(app(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::class))->precompilers
: [];

return Arr::where($precompilers, function ($precompiler) use ($livewireOnlyPrecompilers) {
if ($precompiler instanceof \Livewire\Mechanisms\CompileLivewireTags\LivewireTagPrecompiler) {
return false;
}

if (in_array($precompiler, $livewireOnlyPrecompilers, true)) {
return false;
}

if ($precompiler instanceof Closure && in_array((new ReflectionFunction($precompiler))->getClosureScopeClass()?->getName(), [
\Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::class,
\Livewire\Features\SupportMorphAwareBladeCompilation\SupportMorphAwareBladeCompilation::class,
\Livewire\Mechanisms\ExtendBlade\ExtendBlade::class,
\Livewire\LivewireServiceProvider::class,
], true)) {
return false;
}

return true;
});
}
}
8 changes: 8 additions & 0 deletions src/Exceptions/InvalidBlazeFoldUsageException.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,12 @@ public static function forOnce(string $componentPath): self
);
}

public static function forLivewire(string $componentPath): self
{
return new self(
$componentPath,
'<livewire:|@livewire',
'Components with livewire components should not use @blaze fold as Livewire components maintain runtime state'
);
}
}
2 changes: 2 additions & 0 deletions src/Folder/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ protected function checkProblematicPatterns(ComponentSource $source): void
'auth\\(\\)' => 'forAuth',
'request\\(\\)' => 'forRequest',
'old\\(' => 'forOld',
'<livewire:' => 'forLivewire',
'@livewire' => 'forLivewire',
];

foreach ($problematicPatterns as $pattern => $factoryMethod) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Folder/FolderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@

expect(fn () => app(Folder::class)->fold($node))
->toThrow(InvalidBlazeFoldUsageException::class);
})->with(['errors', 'session', 'error', 'csrf', 'auth', 'request', 'old', 'once']);
})->with(['errors', 'session', 'error', 'csrf', 'auth', 'request', 'old', 'once', 'livewire', 'livewire-directive', 'livewire-wrapped']);

test('does not fold components with slots wrapped in directives', function () {
$input = '<x-foldable.card>@if(false)<x-slot:header>Header</x-slot:header>@endif</x-foldable.card>';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@blaze(fold: true)

@livewire('counter')
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@blaze(fold: true)

<x-invalid-foldable.livewire />
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@blaze(fold: true)

<livewire:counter />
Loading