-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayoutLoader.php
More file actions
422 lines (345 loc) · 15.1 KB
/
Copy pathLayoutLoader.php
File metadata and controls
422 lines (345 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
<?php
declare(strict_types=1);
namespace Capell\LayoutBuilder\Support\Loader;
use Capell\Core\Actions\GetComponentClassAction;
use Capell\Core\Contracts\Pageable;
use Capell\Core\Enums\MediaCollectionEnum;
use Capell\Core\Facades\CapellCore;
use Capell\Core\Models\Language;
use Capell\Core\Models\Layout;
use Capell\Core\Models\Page;
use Capell\LayoutBuilder\Models\Widget;
use Capell\LayoutBuilder\Models\WidgetAsset;
use Capell\LayoutBuilder\Support\LayoutWidgetData;
use Illuminate\Contracts\Database\Eloquent\Builder as BuilderContract;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Schema;
use Throwable;
class LayoutLoader
{
private const string RETRIEVED_MODEL_STORE_SERVICE = 'capell.frontend.retrieved-model-store';
/**
* Preloaded widgets per [layoutId][languageId][pageIdOr0] => [containerKey][widgetKey][occurrence] => Widget
* Used to avoid N+1 queries when resolving multiple widgets for a layout.
*
* @var array<string, array<string, array<string, array<int, Widget>>>>
*/
private array $preloaded = [];
public function getLayout(int $id): ?Layout
{
$key = 'layout-' . $id;
$fromCache = true;
$layout = CapellCore::rememberCache($key, function () use ($id, &$fromCache): ?Layout {
$fromCache = false;
return Layout::query()->find($id);
});
if ($fromCache && $layout instanceof Layout) {
$this->trackRetrievedModel($layout);
$this->layoutWidgets($layout)->each(function (Widget $widget): void {
$this->trackRetrievedModel($widget);
});
}
return $layout;
}
/**
* @param array<int, string>|null $containerKeys
*/
public function preloadLayoutWidgets(Layout $layout, Language $language, ?Pageable $page, ?array $containerKeys = null): void
{
$cacheKey = $this->preloadedKey($layout, $language, $page, $containerKeys);
if (isset($this->preloaded[$cacheKey])) {
return;
}
$containers = $this->selectedLayoutContainers($layout, $containerKeys);
$selectedWidgetKeys = $this->selectedWidgetKeys($containers);
$selectedContainerOccurrences = $this->selectedContainerOccurrences($containers);
if (! $layout->relationLoaded('media')) {
$layout->load(['media' => fn (BuilderContract $query): BuilderContract => $query->ordered()]);
}
if ($selectedWidgetKeys === [] || ! Schema::hasTable('widgets')) {
$layout->setRelation('layoutWidgets', collect());
$this->preloaded[$cacheKey] = [];
return;
}
$layout->setRelation('layoutWidgets', Widget::query()
->whereIn('key', $selectedWidgetKeys)
->whereHas('blueprint', fn (BuilderContract $query): BuilderContract => $query->enabled()->accessible())
->with([
'blueprint',
'media' => fn (BuilderContract $query): BuilderContract => $query->ordered(),
'translation' => fn (BuilderContract $query): BuilderContract => $query->where('language_id', $language->id),
])
->enabled()
->publishedDate()
->get());
$this->layoutWidgets($layout)->each(function (Widget $widget): void {
$this->trackRetrievedModel($widget);
$widget->setRelation('image', $widget->media->firstWhere('type', MediaCollectionEnum::Image->value));
$widget->setRelation('backgroundImage', $widget->media->firstWhere('type', MediaCollectionEnum::BackgroundImage->value));
});
$layoutWidgets = $this->layoutWidgets($layout)->whereIn('key', $selectedWidgetKeys)->values();
// Attach language relation to the loaded translation for consistency
$layoutWidgets->each(function (Widget $widget) use ($language): void {
$widget->translation?->setRelation('language', $language);
});
$this->hydrateLayoutWidgets($layoutWidgets);
// Build a lookup for widgets by id and by key
$widgetsById = [];
$widgetsByKey = [];
foreach ($layoutWidgets as $widget) {
$widgetsById[$widget->id] = $widget;
$widgetsByKey[$widget->key] = $widget;
}
// Compute morph eager loads, including component-specific additions across all widgets
$with = [
Page::class => Page::getMorphRelations($language),
];
CapellCore::getAssets()->each(function (mixed $asset) use (&$with, $language): void {
$model = $asset->model;
if ($model === Page::class || ! method_exists($model, 'getMorphRelations')) {
return;
}
$with[$model] = $model::getMorphRelations($language);
});
foreach ($layoutWidgets as $widget) {
$component = $widget->getComponent();
$componentType = $widget->getMetaComponentType();
$livewire = $componentType === 'livewire';
if (! is_string($component)) {
continue;
}
try {
$componentClass = GetComponentClassAction::run($component, $livewire);
} catch (Throwable) {
continue;
}
if (method_exists($componentClass, 'loadWidgetAssets')) {
$componentClass::loadWidgetAssets($with, $language);
}
}
// Fetch assets for all widgets in one go (page-specific + defaults), eager loading morph relations
$widgetIds = array_keys($widgetsById);
$assetQuery = WidgetAsset::query()
->whereIn('widget_id', $widgetIds)
->whereHas('asset')
->with([
'media',
'asset' => function (Relation $morphTo) use ($with): void {
if ($morphTo instanceof MorphTo) {
$morphTo->morphWith($with);
}
},
])
->ordered()
->alphabetical($language);
if ($page instanceof Pageable) {
$assetQuery->where(function (BuilderContract $query) use ($page, $selectedContainerOccurrences): void {
$query->where([
'pageable_type' => $page->getMorphClass(),
'pageable_id' => $page->getKey(),
])
->when($selectedContainerOccurrences !== [], function (BuilderContract $query) use ($selectedContainerOccurrences): BuilderContract {
return $query->where(function (BuilderContract $query) use ($selectedContainerOccurrences): void {
foreach ($selectedContainerOccurrences as $position) {
$query->orWhere(function (BuilderContract $query) use ($position): void {
$query
->where('container', $position['container'])
->where('occurrence', $position['occurrence']);
});
}
});
})
->orWhereNull(['pageable_type', 'pageable_id']);
});
} else {
$assetQuery->whereNull(['pageable_type', 'pageable_id']);
}
$assets = $assetQuery->get();
// Group assets for fast lookups
$defaultAssetsByWidgetIdOccurrence = [];
$pageAssetsByWidgetIdContainerOcc = [];
$assets->each(function (WidgetAsset $asset) use (&$defaultAssetsByWidgetIdOccurrence, &$pageAssetsByWidgetIdContainerOcc): void {
$this->trackRetrievedModel($asset);
$widgetId = (int) $asset->widget_id;
$occurrence = $asset->occurrence ?? 1;
if ($asset->pageable_id === null && $asset->pageable_type === null) {
$defaultAssetsByWidgetIdOccurrence[$widgetId][$occurrence] ??= [];
$defaultAssetsByWidgetIdOccurrence[$widgetId][$occurrence][] = $asset;
return;
}
$container = $asset->container;
$pageAssetsByWidgetIdContainerOcc[$widgetId][$container][$occurrence] ??= [];
$pageAssetsByWidgetIdContainerOcc[$widgetId][$container][$occurrence][] = $asset;
});
// Build the final preloaded map per container/widget/occurrence
$result = [];
foreach ($containers as $containerKey => $container) {
foreach (LayoutWidgetData::fromContainer($container) as $widgetData) {
$widgetKey = LayoutWidgetData::key($widgetData);
if ($widgetKey === null) {
continue;
}
$occurrence = LayoutWidgetData::occurrence($widgetData);
$baseWidget = $widgetsByKey[$widgetKey] ?? null;
if (! $baseWidget instanceof Widget) {
continue;
}
$clone = clone $baseWidget;
$clone->translation?->setRelation('language', $language);
$wid = $baseWidget->id;
$assetsForPosition = $pageAssetsByWidgetIdContainerOcc[$wid][$containerKey][$occurrence] ?? [];
if ($assetsForPosition === []) {
$assetsForPosition = $defaultAssetsByWidgetIdOccurrence[$wid][$occurrence] ?? [];
}
$clone->setRelation('assets', collect($assetsForPosition));
$result[$containerKey][$widgetKey][$occurrence] = $clone;
}
}
$this->preloaded[$cacheKey] = $result;
}
/**
* @param array<array-key, mixed> $containerKeys
*/
public function getLayoutWidget(
Layout $layout,
string $widgetKey,
Language $language,
?Pageable $page,
string $containerKey,
int $occurrence,
?array $containerKeys = null,
): ?Widget {
$this->preloadLayoutWidgets($layout, $language, $page, $containerKeys);
return $this->loadWidget($layout, $language, $page, $containerKey, $widgetKey, $occurrence, $containerKeys);
}
/**
* @param Collection<int, Widget> $widgets
*/
private function hydrateLayoutWidgets(Collection $widgets): void
{
$widgets
->groupBy(fn (Widget $widget): string => $widget->getComponent() . ':' . $widget->getMetaComponentType())
->each(function (Collection $componentWidgets): void {
/** @var Widget|null $firstWidget */
$firstWidget = $componentWidgets->first();
if (! $firstWidget instanceof Widget) {
return;
}
$component = $firstWidget->getComponent();
$livewire = $firstWidget->getMetaComponentType() === 'livewire';
if (! is_string($component)) {
return;
}
try {
$componentClass = GetComponentClassAction::run($component, $livewire);
} catch (Throwable) {
return;
}
if (! method_exists($componentClass, 'hydrateWidgets')) {
return;
}
$componentClass::hydrateWidgets($componentWidgets);
});
}
/**
* @return Collection<int, Widget>
*/
private function layoutWidgets(Layout $layout): Collection
{
$widgets = $layout->getRelationValue('layoutWidgets');
return $widgets instanceof Collection ? $widgets : collect();
}
private function trackRetrievedModel(object $model): void
{
if (! $model instanceof Model) {
return;
}
if (! app()->bound(self::RETRIEVED_MODEL_STORE_SERVICE)) {
return;
}
$store = resolve(self::RETRIEVED_MODEL_STORE_SERVICE);
if (is_object($store) && method_exists($store, 'track')) {
$store->track($model);
}
}
/**
* @param array<int, string>|null $containerKeys
*/
private function preloadedKey(Layout $layout, Language $language, ?Pageable $page, ?array $containerKeys = null): string
{
$containers = $containerKeys === null ? '*' : implode(',', array_values(array_unique($containerKeys)));
return 'layout:' . $layout->id . ':lang:' . $language->id . ':page:' . ($page instanceof Pageable ? $page->id : 0) . ':containers:' . $containers;
}
/**
* @param array<array-key, mixed> $containerKeys
*/
private function loadWidget(
Layout $layout,
Language $language,
?Pageable $page,
string $containerKey,
string $widgetKey,
int $occurrence,
?array $containerKeys = null,
): ?Widget {
$cacheKey = $this->preloadedKey($layout, $language, $page, $containerKeys);
$map = $this->preloaded[$cacheKey] ?? null;
if ($map === null) {
return null;
}
return $map[$containerKey][$widgetKey][$occurrence] ?? null;
}
/**
* @param array<int, string>|null $containerKeys
* @return array<string, array<array-key, mixed>>
*/
private function selectedLayoutContainers(Layout $layout, ?array $containerKeys): array
{
$containers = $layout->getAttribute('containers');
$containers = is_array($containers) ? $containers : [];
if ($containerKeys === null) {
return $containers;
}
return collect($containers)
->filter(fn (mixed $container, string|int $containerKey): bool => in_array((string) $containerKey, $containerKeys, true))
->map(fn (mixed $container): array => is_array($container) ? $container : [])
->all();
}
/**
* @param array<string, array<array-key, mixed>> $containers
* @return array<int, string>
*/
private function selectedWidgetKeys(array $containers): array
{
return collect($containers)
->flatMap(fn (array $container): array => LayoutWidgetData::fromContainer($container))
->map(static fn (array $widgetData): ?string => LayoutWidgetData::key($widgetData))
->filter()
->unique()
->values()
->all();
}
/**
* @param array<string, array<array-key, mixed>> $containers
* @return array<int, array{container: string, occurrence: int}>
*/
private function selectedContainerOccurrences(array $containers): array
{
$positions = [];
foreach ($containers as $containerKey => $container) {
foreach (LayoutWidgetData::fromContainer($container) as $widgetData) {
$positions[] = [
'container' => $containerKey,
'occurrence' => LayoutWidgetData::occurrence($widgetData),
];
}
}
return collect($positions)
->unique(fn (array $position): string => $position['container'] . ':' . $position['occurrence'])
->values()
->all();
}
}