23 lines
863 B
PHP
23 lines
863 B
PHP
<?php
|
|
|
|
namespace Modules\Settings\Services;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Modules\Settings\Models\Setting;
|
|
|
|
final class SettingsRepository
|
|
{
|
|
public function get(string $key, mixed $default = null, string $group = 'general'): mixed
|
|
{
|
|
return Cache::rememberForever("settings.$group.$key", fn () => Setting::query()->where(compact('group', 'key'))->value('value') ?? $default);
|
|
}
|
|
|
|
public function put(string $key, mixed $value, string $group = 'general', string $type = 'string', bool $public = false): Setting
|
|
{
|
|
$setting = Setting::query()->updateOrCreate(compact('group', 'key'), ['value' => is_scalar($value) || $value === null ? (string) $value : json_encode($value, JSON_THROW_ON_ERROR), 'type' => $type, 'is_public' => $public]);
|
|
Cache::forget("settings.$group.$key");
|
|
|
|
return $setting;
|
|
}
|
|
}
|