Loheide-eu-mitarbeiterplatt.../app/Support/Theme/ThemeCompiler.php
Friederich Loheide 82bbeef31b
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 2m7s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 2m11s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 2m16s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 2m18s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 2m49s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 2m46s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 2m41s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 2m33s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m0s
Phase 5: Erscheinungsbild mit Entwurf, Vorschau und Veröffentlichen
- Theme in den Einstellungen (Entwurf/veröffentlicht), Übernahme der
  Einstellungen aus Phase 0; statische CSS-Datei mit Hash beim Veröffentlichen
- Logos hell/dunkel, Signet, App-Symbol → Favicon, Homescreen-Symbole,
  Web-App-Manifest; SVG-Bereinigung
- Markenfarbe exakt, zweite Akzentfarbe, Kopfleiste hell/Marke/dunkel,
  Kontrastprüfung (WCAG AA) mit automatisch lesbarer Linkfarbe
- Lokale Schriften (Latein), Überschriften-Schrift, eigene WOFF2-Schrift,
  Ecken, Dichte, Dunkelmodus-Regel
- Anmeldeseite (geteilt/zentriert, Bild, Texte), Fußzeile, E-Mails im Design
- Admin: Live-Vorschau (Startseite, Anmeldung, Beispielseite; hell/dunkel),
  Veröffentlichen, Verwerfen, Export/Import, Zurücksetzen, eigenes CSS
- Admin-Bereich übernimmt Logo, Farbe und Favicon; Whitelabel

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 10:46:05 +00:00

98 lines
3.5 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Support\Theme;
use App\Support\ColorPalette;
use Illuminate\Support\Facades\Storage;
/**
* Baut aus dem Theme eine CSS-Datei mit Variablen (Farben, Schriften, Form)
* und dem eigenen CSS. Beim Veröffentlichen wird sie mit Hash im Namen
* gespeichert – kein Build, keine Rechenarbeit pro Seitenaufruf.
*/
class ThemeCompiler
{
/**
* @param array<string, mixed> $theme
*/
public function css(array $theme): string
{
$primary = $theme['primary_color'];
$palette = ColorPalette::fromHex($primary);
$secondary = $theme['secondary_color'] ?? $primary;
$palette2 = ColorPalette::fromHex($secondary);
$light = [
'--brand' => $primary,
'--brand-foreground' => ColorPalette::foregroundFor($primary),
'--brand-text' => $palette[Contrast::textShadeOnLight($primary)],
'--accent2' => $secondary,
'--accent2-foreground' => ColorPalette::foregroundFor($secondary),
'--accent2-text' => $palette2[Contrast::textShadeOnLight($secondary)],
'--radius' => Theme::RADII[$theme['radius']][1],
'--spacing' => Theme::DENSITIES[$theme['density']][1],
'--font-body' => Fonts::stack($theme['font_body']),
'--font-heading' => Fonts::stack($theme['font_heading'] === 'same' ? $theme['font_body'] : $theme['font_heading']),
];
foreach ($palette as $shade => $hex) {
$light["--brand-{$shade}"] = $hex;
}
foreach ($palette2 as $shade => $hex) {
$light["--accent2-{$shade}"] = $hex;
}
$dark = [
'--brand-text' => $palette[Contrast::textShadeOnDark($primary)],
'--accent2-text' => $palette2[Contrast::textShadeOnDark($secondary)],
];
$css = ':root{'.$this->declarations($light).'}'."\n"
.'.dark{'.$this->declarations($dark).'}'."\n";
if ($theme['custom_font']) {
$url = Storage::disk('uploads')->url($theme['custom_font']);
$css .= '@font-face{font-family:"'.Fonts::CUSTOM_FAMILY.'";src:url("'.$url.'") format("woff2");font-weight:100 900;font-display:swap}'."\n";
}
if ($theme['custom_css']) {
// Eigenes CSS steht in einer eigenen CSS-Datei; ein schließendes </style> ist dort bedeutungslos,
// wird aber für die Vorschau (inline) trotzdem entschärft.
$css .= "/* Eigenes CSS */\n".str_ireplace('</style', '<\/style', $theme['custom_css'])."\n";
}
return $css;
}
/**
* CSS-Datei schreiben und ihren Pfad im Speicher „uploads“ liefern.
*
* @param array<string, mixed> $theme
*/
public function write(array $theme): string
{
$css = $this->css($theme);
$path = 'theme/theme-'.substr(sha1($css), 0, 12).'.css';
$disk = Storage::disk('uploads');
if (! $disk->exists($path)) {
$disk->put($path, $css);
}
// Ältere Fassungen aufräumen (die aktuelle und die vorherige bleiben für offene Seiten).
$old = collect($disk->files('theme'))->filter(fn ($f) => preg_match('#^theme/theme-[0-9a-f]{12}\.css$#', $f) && $f !== $path)
->sortByDesc(fn ($f) => $disk->lastModified($f))->slice(1);
$disk->delete($old->all());
return $path;
}
/**
* @param array<string, string> $values
*/
private function declarations(array $values): string
{
return collect($values)->map(fn ($value, $name) => $name.':'.$value)->implode(';');
}
}