*/ public function all(): array { $out = []; foreach (self::KEYS as $key) { $out[$key] = $this->get($key); } return $out; } public function get(string $key, ?string $default = null): ?string { if (! $this->tableReady()) { return $default; } $map = $this->cachedMap(); return array_key_exists($key, $map) ? $map[$key] : $default; } /** * @param array $values */ public function putMany(array $values): void { foreach ($values as $key => $value) { if (! in_array($key, self::KEYS, true)) { continue; } $str = $value === null ? null : trim((string) $value); Setting::query()->updateOrCreate( ['key' => $key], ['value' => $str === '' ? null : $str] ); } Cache::forget($this->cacheKey()); $this->applyToConfig(); } public function applyToConfig(): void { if (! $this->tableReady()) { return; } $map = $this->cachedMap(); $bot = $map['telegram.bot_token'] ?? null; if ($bot !== null && $bot !== '') { config([ 'coruna.telegram.bot_token' => $bot, 'nutgram.token' => $bot, ]); } $chat = $map['telegram.owner_chat_id'] ?? null; if ($chat !== null && $chat !== '') { config(['coruna.telegram.owner_chat_id' => $chat]); } $max = $map['channels.max_per_agent'] ?? null; if ($max !== null && $max !== '' && is_numeric($max)) { config(['coruna.channels.max_per_agent' => max(1, (int) $max)]); } if (array_key_exists('channels.domains', $map) && $map['channels.domains'] !== null) { $domains = self::parseDomains($map['channels.domains']); // Settings override channel/deployment hosts only; reporting stays CORUNA_LAB_AMIDN_DOMAINS. config([ 'coruna.channel_domains' => $domains, 'coruna.deployment_domains' => $domains, ]); } } /** Effective values for the settings form (DB overrides env). */ public function effectiveForForm(): array { $domains = config('coruna.channel_domains', []); return [ 'telegram.bot_token' => (string) (config('coruna.telegram.bot_token') ?: ''), 'telegram.owner_chat_id' => (string) (config('coruna.telegram.owner_chat_id') ?: ''), 'channels.max_per_agent' => (string) (int) config('coruna.channels.max_per_agent', 5), 'channels.domains' => is_array($domains) ? implode("\n", $domains) : '', ]; } /** @return list */ public static function parseDomains(?string $raw): array { if ($raw === null || trim($raw) === '') { return []; } $parts = preg_split('/[\s,;]+/', trim($raw)) ?: []; return array_values(array_filter(array_map(static function ($d) { $d = trim((string) $d); $d = preg_replace('#^https?://#i', '', $d) ?? $d; $d = rtrim($d, '/'); return $d; }, $parts))); } /** @return array */ private function cachedMap(): array { return Cache::remember($this->cacheKey(), 300, function () { return Setting::query()->pluck('value', 'key')->all(); }); } private function cacheKey(): string { return 'coruna.settings.map'; } private function tableReady(): bool { try { return Schema::hasTable('settings'); } catch (\Throwable) { return false; } } }