04a2034975
- storefront 全站改为极简黑白配色:直角、细线分隔、卡片 hover 浅底、深色模式(跟随系统+手动切换) - 抽出 storefront/partials/card 复用卡片 - 一并提交此前暂存的客户端注册 API、站点设置、后台页面与测试 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
86 lines
1.9 KiB
PHP
86 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\StoreSetting;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use RuntimeException;
|
|
use Throwable;
|
|
|
|
class StoreSettings
|
|
{
|
|
private ?array $resolved = null;
|
|
|
|
public function all(): array
|
|
{
|
|
if ($this->resolved !== null) {
|
|
return $this->resolved;
|
|
}
|
|
|
|
$defaults = $this->defaults();
|
|
|
|
if (!$this->tableExists()) {
|
|
return $this->resolved = $defaults;
|
|
}
|
|
|
|
$stored = StoreSetting::query()
|
|
->pluck('value', 'key')
|
|
->map(fn ($value) => (string) $value)
|
|
->all();
|
|
|
|
return $this->resolved = array_replace($defaults, $stored);
|
|
}
|
|
|
|
public function get(string $key, mixed $default = null): mixed
|
|
{
|
|
$settings = $this->all();
|
|
|
|
return $settings[$key] ?? $default;
|
|
}
|
|
|
|
public function defaults(): array
|
|
{
|
|
return (array) config('store.settings_defaults', []);
|
|
}
|
|
|
|
public function keys(): array
|
|
{
|
|
return array_keys($this->defaults());
|
|
}
|
|
|
|
public function setMany(array $values): void
|
|
{
|
|
if (!$this->tableExists()) {
|
|
throw new RuntimeException('store_settings table is missing. Please run php artisan migrate.');
|
|
}
|
|
|
|
$now = now();
|
|
$rows = collect($values)
|
|
->only($this->keys())
|
|
->map(fn ($value, $key) => [
|
|
'key' => $key,
|
|
'value' => (string) $value,
|
|
'created_at' => $now,
|
|
'updated_at' => $now,
|
|
])
|
|
->values()
|
|
->all();
|
|
|
|
if ($rows === []) {
|
|
return;
|
|
}
|
|
|
|
StoreSetting::query()->upsert($rows, ['key'], ['value', 'updated_at']);
|
|
$this->resolved = null;
|
|
}
|
|
|
|
private function tableExists(): bool
|
|
{
|
|
try {
|
|
return Schema::hasTable('store_settings');
|
|
} catch (Throwable) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|