Files
Xboard/app/Support/Setting.php

141 lines
3.3 KiB
PHP
Raw Normal View History

2023-11-17 14:44:01 +08:00
<?php
namespace App\Support;
use App\Models\Setting as SettingModel;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Illuminate\Contracts\Cache\Repository;
2023-11-17 14:44:01 +08:00
2025-01-21 14:57:54 +08:00
class Setting
2023-11-17 14:44:01 +08:00
{
2025-01-21 14:57:54 +08:00
const CACHE_KEY = 'admin_settings';
private Repository $cache;
private ?array $loadedSettings = null; // 请求内缓存
public function __construct()
{
$this->cache = Cache::store('redis');
2023-11-17 14:44:01 +08:00
}
/**
* 获取配置.
*/
public function get(string $key, mixed $default = null): mixed
{
$this->load();
return Arr::get($this->loadedSettings, strtolower($key), $default);
}
2023-11-17 14:44:01 +08:00
/**
* 设置配置信息.
2023-11-17 14:44:01 +08:00
*/
public function set(string $key, mixed $value = null): bool
2023-11-17 14:44:01 +08:00
{
SettingModel::createOrUpdate(strtolower($key), $value);
$this->flush();
return true;
}
/**
* 保存配置到数据库.
*/
public function save(array $settings): bool
{
foreach ($settings as $key => $value) {
SettingModel::createOrUpdate(strtolower($key), $value);
}
$this->flush();
return true;
}
/**
* 删除配置信息
*/
public function remove(string $key): bool
{
SettingModel::where('name', $key)->delete();
$this->flush();
return true;
2023-11-17 14:44:01 +08:00
}
/**
* 更新单个设置项
2023-11-17 14:44:01 +08:00
*/
public function update(string $key, $value): bool
2023-11-17 14:44:01 +08:00
{
return $this->set($key, $value);
2023-11-17 14:44:01 +08:00
}
2023-11-17 14:44:01 +08:00
/**
* 批量获取配置项
2023-11-17 14:44:01 +08:00
*/
public function getBatch(array $keys): array
2023-11-17 14:44:01 +08:00
{
$this->load();
$result = [];
foreach ($keys as $index => $item) {
$isNumericIndex = is_numeric($index);
$key = strtolower($isNumericIndex ? $item : $index);
$default = $isNumericIndex ? config('v2board.' . $item) : (config('v2board.' . $key) ?? $item);
$result[$item] = Arr::get($this->loadedSettings, $key, $default);
2025-01-21 14:57:54 +08:00
}
return $result;
2023-11-17 14:44:01 +08:00
}
2023-11-17 14:44:01 +08:00
/**
* 将所有设置转换为数组
2023-11-17 14:44:01 +08:00
*/
public function toArray(): array
2023-11-17 14:44:01 +08:00
{
$this->load();
return $this->loadedSettings;
2023-11-17 14:44:01 +08:00
}
/**
* 加载配置到请求内缓存
2023-11-17 14:44:01 +08:00
*/
private function load(): void
2023-11-17 14:44:01 +08:00
{
if ($this->loadedSettings !== null) {
return;
}
2023-11-17 14:44:01 +08:00
try {
$settings = $this->cache->rememberForever(self::CACHE_KEY, function (): array {
return array_change_key_case(
SettingModel::pluck('value', 'name')->toArray(),
CASE_LOWER
);
});
// 处理JSON格式的值
foreach ($settings as $key => $value) {
if (is_string($value)) {
$decoded = json_decode($value, true);
if (json_last_error() === JSON_ERROR_NONE) {
$settings[$key] = $decoded;
}
}
}
$this->loadedSettings = $settings;
} catch (\Throwable) {
$this->loadedSettings = [];
2023-11-17 14:44:01 +08:00
}
}
/**
* 清空缓存
*/
private function flush(): void
{
$this->cache->forget(self::CACHE_KEY);
$this->loadedSettings = null;
}
2023-11-17 14:44:01 +08:00
}