*/ private array $cache = []; public function render(string $slot, ?string $class = null, ?string $label = null): HtmlString { $world = $this->world(); $file = $world?->fileFor($slot); if ($file === null) { return new HtmlString(''); // a template asks for decoration and gets none } $svg = $this->cache[$file] ??= (string) file_get_contents($file); if ($svg === '') { return new HtmlString(''); } $unique = 'm'.Str::lower(Str::random(6)); $svg = (string) preg_replace(['/\s(role|aria-label|aria-hidden)="[^"]*"/', '/.*?<\/title>/s'], '', $svg); $svg = (string) preg_replace(['/id="([^"]+)"/', '/url\(#([^)]+)\)/'], ['id="$1-'.$unique.'"', 'url(#$1-'.$unique.')'], $svg); $attributes = 'class="'.e((string) $class).'" '.($label !== null ? 'role="img" aria-label="'.e($label).'"' : 'aria-hidden="true" focusable="false"'); return new HtmlString((string) preg_replace('/<svg\s/', '<svg '.$attributes.' ', $svg, 1)); } public function has(string $slot): bool { return $this->world()?->fileFor($slot) !== null; } /** * The picture world in force for what is being rendered right now. */ public function world(): ?ImageWorldManifest { $registry = app(ImageWorldRegistry::class); $theme = app()->bound(Theme::class) ? app(Theme::class) : null; return $registry->get($theme?->property()?->image_world) ?? $registry->get($theme?->site->image_world) ?? $registry->get(config('image-worlds.default')); } }