Files
fewo-stommer/app/Themes/Theme.php
T
Lars StommerandClaude Opus 5.5 66e82d61f5 Theme-System mit Basis-Theme Ostsee-Licht, Bausteinen und saisonalen Erscheinungsbildern
- Sites (Domain, Theme, Farben; mehrfähig für weitere Vermieter), Seiten aus Bausteinen mit Menüs, Erscheinungsbilder mit jährlichem Zeitplan, Priorität und signierter Vorschau
- Theme-Kern (app/Themes): Manifest, Registry, Vererbung (Child-Themes), Token-Farben als CSS-Variablen, Bausteinkatalog als Registry (config/themes.php)
- 15 Bausteine: Titelbereich mit Suche, Eckdaten, Text mit Bildern, Galerie, Ausstattung, Lage, Preise, Angebote, Kalender & Anfrage, Bewertungen, Gastgeber, FAQ, Abschluss, Kontakt, Fließtext
- Theme Ostsee-Licht: Layout, Kopf/Fuß, Banner, mobile Buchungsleiste, Lightbox, selbst gehostete Schriften, Reduced Motion
- Seiten Start, Wohnung, Kühlungsborn, Preise & Buchung, Kontakt, Impressum, Datenschutz; Sitemap, robots.txt, JSON-LD
- Admin: Seiten-Baukasten, Erscheinungsbilder, Websites
- Dokumentation docs/THEMES.md inkl. Übergabe für die Design-Sitzung

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 15:04:21 +02:00

143 lines
4.0 KiB
PHP

<?php
namespace App\Themes;
use App\Models\Appearance;
use App\Models\Page;
use App\Models\Photo;
use App\Models\Property;
use App\Models\Site;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\View;
/**
* The theme context of the current request: site, theme chain and appearance. Activating it registers the
* view namespace "theme::" with the theme's own views first, then its parents, then the base theme.
*/
class Theme
{
private ?Property $property = null;
private bool $propertyResolved = false;
/** @var array<string, Collection<int, Page>> */
private array $menus = [];
public function __construct(
private ThemeRegistry $registry,
public readonly Site $site,
public readonly ?Appearance $appearance,
public readonly bool $isPreview = false,
) {}
public function activate(): self
{
$paths = array_map(fn (ThemeManifest $theme): string => $theme->viewsPath(), $this->chain());
View::replaceNamespace('theme', $paths);
return $this;
}
public function manifest(): ThemeManifest
{
return $this->registry->get($this->site->theme ?: config('themes.base'));
}
/**
* @return list<ThemeManifest>
*/
public function chain(): array
{
return $this->registry->chain($this->manifest()->key);
}
/**
* Colour tokens: theme defaults (base first, then children), site settings, appearance.
*
* @return array<string, string>
*/
public function tokens(): array
{
$tokens = [];
foreach (array_reverse($this->chain()) as $theme) {
foreach ($theme->tokens as $key => $token) {
$tokens[$key] = $token['default'];
}
}
$overrides = [...($this->site->theme_settings['tokens'] ?? []), ...($this->appearance?->tokens ?? [])];
foreach ($overrides as $key => $value) {
if (isset($tokens[$key]) && is_string($value) && preg_match('/^#[0-9a-fA-F]{3,8}$/', $value)) {
$tokens[$key] = $value;
}
}
return $tokens;
}
/**
* ":root{--ground:#F6F2EA;…}" for an inline <style> in the layout.
*/
public function cssVariables(): string
{
return ':root{'.collect($this->tokens())->map(fn (string $value, string $key): string => "--{$key}:{$value}")->implode(';').'}';
}
/**
* The appearance's replacement for a text, else $fallback.
*/
public function text(string $key, ?string $fallback = null): ?string
{
return $this->appearance?->override($key) ?? $fallback;
}
public function heroPhoto(?Photo $fallback = null): ?Photo
{
return $this->appearance?->heroPhoto ?? $fallback;
}
public function appearanceKey(): string
{
return $this->appearance ? str($this->appearance->name)->slug()->toString() : 'standard';
}
public function property(): ?Property
{
if (! $this->propertyResolved) {
$this->property = $this->site->exists ? $this->site->primaryProperty() : Property::query()->where('is_active', true)->first();
$this->propertyResolved = true;
}
return $this->property;
}
/**
* Published pages of a menu location ("main" or "footer").
*
* @return Collection<int, Page>
*/
public function menu(string $location): Collection
{
return $this->menus[$location] ??= $this->site->exists
? $this->site->pages()->where('is_published', true)->where('menu_location', $location)->get()
: collect();
}
/**
* Vite entry points of the active theme (a child theme without own assets uses its parent's).
*
* @return list<string>
*/
public function assets(): array
{
foreach ($this->chain() as $theme) {
$assets = array_values(array_filter([$theme->assets['css'] ?? null, $theme->assets['js'] ?? null]));
if ($assets !== []) {
return $assets;
}
}
return [];
}
}