2023-11-17 01:44:01 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Support;
|
|
|
|
|
|
|
|
use App\Models\Setting as SettingModel;
|
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use Illuminate\Support\Fluent;
|
|
|
|
|
2025-01-06 12:20:11 -05:00
|
|
|
class Setting
|
2023-11-17 01:44:01 -05:00
|
|
|
{
|
2025-01-06 12:20:11 -05:00
|
|
|
const CACHE_KEY = 'admin_settings';
|
|
|
|
|
|
|
|
private $cache;
|
2024-04-09 12:51:03 -04:00
|
|
|
public function __construct()
|
|
|
|
{
|
2025-01-06 12:20:11 -05:00
|
|
|
$this->cache = Cache::store('octane');
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 获取配置.
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param mixed $default
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function get($key, $default = null)
|
|
|
|
{
|
2025-01-10 08:57:26 -05:00
|
|
|
$key = strtolower($key);
|
2025-01-06 12:20:11 -05:00
|
|
|
return Arr::get($this->fromDatabase(), $key, $default);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 设置配置信息.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return $this
|
|
|
|
*/
|
2025-01-06 12:20:11 -05:00
|
|
|
public function set($key, $value = null): bool
|
2023-11-17 01:44:01 -05:00
|
|
|
{
|
2025-01-06 12:20:11 -05:00
|
|
|
if (is_array($value)) {
|
|
|
|
$value = json_encode($value);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
2025-01-10 08:57:26 -05:00
|
|
|
$key = strtolower($key);
|
2025-01-06 12:20:11 -05:00
|
|
|
SettingModel::updateOrCreate(['name' => $key], ['value' => $value]);
|
|
|
|
$this->cache->forget(self::CACHE_KEY);
|
|
|
|
return true;
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-01-06 12:20:11 -05:00
|
|
|
* 保存配置到数据库.
|
2023-11-17 01:44:01 -05:00
|
|
|
*
|
2025-01-06 12:20:11 -05:00
|
|
|
* @param array $data
|
2023-11-17 01:44:01 -05:00
|
|
|
* @return $this
|
|
|
|
*/
|
2025-01-06 12:20:11 -05:00
|
|
|
public function save(array $data = []): bool
|
2023-11-17 01:44:01 -05:00
|
|
|
{
|
2025-01-06 12:20:11 -05:00
|
|
|
foreach ($data as $key => $value) {
|
|
|
|
$this->set($key, $value);
|
|
|
|
}
|
2023-11-17 01:44:01 -05:00
|
|
|
|
2025-01-06 12:20:11 -05:00
|
|
|
return true;
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2025-01-06 12:20:11 -05:00
|
|
|
* 删除配置信息
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return bool
|
2023-11-17 01:44:01 -05:00
|
|
|
*/
|
2025-01-06 12:20:11 -05:00
|
|
|
public function remove($key): bool
|
2023-11-17 01:44:01 -05:00
|
|
|
{
|
2025-01-06 12:20:11 -05:00
|
|
|
SettingModel::where('name', $key)->delete();
|
|
|
|
$this->cache->forget(self::CACHE_KEY);
|
|
|
|
return true;
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2025-01-06 12:20:11 -05:00
|
|
|
* 获取配置信息.
|
|
|
|
* @return array
|
2023-11-17 01:44:01 -05:00
|
|
|
*/
|
2025-01-06 12:20:11 -05:00
|
|
|
public function fromDatabase(): array
|
2023-11-17 01:44:01 -05:00
|
|
|
{
|
|
|
|
try {
|
2025-01-06 12:20:11 -05:00
|
|
|
return $this->cache->rememberForever(self::CACHE_KEY, function (): array {
|
2025-01-10 08:57:26 -05:00
|
|
|
return array_change_key_case(SettingModel::pluck('value', 'name')->toArray(), CASE_LOWER);
|
2024-04-09 12:51:03 -04:00
|
|
|
});
|
2025-01-06 12:20:11 -05:00
|
|
|
} catch (\Throwable $th) {
|
|
|
|
return [];
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|