53 lines
No EOL
1.3 KiB
PHP
53 lines
No EOL
1.3 KiB
PHP
<?php
|
|
class KeyValue {
|
|
protected static $kv = null;
|
|
|
|
public static function get(string $keyname, ?string $channel = null) {
|
|
if (self::$kv === null) {
|
|
self::load();
|
|
}
|
|
|
|
if ($channel === null) {
|
|
$channel = "all";
|
|
}
|
|
|
|
if (isset(self::$kv[$keyname][$channel])) {
|
|
return self::$kv[$keyname][$channel];
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static function save(string $keyname, string $value, ?string $channel = null) {
|
|
if (self::$kv === null) {
|
|
self::load();
|
|
}
|
|
|
|
if ($channel === null) {
|
|
$channel = "all";
|
|
}
|
|
|
|
$db = new Database();
|
|
$stmt = $db::$handle->prepare("INSERT OR REPLACE INTO `keyvalue` (`key`, `value`, `channel`, `created`) VALUES (:key, :value, :channel, strftime('%Y-%m-%d %H:%M:%S','now'))");
|
|
$stmt->bindValue(":key", $keyname, SQLITE3_TEXT);
|
|
$stmt->bindValue(":value", $value, SQLITE3_TEXT);
|
|
$stmt->bindValue(":channel", $channel, SQLITE3_TEXT);
|
|
$stmt->execute();
|
|
|
|
self::$kv[$keyname][$channel] = $value;
|
|
}
|
|
|
|
protected static function load() {
|
|
$db = new Database();
|
|
$stmt = $db::$handle->prepare("SELECT * FROM `keyvalue`");
|
|
$result = $stmt->execute();
|
|
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
|
|
$channel = $row["channel"];
|
|
if ($channel === null) {
|
|
$channel = "all";
|
|
}
|
|
|
|
self::$kv[$row["key"]][$channel] = $row["value"];
|
|
}
|
|
}
|
|
} |