cambios importantes
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user