Initial commit: Pireon Admin — headless admin panel and public API
Laravel 13 + Filament 5 admin panel and REST API for a non-profit developer collective, backed by the Pireon OAuth identity server. The public site is a separate, server-rendered application consuming /api/public/v1. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
68
app/Models/Setting.php
Normal file
68
app/Models/Setting.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
/**
|
||||
* A single runtime setting.
|
||||
*
|
||||
* Reads go through the cache, because some settings — maintenance mode above
|
||||
* all — are consulted on every public request. Writes clear that entry, so a
|
||||
* change in the panel takes effect immediately rather than after a TTL.
|
||||
*/
|
||||
#[Fillable(['key', 'value'])]
|
||||
class Setting extends Model
|
||||
{
|
||||
public $incrementing = false;
|
||||
|
||||
protected $primaryKey = 'key';
|
||||
|
||||
protected $keyType = 'string';
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return ['value' => 'array'];
|
||||
}
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saved(fn (Setting $setting) => Cache::forget(self::cacheKey($setting->key)));
|
||||
static::deleted(fn (Setting $setting) => Cache::forget(self::cacheKey($setting->key)));
|
||||
}
|
||||
|
||||
public static function get(string $key, mixed $default = null): mixed
|
||||
{
|
||||
$value = Cache::rememberForever(
|
||||
self::cacheKey($key),
|
||||
// Wrapped in an array so a legitimately null value is cached as
|
||||
// "known to be null" rather than re-queried on every request.
|
||||
fn () => ['v' => self::query()->find($key)?->value],
|
||||
);
|
||||
|
||||
return $value['v'] ?? $default;
|
||||
}
|
||||
|
||||
public static function set(string $key, mixed $value): void
|
||||
{
|
||||
self::query()->updateOrCreate(['key' => $key], ['value' => $value]);
|
||||
|
||||
Cache::forget(self::cacheKey($key));
|
||||
}
|
||||
|
||||
public static function forget(string $key): void
|
||||
{
|
||||
self::query()->whereKey($key)->delete();
|
||||
|
||||
Cache::forget(self::cacheKey($key));
|
||||
}
|
||||
|
||||
protected static function cacheKey(string $key): string
|
||||
{
|
||||
return "settings:{$key}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user