diff --git a/app/Core/IniFile.php b/app/Core/IniFile.php index 50dff74..d40ee54 100644 --- a/app/Core/IniFile.php +++ b/app/Core/IniFile.php @@ -11,7 +11,6 @@ namespace App\Core; use App\Errors\PlaylistNotFoundException; use Exception; -use Throwable; /** * Класс для работы со списком плейлистов @@ -31,46 +30,42 @@ class IniFile /** * Считывает ini-файл и инициализирует плейлисты * - * @return array - * @throws Exception + * @return void */ - public function load(): array + public function load(): void { $filepath = config_path('playlists.ini'); $ini = parse_ini_file($filepath, true); $this->updatedAt = date('d.m.Y h:i', filemtime($filepath)); + $plsCodes = array_keys($ini); + $cached = array_combine($plsCodes, redis()->mget($plsCodes)); - // сохраняем порядок - foreach (array_keys($ini) as $code) { - try { - $data = @redis()->get($code); - } catch (Throwable) { - $data = false; - } - if ($data === false) { - $raw = $ini[$code]; - $data = [ - 'code' => $code, - 'name' => $raw['name'] ?? "Playlist #$code", - 'description' => $raw['desc'] ?? null, - 'url' => $raw['pls'], - 'source' => $raw['src'] ?? null, - 'content' => null, - 'isOnline' => null, - 'attributes' => [], - 'groups' => [], - 'channels' => [], - 'checkedAt' => null, - ]; - } elseif (!isset($data['attributes'])) { - $data['attributes'] = []; - } + foreach ($cached as $code => $data) { + $raw = $ini[$code]; + $data === false && $data = [ + 'code' => $code, + 'name' => $raw['name'] ?? "Плейлист #$code", + 'description' => $raw['desc'] ?? null, + 'url' => $raw['pls'], + 'source' => $raw['src'] ?? null, + 'content' => null, + 'isOnline' => null, + 'attributes' => [], + 'groups' => [], + 'channels' => [], + 'checkedAt' => null, + ]; + + // приколы golang + $data['attributes'] === null && $data['attributes'] = []; + $data['groups'] === null && $data['groups'] = []; + $data['channels'] === null && $data['channels'] = []; $data['onlinePercent'] = 0; $data['offlinePercent'] = 0; - if ($data['isOnline'] === true && count($data['channels'] ?? []) > 0) { - $data['onlinePercent'] = round($data['onlineCount'] / count($data['channels'] ?? []) * 100); - $data['offlinePercent'] = round($data['offlineCount'] / count($data['channels'] ?? []) * 100); + if ($data['isOnline'] === true && count($data['channels']) > 0) { + $data['onlinePercent'] = round($data['onlineCount'] / count($data['channels']) * 100); + $data['offlinePercent'] = round($data['offlineCount'] / count($data['channels']) * 100); } $data['hasCatchup'] = str_contains($data['content'] ?? '', 'catchup'); @@ -78,7 +73,7 @@ class IniFile || !empty($data['attributes']['x-tvg-url']); $data['tags'] = []; - foreach ($data['channels'] ?? [] as $channel) { + foreach ($data['channels'] as $channel) { $data['tags'] = array_merge($data['tags'], $channel['tags']); } $data['tags'] = array_values(array_unique($data['tags'])); @@ -86,8 +81,6 @@ class IniFile $this->playlists[$code] = $data; } - - return $this->playlists; } /** @@ -98,17 +91,8 @@ class IniFile */ public function getPlaylists(): array { - return $this->playlists ??= $this->load(); - } - - /** - * Возвращает дату обновления ini-файла - * - * @return string - */ - public function updatedAt(): string - { - return $this->updatedAt; + empty($this->playlists) && $this->load(); + return $this->playlists; } /** @@ -121,10 +105,17 @@ class IniFile */ public function getPlaylist(string $code): ?array { - if (empty($this->playlists)) { - $this->load(); - } - + empty($this->playlists) && $this->load(); return $this->playlists[$code] ?? throw new PlaylistNotFoundException($code); } + + /** + * Возвращает дату обновления ini-файла + * + * @return string + */ + public function updatedAt(): string + { + return $this->updatedAt; + } }