- Add icanhazstring/composer-unused as dev dependency for dependency hygiene checks - Add German documentation (docs/) covering architecture, conventions, workflows, and developer checklists - Add API layer (ApiAuth, ApiBootstrap, ApiResponse), audit, scheduler, custom fields, and SSO services - Add Microsoft OIDC SSO, API token management, and user lifecycle features - Add swagger-ui vendor integration and OpenAPI spec - Add production Docker setup and bin/ scripts - Update composer dependencies, config, templates, and frontend assets throughout Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
109 lines
2.7 KiB
PHP
109 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace MintyPHP\Service\Settings;
|
|
|
|
class SettingCacheService
|
|
{
|
|
private static ?array $settings = null;
|
|
|
|
public static function get(string $key): ?string
|
|
{
|
|
$settings = self::all();
|
|
$value = $settings[$key] ?? null;
|
|
if ($value === null) {
|
|
return null;
|
|
}
|
|
$value = trim((string) $value);
|
|
return $value !== '' ? $value : null;
|
|
}
|
|
|
|
public static function all(): array
|
|
{
|
|
if (self::$settings !== null) {
|
|
return self::$settings;
|
|
}
|
|
|
|
$file = self::filePath();
|
|
if (!is_file($file)) {
|
|
self::$settings = [];
|
|
return self::$settings;
|
|
}
|
|
|
|
$loaded = include $file;
|
|
self::$settings = is_array($loaded) ? $loaded : [];
|
|
return self::$settings;
|
|
}
|
|
|
|
public static function update(array $values): bool
|
|
{
|
|
$current = self::all();
|
|
foreach ($values as $key => $value) {
|
|
$settingKey = trim((string) $key);
|
|
if ($settingKey === '') {
|
|
continue;
|
|
}
|
|
$current[$settingKey] = $value === null ? null : (string) $value;
|
|
}
|
|
return self::write($current);
|
|
}
|
|
|
|
public static function write(array $settings): bool
|
|
{
|
|
$file = self::filePath();
|
|
$dir = dirname($file);
|
|
if (!is_dir($dir) && !@mkdir($dir, 0775, true) && !is_dir($dir)) {
|
|
return false;
|
|
}
|
|
|
|
$tmp = tempnam($dir, 'settings_');
|
|
if ($tmp === false) {
|
|
return false;
|
|
}
|
|
|
|
$payload = "<?php\nreturn " . self::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;
|
|
}
|
|
|
|
self::$settings = $settings;
|
|
return true;
|
|
}
|
|
|
|
public static function filePath(): string
|
|
{
|
|
return dirname(__DIR__, 3) . '/config/settings.php';
|
|
}
|
|
|
|
private static 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)
|
|
. ' => '
|
|
. self::exportValue($item, $depth + 1)
|
|
. ',';
|
|
}
|
|
|
|
return "[\n" . implode("\n", $lines) . "\n" . $indent . ']';
|
|
}
|
|
}
|