Refactor helper drift in lib: centralize theme/translation and remove app() hotspots

This commit is contained in:
2026-03-19 08:23:14 +01:00
parent 5da506a20f
commit 5739cc1200
24 changed files with 197 additions and 88 deletions

View File

@@ -57,7 +57,7 @@ class SettingsAppGateway
{
$value = $this->settingsMetadataGateway->getValue(SettingKeys::APP_THEME_KEY);
$value = $value !== null ? strtolower(trim($value)) : '';
if (!in_array($value, $this->allowedThemes(), true)) {
if (!$this->isAllowedTheme($value)) {
return null;
}
@@ -67,7 +67,7 @@ class SettingsAppGateway
public function setAppTheme(?string $theme, ?string $description = null): bool
{
$value = $theme !== null ? strtolower(trim($theme)) : '';
if ($value === '' || !in_array($value, $this->allowedThemes(), true)) {
if ($value === '' || !$this->isAllowedTheme($value)) {
$value = null;
}
@@ -75,6 +75,50 @@ class SettingsAppGateway
return $this->settingsMetadataGateway->set(SettingKeys::APP_THEME_KEY, $value, $desc);
}
/**
* @return array<string, string>
*/
public function listThemes(): array
{
return $this->themeConfigService->all();
}
public function isAllowedTheme(?string $theme): bool
{
$value = strtolower(trim((string) $theme));
return $value !== '' && in_array($value, $this->allowedThemes(), true);
}
public function resolveDefaultTheme(): string
{
$settingTheme = $this->getAppTheme();
if ($settingTheme !== null) {
return $settingTheme;
}
$envTheme = strtolower(trim((string) (getenv('APP_THEME') ?: '')));
if ($this->isAllowedTheme($envTheme)) {
return $envTheme;
}
$allowedThemes = $this->allowedThemes();
if (in_array('light', $allowedThemes, true)) {
return 'light';
}
return (string) ($allowedThemes[0] ?? 'light');
}
public function normalizeTheme(?string $theme): string
{
$value = strtolower(trim((string) $theme));
if ($this->isAllowedTheme($value)) {
return $value;
}
return $this->resolveDefaultTheme();
}
public function isUserThemeAllowed(): bool
{
$value = $this->settingsMetadataGateway->getValue(SettingKeys::APP_THEME_USER_KEY);
@@ -146,7 +190,7 @@ class SettingsAppGateway
private function allowedThemes(): array
{
return array_keys($this->themeConfigService->all());
return array_keys($this->listThemes());
}
public function normalizeLocale(?string $locale): string