1
0
Files
breadcrumb-the-shire/core/Service/Settings/SettingCacheService.php
fs 149d4515de refactor(settings): remove global appearance settings — tenant is sole source
Removes the global app_theme, app_theme_user and app_primary_color settings
from the admin/settings area and the underlying service/cache/API/i18n/docs
layers. The tenant columns (tenants.primary_color, default_theme,
allow_user_theme) become the single source of truth for appearance.
Branding helpers are tenant-only and fall back to a hardcoded 'light' theme
with no brand color when no tenant context is available (login, error,
pre-session pages). The APP_THEME env var is removed.

Scope:
- UI: Appearance tab gone from /admin/settings; tenant edit form drops the
  global-fallback color preview.
- Service: SettingsAppGateway no longer exposes setting-backed accessors
  for theme/user-theme/primary-color. Theme catalog utilities (listThemes,
  isAllowedTheme, normalizeTheme, resolveDefaultTheme) stay and are used
  across Tenant / Auth / User flows; resolveDefaultTheme now hardcodes
  'light' with a catalog fallback (no global/env lookup).
- AdminSettingsService: buildPageData, sanitization, audit snapshot, apply
  step and cache payload are all stripped of the three keys. Dead helper
  applyAppPrimaryColor + normalizePrimaryColor removed.
- Cache: SettingCacheService HOT_PATH_KEYS drops the three keys.
- API: /settings (GET/PUT) and /settings/public (GET) no longer expose
  appearance fields; OpenAPI schemas pruned accordingly.
- Audit redaction list shortened.
- DB: db/init/init.sql seed rows removed. No migration for existing
  installs — legacy rows stay inert.
- i18n + docs: setting.app_theme, setting.app_theme_user,
  setting.app_primary_color removed from de+en locales; reference and
  explanation docs updated.
- Tests: covering tests for the removed methods pruned; ThemeResolutionTest
  rewritten for tenant-only semantics. One unrelated PHP-CS-Fixer issue in
  UserRegistrar.php cleaned up to keep QG-006 green.

Workflow: .agents/runs/SETTINGS-APPEARANCE-TENANT-ONLY/ (gitignored).
Gates: QG-001..003, QG-006, QG-008 all pass (1985 tests, 28764 assertions).
Reviews: code, security, acceptance all pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 22:40:15 +02:00

157 lines
4.3 KiB
PHP

<?php
namespace MintyPHP\Service\Settings;
class SettingCacheService
{
private const HOT_PATH_KEYS = [
SettingKeys::APP_TITLE_KEY,
SettingKeys::APP_LOCALE_KEY,
SettingKeys::APP_REGISTRATION_KEY,
];
private ?array $settings = null;
private ?string $cacheFilePath;
private ?SettingsMetadataGateway $settingsMetadataGateway;
public function __construct(?string $cacheFilePath = null, ?SettingsMetadataGateway $settingsMetadataGateway = null)
{
$this->cacheFilePath = $cacheFilePath;
$this->settingsMetadataGateway = $settingsMetadataGateway;
}
public function get(string $key): ?string
{
$settings = $this->all();
$value = $settings[$key] ?? null;
if ($value === null) {
return null;
}
$value = trim((string) $value);
return $value !== '' ? $value : null;
}
public function all(): array
{
if ($this->settings !== null) {
return $this->settings;
}
$file = $this->filePath();
if (!is_file($file)) {
$this->settings = $this->hydrateColdStartCache();
return $this->settings;
}
$loaded = include $file;
$this->settings = is_array($loaded) ? $loaded : [];
return $this->settings;
}
public function update(array $values): bool
{
$current = $this->all();
foreach ($values as $key => $value) {
$settingKey = trim((string) $key);
if ($settingKey === '') {
continue;
}
$current[$settingKey] = $value === null ? null : (string) $value;
}
return $this->write($current);
}
public function write(array $settings): bool
{
$file = $this->filePath();
$dir = dirname($file);
if (!is_dir($dir) && !@mkdir($dir, 0775, true) && !is_dir($dir)) {
return false;
}
// Write to a temp file first, then rename — rename is atomic on most filesystems,
// so readers never see a half-written file. LOCK_EX prevents concurrent write corruption.
$tmp = tempnam($dir, 'settings_');
if ($tmp === false) {
return false;
}
// PHP include returns an array — loaded by OPcache on subsequent requests, much faster than DB.
$payload = "<?php\n\nreturn " . $this->exportValue($settings) . ";\n";
$written = @file_put_contents($tmp, $payload, LOCK_EX);
if ($written === false) {
@unlink($tmp);
return false;
}
if (!@rename($tmp, $file)) {
@unlink($tmp);
return false;
}
$this->settings = $settings;
return true;
}
public function filePath(): string
{
if (is_string($this->cacheFilePath) && $this->cacheFilePath !== '') {
return $this->cacheFilePath;
}
$storagePath = defined('APP_STORAGE_PATH')
? (string) APP_STORAGE_PATH
: dirname(__DIR__, 3) . '/storage';
return rtrim($storagePath, '/') . '/runtime/settings.php';
}
private function hydrateColdStartCache(): array
{
if (!$this->settingsMetadataGateway instanceof SettingsMetadataGateway) {
return [];
}
$hydrated = [];
foreach (self::HOT_PATH_KEYS as $key) {
$value = $this->settingsMetadataGateway->getValue($key);
if ($value === null) {
continue;
}
$hydrated[$key] = (string) $value;
}
if ($this->write($hydrated)) {
return $this->settings ?? $hydrated;
}
$this->settings = $hydrated;
return $hydrated;
}
private function exportValue(mixed $value, int $depth = 0): string
{
if (!is_array($value)) {
return var_export($value, true);
}
if ($value === []) {
return '[]';
}
$indent = str_repeat(' ', $depth);
$childIndent = str_repeat(' ', $depth + 1);
$lines = [];
foreach ($value as $key => $item) {
$lines[] = $childIndent
. var_export($key, true)
. ' => '
. $this->exportValue($item, $depth + 1)
. ',';
}
return "[\n" . implode("\n", $lines) . "\n" . $indent . ']';
}
}