diff --git a/app/Controllers/WebController.php b/app/Controllers/WebController.php index 0d2a956..66d9dd5 100644 --- a/app/Controllers/WebController.php +++ b/app/Controllers/WebController.php @@ -36,28 +36,24 @@ class WebController extends BasicController */ public function home(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface { - $playlists = ini()->getPlaylists(); - - $count = count($playlists); - $onlineCount = count(array_filter($playlists, static fn (array $playlist) => $playlist['isOnline'] === true)); - $uncheckedCount = count(array_filter($playlists, static fn (array $playlist) => $playlist['isOnline'] === null)); - $offlineCount = $count - $onlineCount - $uncheckedCount; + $ini = ini()->load(); + $count = count($ini); $pageSize = config('app.page_size'); if ($pageSize > 0) { $pageCurrent = (int)($request->getAttributes()['page'] ?? $request->getQueryParams()['page'] ?? 1); $pageCount = ceil($count / $pageSize); $offset = max(0, ($pageCurrent - 1) * $pageSize); - $playlists = array_slice($playlists, $offset, $pageSize, true); + $ini = array_slice($ini, $offset, $pageSize, true); + $playlists = ini()->getPlaylists(array_keys($ini)); + } else { + $playlists = ini()->getPlaylists(); } return $this->view($request, $response, 'list.twig', [ 'updatedAt' => ini()->updatedAt(), 'playlists' => $playlists, 'count' => $count, - 'onlineCount' => $onlineCount, - 'uncheckedCount' => $uncheckedCount, - 'offlineCount' => $offlineCount, 'pageCount' => $pageCount ?? 1, 'pageCurrent' => $pageCurrent ?? 1, ]); diff --git a/app/Core/IniFile.php b/app/Core/IniFile.php index 93f934a..2f1187b 100644 --- a/app/Core/IniFile.php +++ b/app/Core/IniFile.php @@ -30,13 +30,15 @@ class IniFile /** * Считывает ini-файл и инициализирует плейлисты * - * @return void + * @return array */ - public function load(): void + public function load(): array { $filepath = config_path('playlists.ini'); $this->playlists = parse_ini_file($filepath, true); $this->updatedAt = date('d.m.Y h:i', filemtime($filepath)); + + return $this->playlists; } /** @@ -45,16 +47,17 @@ class IniFile * @return array[] * @throws Exception */ - public function getPlaylists(): array + public function getPlaylists(array $plsCodes = []): array { + $playlists = []; empty($this->playlists) && $this->load(); - $plsCodes = array_keys($this->playlists); + empty($plsCodes) && $plsCodes = array_keys($this->playlists); $cached = array_combine($plsCodes, redis()->mget($plsCodes)); foreach ($cached as $code => $data) { - $this->playlists[$code] = $this->initPlaylist($code, $data); + $playlists[$code] = $this->initPlaylist($code, $data); } - return $this->playlists; + return $playlists; } /** @@ -69,9 +72,7 @@ class IniFile { empty($this->playlists) && $this->load(); $data = redis()->get($code); - $pls = $this->initPlaylist($code, $data); - - return $pls ?? throw new PlaylistNotFoundException($code); + return $this->initPlaylist($code, $data); } /** @@ -90,11 +91,13 @@ class IniFile * @param string $code * @param array|false $data * @return array + * @throws PlaylistNotFoundException */ protected function initPlaylist(string $code, array|false $data): array { if ($data === false) { $raw = $this->playlists[$code]; + empty($raw) && throw new PlaylistNotFoundException($code); $data === false && $data = [ 'code' => $code, 'name' => $raw['name'] ?? "Плейлист #$code", diff --git a/views/list.twig b/views/list.twig index 7440719..4a5b4e4 100644 --- a/views/list.twig +++ b/views/list.twig @@ -31,11 +31,6 @@