Skip to content

Commit bebc6c8

Browse files
committed
Fix FileEngine getCompiler() error in viewContainsExpiredFrontMatter
This fix resolves the 'Call to undefined method Illuminate\View\Engines\FileEngine::getCompiler()' error that occurs when Blaze processes views that use FileEngine (plain PHP templates) instead of CompilerEngine (Blade templates). The issue manifests when: - Email notifications are sent (especially in queued jobs) - Plain PHP views are rendered - Any view using FileEngine or PhpEngine is processed The fix checks if the view engine has the getCompiler() method before attempting to call it. For engines without a compiler (FileEngine, PhpEngine), it returns false, indicating no expired frontmatter to check. This approach is correct because: 1. Non-compiler engines don't compile templates, so they can't have expired frontmatter 2. It's safer and more explicit than catching exceptions 3. It properly caches the result to avoid repeated checks Fixes #12
1 parent 412a05c commit bebc6c8

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

src/BlazeManager.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,16 @@ public function viewContainsExpiredFrontMatter($view): bool
6363
return $this->expiredMemo[$path];
6464
}
6565

66-
$compiler = $view->getEngine()->getCompiler();
66+
$engine = $view->getEngine();
67+
68+
// Only CompilerEngine has the getCompiler() method.
69+
// FileEngine and PhpEngine do not have this method, so we should skip checking them.
70+
if (! method_exists($engine, 'getCompiler')) {
71+
$this->expiredMemo[$path] = false;
72+
return false;
73+
}
74+
75+
$compiler = $engine->getCompiler();
6776
$compiled = $compiler->getCompiledPath($path);
6877
$expired = $compiler->isExpired($path);
6978

tests/FileEngineTest.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
use Illuminate\View\Engines\FileEngine;
4+
use Illuminate\View\View;
5+
use Livewire\Blaze\BlazeManager;
6+
7+
describe('FileEngine compatibility', function () {
8+
it('handles views with FileEngine gracefully without throwing errors', function () {
9+
$blazeManager = app(BlazeManager::class);
10+
11+
// Create a mock view that uses FileEngine (which doesn't have getCompiler method)
12+
$fileEngine = new FileEngine(app('files'));
13+
14+
// Create a temporary PHP file (not a blade file)
15+
$tempPath = sys_get_temp_dir() . '/test-view-' . uniqid() . '.php';
16+
file_put_contents($tempPath, '<?php echo "Hello"; ?>');
17+
18+
try {
19+
$view = new View(
20+
app('view'),
21+
$fileEngine,
22+
'test',
23+
$tempPath,
24+
[]
25+
);
26+
27+
// This should not throw an error
28+
$result = $blazeManager->viewContainsExpiredFrontMatter($view);
29+
30+
// For non-compiler engines, it should return false (no expired frontmatter)
31+
expect($result)->toBeFalse();
32+
} finally {
33+
// Clean up
34+
@unlink($tempPath);
35+
}
36+
});
37+
38+
it('caches the result for FileEngine views', function () {
39+
$blazeManager = app(BlazeManager::class);
40+
41+
$fileEngine = new FileEngine(app('files'));
42+
43+
$tempPath = sys_get_temp_dir() . '/test-view-cached-' . uniqid() . '.php';
44+
file_put_contents($tempPath, '<?php echo "Hello"; ?>');
45+
46+
try {
47+
$view = new View(
48+
app('view'),
49+
$fileEngine,
50+
'test',
51+
$tempPath,
52+
[]
53+
);
54+
55+
// First call
56+
$result1 = $blazeManager->viewContainsExpiredFrontMatter($view);
57+
58+
// Second call (should use cached result)
59+
$result2 = $blazeManager->viewContainsExpiredFrontMatter($view);
60+
61+
expect($result1)->toBeFalse();
62+
expect($result2)->toBeFalse();
63+
expect($result1)->toBe($result2);
64+
} finally {
65+
@unlink($tempPath);
66+
}
67+
});
68+
69+
it('still works correctly with CompilerEngine (blade views)', function () {
70+
// This ensures our fix doesn't break the normal blade compilation flow
71+
$blazeManager = app(BlazeManager::class);
72+
73+
// Set up blade compiler
74+
app('blade.compiler')->anonymousComponentPath(__DIR__ . '/fixtures/components');
75+
76+
// Create a blade view
77+
$template = '<x-button>Click me</x-button>';
78+
$compiled = $blazeManager->compile($template);
79+
80+
expect($compiled)->toBeString();
81+
expect($compiled)->toContain('button');
82+
});
83+
});

0 commit comments

Comments
 (0)