feat: new xboard

This commit is contained in:
xboard
2025-01-21 14:57:54 +08:00
parent de18cfe596
commit 0f43fff242
373 changed files with 17923 additions and 20264 deletions

View File

@@ -2,36 +2,20 @@
namespace App\Support;
use Illuminate\Database\QueryException;
use App\Models\Setting as SettingModel;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Fluent;
class Setting extends Fluent
class Setting
{
const CACHE_KEY = 'admin_settings';
private $cache;
public function __construct()
{
$this->attributes = self::fromDatabase();
$this->cache = Cache::store('octane');
}
/**
* 获取配置,并转化为数组.
*
* @param string $key
* @param mixed $default
* @return array
*/
public function getArray($key, $default = [])
{
$value = $this->get($key, $default);
if (!$value) {
return [];
}
return is_array($value) ? $value : (json_decode($value, true) ?: []);
}
/**
* 获取配置.
*
@@ -41,7 +25,8 @@ class Setting extends Fluent
*/
public function get($key, $default = null)
{
return Arr::get($this->attributes, $key, $default);
$key = strtolower($key);
return Arr::get($this->fromDatabase(), $key, $default);
}
/**
@@ -50,51 +35,17 @@ class Setting extends Fluent
* @param array $data
* @return $this
*/
public function set($key, $value = null)
public function set($key, $value = null): bool
{
$data = is_array($key) ? $key : [$key => $value];
foreach ($data as $key => $value) {
Arr::set($this->attributes, $key, $value);
if (is_array($value)) {
$value = json_encode($value);
}
return $this;
$key = strtolower($key);
SettingModel::updateOrCreate(['name' => $key], ['value' => $value]);
$this->cache->forget(self::CACHE_KEY);
return true;
}
/**
* 追加数据.
*
* @param mixed $key
* @param mixed $value
* @param mixed $k
* @return $this
*/
public function add($key, $value, $k = null)
{
$results = $this->getArray($key);
if ($k !== null) {
$results[] = $value;
} else {
$results[$k] = $value;
}
return $this->set($key, $results);
}
/**
* 批量追加数据.
*
* @param string $key
* @param array $value
* @return $this
*/
public function addMany($key, array $value)
{
$results = $this->getArray($key);
return $this->set($key, array_merge($results, $value));
}
/**
* 保存配置到数据库.
@@ -102,43 +53,40 @@ class Setting extends Fluent
* @param array $data
* @return $this
*/
public function save(array $data = [])
public function save(array $data = []): bool
{
if ($data) {
$this->set($data);
foreach ($data as $key => $value) {
$this->set($key, $value);
}
foreach ($this->attributes as $key => $value) {
if (is_array($value)) {
$value = json_encode($value);
}
$model = SettingModel::query()
->where('name', $key)
->first() ?: new SettingModel();
$model->fill([
'name' => $key,
'value' => (string) $value,
])->save();
}
Cache::forget('admin_settings');
return $this;
return true;
}
/**
* @return static
* 删除配置信息
*
* @param string $key
* @return bool
*/
public static function fromDatabase()
public function remove($key): bool
{
SettingModel::where('name', $key)->delete();
$this->cache->forget(self::CACHE_KEY);
return true;
}
/**
* 获取配置信息.
* @return array
*/
public function fromDatabase(): array
{
$values = [];
try {
$values = Cache::remember('admin_settings', env('ADMIN_SETTING_CACHE', 0), function () {
return SettingModel::pluck('value', 'name')->toArray();
return $this->cache->rememberForever(self::CACHE_KEY, function (): array {
return array_change_key_case(SettingModel::pluck('value', 'name')->toArray(), CASE_LOWER);
});
} catch (QueryException $e) {
} catch (\Throwable $th) {
return [];
}
return $values;
}
}