diff --git a/src/BlazeServiceProvider.php b/src/BlazeServiceProvider.php index 3af4afe5..35036f98 100644 --- a/src/BlazeServiceProvider.php +++ b/src/BlazeServiceProvider.php @@ -7,7 +7,6 @@ use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Blade; use Illuminate\Support\Facades\View; -use Illuminate\View\Engines\CompilerEngine; class BlazeServiceProvider extends ServiceProvider { @@ -58,8 +57,7 @@ protected function registerBlazeRuntime(): void return; } - // Avoid injecting the BlazeRuntime into non-Blade views (like Statamic's Antlers) - if ($view->getEngine() instanceof CompilerEngine) { + if (str_ends_with($view->getPath(), '.blade.php')) { $view->with('__blaze', $this->app->make(BlazeRuntime::class)); } }); diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index a9ae7aa9..a061a619 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -1,30 +1,29 @@ render(); +}); + +test('renders components', function () { + view('mix')->render(); })->throwsNoExceptions(); test('renders components with blaze off', function () { - Artisan::call('view:clear'); - Blaze::disable(); - view('inputs')->render(); + view('mix')->render(); })->throwsNoExceptions(); test('renders components with blaze off and debug mode on', function () { - Artisan::call('view:clear'); - Blaze::disable(); Blaze::debug(); - view('inputs')->render(); + view('mix')->render(); })->throwsNoExceptions(); test('ignores verbatim blocks', function () { @@ -46,7 +45,37 @@ }); test('supports php engine', function () { - // Make sure our hooks do not break views - // rendered using the regular php engine. view('php-view')->render(); -})->throwsNoExceptions(); \ No newline at end of file +})->throwsNoExceptions(); + +test('supports decorated engine', function () { + $resolver = app('view.engine.resolver'); + $blade = $resolver->resolve('blade'); + + // This replicates how Sentry wraps the blade engine... + $resolver->register('blade', function () use ($blade) { + return new class($blade) implements Engine { + public function __construct(private Engine $engine) {} + + public function get($path, array $data = []): string { + return $this->engine->get($path, $data); + } + }; + }); + + view('mix')->render(); +})->throwsNoExceptions(); + +test('does not inject __blaze into non-blade engine views', function () { + // Statamic serializes all view data, we need to make sure + // we don't inject BlazeRuntime which is not serializable. + app('view')->addExtension('antlers.html', 'antlers', function () { + return new class implements Engine { + public function get($path, array $data = []): string { + return isset($data['__blaze']) ? 'BLAZE' : 'NO_BLAZE'; + } + }; + }); + + expect(view('antlers-view')->render())->toBe('NO_BLAZE'); +}); \ No newline at end of file diff --git a/tests/fixtures/views/antlers-view.antlers.html b/tests/fixtures/views/antlers-view.antlers.html new file mode 100644 index 00000000..6d5a89f8 --- /dev/null +++ b/tests/fixtures/views/antlers-view.antlers.html @@ -0,0 +1 @@ +{{ title }} diff --git a/tests/fixtures/views/inputs.blade.php b/tests/fixtures/views/inputs.blade.php deleted file mode 100644 index 0a87d0ee..00000000 --- a/tests/fixtures/views/inputs.blade.php +++ /dev/null @@ -1,2 +0,0 @@ - - \ No newline at end of file diff --git a/tests/fixtures/views/mix.blade.php b/tests/fixtures/views/mix.blade.php new file mode 100644 index 00000000..35084dae --- /dev/null +++ b/tests/fixtures/views/mix.blade.php @@ -0,0 +1,4 @@ + + + + \ No newline at end of file