Theme modules: Updated view includes to prevent caching conflicts

This commit is contained in:
Dan Brown
2026-02-08 13:39:34 +00:00
parent a20438b901
commit 984a73159f
3 changed files with 23 additions and 16 deletions

View File

@@ -27,6 +27,16 @@ class ThemeServiceProvider extends ServiceProvider
// Boot up the theme system
$themeService = $this->app->make(ThemeService::class);
$viewFactory = $this->app->make('view');
$themeViews = new ThemeViews($viewFactory->getFinder());
// Use a custom include so that we can insert theme views before/after includes.
// This is done, even if no theme is active, so that view caching does not create problems
// when switching between themes or when switching a theme on/off.
$viewFactory->share('__themeViews', $themeViews);
Blade::directive('include', function ($expression) {
return "<?php echo \$__themeViews->handleViewInclude({$expression}, array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1])); ?>";
});
if (!$themeService->getTheme()) {
return;
}
@@ -35,14 +45,7 @@ class ThemeServiceProvider extends ServiceProvider
$themeService->readThemeActions();
$themeService->dispatch(ThemeEvents::APP_BOOT, $this->app);
$themeViews = new ThemeViews($viewFactory->getFinder());
$themeViews->registerViewPathsForTheme($themeService->getModules());
$themeService->dispatch(ThemeEvents::THEME_REGISTER_VIEWS, $themeViews);
if ($themeViews->hasRegisteredViews()) {
$viewFactory->share('__themeViews', $themeViews);
Blade::directive('include', function ($expression) {
return "<?php echo \$__themeViews->handleViewInclude({$expression}, array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1])); ?>";
});
}
}
}

View File

@@ -42,16 +42,20 @@ class ThemeViews
/**
* Provide the response for a blade template view include.
*/
public function handleViewInclude(string $viewPath, array $data = []): string
public function handleViewInclude(string $viewPath, array $data = [], array $mergeData = []): string
{
if (!$this->hasRegisteredViews()) {
return view()->make($viewPath, $data)->render();
return view()->make($viewPath, $data, $mergeData)->render();
}
if (str_contains('book-tree', $viewPath)) {
dd($viewPath, $data);
}
$viewsContent = [
...$this->renderViewSets($this->beforeViews[$viewPath] ?? [], $data),
view()->make($viewPath, $data)->render(),
...$this->renderViewSets($this->afterViews[$viewPath] ?? [], $data),
...$this->renderViewSets($this->beforeViews[$viewPath] ?? [], $data, $mergeData),
view()->make($viewPath, $data, $mergeData)->render(),
...$this->renderViewSets($this->afterViews[$viewPath] ?? [], $data, $mergeData),
];
return implode("\n", $viewsContent);
@@ -97,15 +101,15 @@ class ThemeViews
* @param array<string, int> $viewSet
* @return string[]
*/
protected function renderViewSets(array $viewSet, array $data): array
protected function renderViewSets(array $viewSet, array $data, array $mergeData): array
{
$paths = array_keys($viewSet);
usort($paths, function (string $a, string $b) use ($viewSet) {
return $viewSet[$a] <=> $viewSet[$b];
});
return array_map(function (string $viewPath) use ($data) {
return view()->file($viewPath, $data)->render();
return array_map(function (string $viewPath) use ($data, $mergeData) {
return view()->file($viewPath, $data, $mergeData)->render();
}, $paths);
}
}