Files
2026-06-12 12:18:21 -05:00

41 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
class Settings
{
private static ?array $cache = null;
public static function all(): array
{
if (self::$cache !== null) return self::$cache;
$rows = db()->query("SELECT `key`, `value` FROM settings")->fetchAll();
$map = [];
foreach ($rows as $r) $map[$r['key']] = $r['value'];
self::$cache = $map;
return $map;
}
public static function get(string $key, string $default = ''): string
{
$all = self::all();
return $all[$key] ?? $default;
}
public static function set(string $key, string $value): void
{
$stmt = db()->prepare("INSERT INTO settings (`key`, `value`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `value` = ?");
$stmt->execute([$key, $value, $value]);
self::$cache = null;
}
public static function setMany(array $pairs): void
{
$db = db();
$stmt = $db->prepare("INSERT INTO settings (`key`, `value`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `value` = ?");
foreach ($pairs as $key => $value) {
$stmt->execute([$key, (string)$value, (string)$value]);
}
self::$cache = null;
}
}