Initial dev export (exclude uploads/runtime)
This commit is contained in:
67
core/services/Settings.php
Normal file
67
core/services/Settings.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Core\Services;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class Settings
|
||||
{
|
||||
private static array $data = [];
|
||||
private static bool $loaded = false;
|
||||
|
||||
public static function init(string $path): void
|
||||
{
|
||||
if (self::$loaded) {
|
||||
return;
|
||||
}
|
||||
if (is_file($path)) {
|
||||
$loaded = require $path;
|
||||
if (is_array($loaded)) {
|
||||
self::$data = $loaded;
|
||||
}
|
||||
}
|
||||
self::reload();
|
||||
self::$loaded = true;
|
||||
}
|
||||
|
||||
public static function get(string $key, string $default = ''): string
|
||||
{
|
||||
return array_key_exists($key, self::$data) ? (string)self::$data[$key] : $default;
|
||||
}
|
||||
|
||||
public static function reload(): void
|
||||
{
|
||||
$db = Database::get();
|
||||
if (!$db) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
$rows = $db->query("SELECT setting_key, setting_value FROM ac_settings")->fetchAll();
|
||||
foreach ($rows as $row) {
|
||||
$k = (string)($row['setting_key'] ?? '');
|
||||
$v = (string)($row['setting_value'] ?? '');
|
||||
if ($k !== '') {
|
||||
self::$data[$k] = $v;
|
||||
}
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public static function set(string $key, string $value): void
|
||||
{
|
||||
self::$data[$key] = $value;
|
||||
$db = Database::get();
|
||||
if (!$db) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
$stmt = $db->prepare("REPLACE INTO ac_settings (setting_key, setting_value) VALUES (:k, :v)");
|
||||
$stmt->execute([':k' => $key, ':v' => $value]);
|
||||
} catch (Throwable $e) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user