Skip to content

Commit 984a731

Browse files
committed
Theme modules: Updated view includes to prevent caching conflicts
1 parent a20438b commit 984a731

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

app/App/Providers/ThemeServiceProvider.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ public function boot(): void
2727
// Boot up the theme system
2828
$themeService = $this->app->make(ThemeService::class);
2929
$viewFactory = $this->app->make('view');
30+
$themeViews = new ThemeViews($viewFactory->getFinder());
31+
32+
// Use a custom include so that we can insert theme views before/after includes.
33+
// This is done, even if no theme is active, so that view caching does not create problems
34+
// when switching between themes or when switching a theme on/off.
35+
$viewFactory->share('__themeViews', $themeViews);
36+
Blade::directive('include', function ($expression) {
37+
return "<?php echo \$__themeViews->handleViewInclude({$expression}, array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1])); ?>";
38+
});
39+
3040
if (!$themeService->getTheme()) {
3141
return;
3242
}
@@ -35,14 +45,7 @@ public function boot(): void
3545
$themeService->readThemeActions();
3646
$themeService->dispatch(ThemeEvents::APP_BOOT, $this->app);
3747

38-
$themeViews = new ThemeViews($viewFactory->getFinder());
3948
$themeViews->registerViewPathsForTheme($themeService->getModules());
4049
$themeService->dispatch(ThemeEvents::THEME_REGISTER_VIEWS, $themeViews);
41-
if ($themeViews->hasRegisteredViews()) {
42-
$viewFactory->share('__themeViews', $themeViews);
43-
Blade::directive('include', function ($expression) {
44-
return "<?php echo \$__themeViews->handleViewInclude({$expression}, array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1])); ?>";
45-
});
46-
}
4750
}
4851
}

app/Theming/ThemeViews.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,20 @@ public function registerViewPathsForTheme(array $modules): void
4242
/**
4343
* Provide the response for a blade template view include.
4444
*/
45-
public function handleViewInclude(string $viewPath, array $data = []): string
45+
public function handleViewInclude(string $viewPath, array $data = [], array $mergeData = []): string
4646
{
4747
if (!$this->hasRegisteredViews()) {
48-
return view()->make($viewPath, $data)->render();
48+
return view()->make($viewPath, $data, $mergeData)->render();
49+
}
50+
51+
if (str_contains('book-tree', $viewPath)) {
52+
dd($viewPath, $data);
4953
}
5054

5155
$viewsContent = [
52-
...$this->renderViewSets($this->beforeViews[$viewPath] ?? [], $data),
53-
view()->make($viewPath, $data)->render(),
54-
...$this->renderViewSets($this->afterViews[$viewPath] ?? [], $data),
56+
...$this->renderViewSets($this->beforeViews[$viewPath] ?? [], $data, $mergeData),
57+
view()->make($viewPath, $data, $mergeData)->render(),
58+
...$this->renderViewSets($this->afterViews[$viewPath] ?? [], $data, $mergeData),
5559
];
5660

5761
return implode("\n", $viewsContent);
@@ -97,15 +101,15 @@ protected function registerAdjacentView(array &$location, string $targetView, st
97101
* @param array<string, int> $viewSet
98102
* @return string[]
99103
*/
100-
protected function renderViewSets(array $viewSet, array $data): array
104+
protected function renderViewSets(array $viewSet, array $data, array $mergeData): array
101105
{
102106
$paths = array_keys($viewSet);
103107
usort($paths, function (string $a, string $b) use ($viewSet) {
104108
return $viewSet[$a] <=> $viewSet[$b];
105109
});
106110

107-
return array_map(function (string $viewPath) use ($data) {
108-
return view()->file($viewPath, $data)->render();
111+
return array_map(function (string $viewPath) use ($data, $mergeData) {
112+
return view()->file($viewPath, $data, $mergeData)->render();
109113
}, $paths);
110114
}
111115
}

resources/views/pages/show.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
class="page-content clearfix">
2323
@include('pages.parts.page-display')
2424
</div>
25-
@include('pages.parts.pointer', ['page' => $page])
25+
@include('pages.parts.pointer', ['page' => $page, 'commentTree' => $commentTree])
2626
</main>
2727

2828
@include('entities.sibling-navigation', ['next' => $next, 'previous' => $previous])

0 commit comments

Comments
 (0)