All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m5s
62 lines
1.4 KiB
PHP
62 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Core\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class SystemSetting extends Model
|
|
{
|
|
protected $fillable = [
|
|
'group',
|
|
'key',
|
|
'value',
|
|
'type',
|
|
'description',
|
|
];
|
|
|
|
/**
|
|
* 同請求內的記憶體快取,避免重複查詢 DB
|
|
* PHP 請求結束後自動釋放,無需額外處理失效
|
|
*/
|
|
protected static array $cache = [];
|
|
|
|
/**
|
|
* 取得特定設定值(含記憶體快取)
|
|
*/
|
|
public static function getVal(string $key, $default = null)
|
|
{
|
|
if (array_key_exists($key, static::$cache)) {
|
|
return static::$cache[$key];
|
|
}
|
|
|
|
$setting = self::where('key', $key)->first();
|
|
|
|
if (!$setting) {
|
|
static::$cache[$key] = $default;
|
|
return $default;
|
|
}
|
|
|
|
$value = $setting->value;
|
|
|
|
// 根據 type 進行類別轉換
|
|
$resolved = match ($setting->type) {
|
|
'integer', 'number' => (int) $value,
|
|
'boolean', 'bool' => filter_var($value, FILTER_VALIDATE_BOOLEAN),
|
|
'json', 'array' => json_decode($value, true),
|
|
default => $value,
|
|
};
|
|
|
|
static::$cache[$key] = $resolved;
|
|
|
|
return $resolved;
|
|
}
|
|
|
|
/**
|
|
* 清除記憶體快取(儲存設定後應呼叫)
|
|
*/
|
|
public static function clearCache(): void
|
|
{
|
|
static::$cache = [];
|
|
}
|
|
}
|