Files
2026-07-22 10:18:59 +08:00

59 lines
1.6 KiB
PHP

<?php
namespace app\service;
use app\model\SystemSetting;
class SettingsService
{
public static function get(string $key, mixed $default = null): mixed
{
$row = SystemSetting::where('setting_key', $key)->find();
if (!$row) {
return $default;
}
$decoded = json_decode($row->setting_value, true);
return json_last_error() === JSON_ERROR_NONE ? $decoded : $row->setting_value;
}
public static function set(string $key, mixed $value): void
{
$stored = is_array($value) || is_object($value)
? json_encode($value, JSON_UNESCAPED_UNICODE)
: (string) $value;
$setting = SystemSetting::where('setting_key', $key)->find();
if ($setting) {
$setting->save(['setting_value' => $stored]);
} else {
SystemSetting::create([
'setting_key' => $key,
'setting_value' => $stored,
]);
}
}
public static function getFeatures(): array
{
return self::get('features', [
'markdown' => true,
'image' => true,
'video' => true,
'voice' => true,
'document' => true,
'emoji' => true,
'upload_image' => true,
'upload_video' => true,
'upload_file' => true,
'paste_image' => true,
]);
}
public static function isFeatureEnabled(string $feature): bool
{
$features = self::getFeatures();
return !empty($features[$feature]);
}
}