-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildPublicLayoutGraphAction.php
More file actions
355 lines (301 loc) · 11.8 KB
/
Copy pathBuildPublicLayoutGraphAction.php
File metadata and controls
355 lines (301 loc) · 11.8 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
<?php
declare(strict_types=1);
namespace Capell\LayoutBuilder\Actions;
use Capell\Core\Models\Blueprint;
use Capell\Core\Models\Language;
use Capell\Core\Models\Layout;
use Capell\Core\Models\Page;
use Capell\Core\Models\Site;
use Capell\LayoutBuilder\Contracts\PublicLayoutWidgetPayloadResolver;
use Capell\LayoutBuilder\Data\PublicLayoutContainerData;
use Capell\LayoutBuilder\Data\PublicLayoutGraphData;
use Capell\LayoutBuilder\Data\PublicLayoutWidgetData;
use Capell\LayoutBuilder\Models\Widget;
use Capell\LayoutBuilder\Support\CapellLayoutManager;
use Capell\LayoutBuilder\Support\LayoutWidgetData;
use Capell\LayoutBuilder\Support\Loader\LayoutLoader;
use Illuminate\Support\Str;
use Lorisleiva\Actions\Concerns\AsFake;
use Lorisleiva\Actions\Concerns\AsObject;
/**
* @method static PublicLayoutGraphData run(Layout $layout, Page $page, Language $language, array<int, string> $containers = [], bool $includeHtml = false)
*/
class BuildPublicLayoutGraphAction
{
use AsFake;
use AsObject;
/**
* @param array<int, string> $containers
*/
public function handle(Layout $layout, Page $page, Language $language, array $containers = [], bool $includeHtml = false): PublicLayoutGraphData
{
$layoutContainers = $layout->getAttribute('containers');
$layoutContainers = is_array($layoutContainers) ? $layoutContainers : [];
$page->setRelation('layout', $layout);
$this->hydrateSiteRelation($layout, $page);
$selectedContainers = $this->selectedContainers($containers);
$loader = resolve(LayoutLoader::class);
$resolver = resolve(PublicLayoutWidgetPayloadResolver::class);
$loader->preloadLayoutWidgets($layout, $language, $page, $selectedContainers);
return new PublicLayoutGraphData(
key: $layout->key,
meta: [],
containers: collect($layoutContainers)
->filter(fn (mixed $container, string|int $containerKey): bool => $this->shouldIncludeContainer((string) $containerKey, $selectedContainers))
->map(fn (mixed $container, string|int $containerKey): PublicLayoutContainerData => $this->containerData(
layout: $layout,
page: $page,
language: $language,
loader: $loader,
resolver: $resolver,
containerKey: (string) $containerKey,
container: is_array($container) ? $container : [],
includeHtml: $includeHtml,
selectedContainers: $selectedContainers,
))
->values()
->all(),
);
}
/**
* @param array<string, mixed> $container
* @param array<int, string>|null $selectedContainers
*/
private function containerData(
Layout $layout,
Page $page,
Language $language,
LayoutLoader $loader,
PublicLayoutWidgetPayloadResolver $resolver,
string $containerKey,
array $container,
bool $includeHtml,
?array $selectedContainers,
): PublicLayoutContainerData {
$widgets = LayoutWidgetData::fromContainer($container);
return new PublicLayoutContainerData(
key: $containerKey,
meta: [],
widgets: collect($widgets)
->map(fn (mixed $widgetData): ?PublicLayoutWidgetData => $this->widgetData(
layout: $layout,
page: $page,
language: $language,
loader: $loader,
resolver: $resolver,
containerKey: $containerKey,
widgetData: $widgetData,
includeHtml: $includeHtml,
selectedContainers: $selectedContainers,
))
->filter()
->values()
->all(),
);
}
/**
* @param array<string, mixed> $widgetData
* @param array<int, string>|null $selectedContainers
*/
private function widgetData(
Layout $layout,
Page $page,
Language $language,
LayoutLoader $loader,
PublicLayoutWidgetPayloadResolver $resolver,
string $containerKey,
array $widgetData,
bool $includeHtml,
?array $selectedContainers,
): ?PublicLayoutWidgetData {
$widgetKey = LayoutWidgetData::key($widgetData);
if ($widgetKey === null) {
return null;
}
$occurrence = LayoutWidgetData::occurrence($widgetData);
$widget = CapellLayoutManager::getStoredContainerWidget($containerKey, $widgetKey, $occurrence)
?? $loader->getLayoutWidget($layout, $widgetKey, $language, $page, $containerKey, $occurrence, $selectedContainers);
if (! $widget instanceof Widget) {
return null;
}
$publicWidget = $this->widgetWithPublicOccurrenceMeta($widget, $widgetData);
$blueprint = $widget->relationLoaded('blueprint') ? $widget->getRelation('blueprint') : null;
return new PublicLayoutWidgetData(
key: $widgetKey,
occurrence: $occurrence,
type: $blueprint instanceof Blueprint ? $blueprint->key : null,
data: $resolver->data($publicWidget, $page, $language, $containerKey, $occurrence),
html: $includeHtml ? $resolver->html($publicWidget, $page, $language, $containerKey, $occurrence) : null,
);
}
/**
* @param array<string, mixed> $widgetData
*/
private function widgetWithPublicOccurrenceMeta(Widget $widget, array $widgetData): Widget
{
$occurrenceMeta = is_array($widgetData['meta'] ?? null) ? $widgetData['meta'] : [];
$safeOccurrenceMeta = $this->safePublicWidgetMeta($occurrenceMeta);
$baseMeta = is_array($widget->meta) ? $widget->meta : [];
$publicWidget = clone $widget;
$publicWidget->setAttribute('meta', array_replace_recursive($this->safePublicWidgetMeta($baseMeta), $safeOccurrenceMeta));
return $publicWidget;
}
/**
* @param array<string, mixed> $meta
* @return array<string, mixed>
*/
private function safePublicWidgetMeta(array $meta): array
{
$safeMeta = [];
foreach (['widget_key', 'widget_variant'] as $key) {
$value = $meta[$key] ?? null;
if ($this->isSafeIdentifier($value)) {
$safeMeta[$key] = trim((string) $value);
}
}
foreach (['show_home', 'show_parent', 'show_current_page'] as $key) {
$value = $this->safeBoolean($meta[$key] ?? null);
if ($value !== null) {
$safeMeta[$key] = $value;
}
}
$minimumItems = $meta['minimum_items'] ?? null;
if (is_int($minimumItems)) {
$safeMeta['minimum_items'] = max(1, min(10, $minimumItems));
} elseif (is_string($minimumItems) && ctype_digit($minimumItems)) {
$safeMeta['minimum_items'] = max(1, min(10, (int) $minimumItems));
}
$settings = is_array($meta['widget_settings'] ?? null) ? $meta['widget_settings'] : [];
$safeSettings = [];
$this->putAllowedSetting($safeSettings, 'spacing', $settings['spacing'] ?? null, ['tight', 'normal', 'spacious']);
$this->putAllowedSetting($safeSettings, 'background', $settings['background'] ?? null, ['default', 'muted', 'dark', 'image']);
$this->putAllowedSetting($safeSettings, 'media_position', $settings['media_position'] ?? null, ['left', 'right', 'top']);
$this->putAllowedSetting($safeSettings, 'heading_width', $settings['heading_width'] ?? null, ['narrow', 'normal', 'wide']);
$cardsPerRow = $settings['cards_per_row'] ?? null;
if (is_int($cardsPerRow)) {
$safeSettings['cards_per_row'] = max(1, min(6, $cardsPerRow));
} elseif (is_string($cardsPerRow) && ctype_digit($cardsPerRow)) {
$safeSettings['cards_per_row'] = max(1, min(6, (int) $cardsPerRow));
}
$showCta = $settings['show_cta'] ?? null;
if (is_bool($showCta)) {
$safeSettings['show_cta'] = $showCta;
}
$anchorId = $this->safeAnchorId($settings['anchor_id'] ?? null);
if ($anchorId !== null) {
$safeSettings['anchor_id'] = $anchorId;
}
if ($safeSettings !== []) {
$safeMeta['widget_settings'] = $safeSettings;
}
return $safeMeta;
}
private function hydrateSiteRelation(Layout $layout, Page $page): void
{
if ($page->relationLoaded('site')) {
return;
}
$layoutSite = $layout->relationLoaded('site') ? $layout->getRelation('site') : null;
if ($layoutSite instanceof Site) {
$page->setRelation('site', $layoutSite);
return;
}
$siteId = $page->getAttribute('site_id');
if (! is_int($siteId) && ! is_string($siteId)) {
return;
}
$site = Site::query()->find($siteId);
if ($site instanceof Site) {
$page->setRelation('site', $site);
}
}
private function isSafeIdentifier(mixed $value): bool
{
return is_string($value)
&& ! $this->containsUnsafePublicMarker($value)
&& preg_match('/\A[a-z0-9][a-z0-9._-]{0,127}\z/i', trim($value)) === 1;
}
private function safeBoolean(mixed $value): ?bool
{
if (is_bool($value)) {
return $value;
}
if (is_int($value)) {
return match ($value) {
0 => false,
1 => true,
default => null,
};
}
if (! is_string($value)) {
return null;
}
return match (strtolower(trim($value))) {
'0', 'false', 'no', 'off' => false,
'1', 'true', 'yes', 'on' => true,
default => null,
};
}
/**
* @param array<string, mixed> $safeSettings
* @param array<int, string> $allowed
*/
private function putAllowedSetting(array &$safeSettings, string $key, mixed $value, array $allowed): void
{
if (is_string($value) && in_array($value, $allowed, true)) {
$safeSettings[$key] = $value;
}
}
private function safeAnchorId(mixed $value): ?string
{
if (! is_string($value) || trim($value) === '') {
return null;
}
if ($this->containsUnsafePublicMarker($value)) {
return null;
}
if (preg_match('/[:\/?#&=@\\\\]/', $value) === 1) {
return null;
}
$anchorId = Str::slug($value);
return $anchorId === '' || strlen($anchorId) > 80 ? null : $anchorId;
}
private function containsUnsafePublicMarker(string $value): bool
{
$tokens = preg_split('/[^a-z0-9]+/i', strtolower($value), flags: PREG_SPLIT_NO_EMPTY);
if (! is_array($tokens)) {
return true;
}
return array_intersect($tokens, [
'admin',
'authoring',
'filament',
'permission',
'permissions',
'schema',
'secret',
'selector',
'signed',
'token',
'url',
]) !== [];
}
/**
* @param array<int, string> $containers
* @return array<int, string>|null
*/
private function selectedContainers(array $containers): ?array
{
if ($containers === [] || in_array('*', $containers, true)) {
return null;
}
return array_values(array_unique($containers));
}
/**
* @param array<int, string>|null $selectedContainers
*/
private function shouldIncludeContainer(string $containerKey, ?array $selectedContainers): bool
{
return $selectedContainers === null || in_array($containerKey, $selectedContainers, true);
}
}