Разделение по отдельным репозиториям

This commit is contained in:
2025-03-03 16:31:32 +08:00
commit a1f5154ce8
44 changed files with 4362 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace App\Controllers;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Random\RandomException;
/**
*
*/
class ApiController extends BasicController
{
/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
* @throws RandomException
*/
public function json(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
$code = $request->getAttributes()['code'];
$playlist = $this->getPlaylist($code, true);
$playlist->fetchContent();
$playlist->parse();
$json = json_encode($playlist->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$response->getBody()->write($json);
return $response
->withHeader('Content-Type', 'application/json')
->withHeader('Content-Length', strlen($json));
}
}

View File

@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace App\Controllers;
use App\Core\Playlist;
use App\Errors\PlaylistNotFoundException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Views\Twig;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
/**
*
*/
class BasicController
{
/**
* Отправляет сообщение о том, что метод не найден с кодом страницы 404
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function notFound(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
$response->withStatus(404);
$this->view($request, $response, 'notfound.twig');
return $response;
}
/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param string $template
* @param array $data
* @return ResponseInterface
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
protected function view(
ServerRequestInterface $request,
ResponseInterface $response,
string $template,
array $data = [],
): ResponseInterface {
$view = Twig::fromRequest($request);
return $view->render($response, $template, $data);
}
}

View File

@@ -0,0 +1,128 @@
<?php
declare(strict_types=1);
namespace App\Controllers;
use App\Core\ChannelLogo;
use App\Errors\PlaylistNotFoundException;
use Exception;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
/**
*
*/
class WebController extends BasicController
{
/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
* @throws Exception
*/
public function home(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
ini()->load();
$playlists = ini()->playlists(false);
$count = count($playlists);
$page = (int)($request->getAttributes()['page'] ?? $request->getQueryParams()['page'] ?? 1);
$pageSize = config('app.page_size');
$pageCount = ceil($count / $pageSize);
$offset = max(0, ($page - 1) * $pageSize);
$list = array_slice($playlists, $offset, $pageSize, true);
return $this->view($request, $response, 'list.twig', [
'updated_at' => ini()->updatedAt(),
'playlists' => $list,
'count' => $count,
'pageCount' => $pageCount,
'pageCurrent' => $page,
]);
}
/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function faq(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
return $this->view($request, $response, 'faq.twig');
}
/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
*/
public function redirect(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
ini()->load();
$code = $request->getAttributes()['code'];
try {
$playlist = ini()->getPlaylist($code);
return $response->withHeader('Location', $playlist->pls);
} catch (PlaylistNotFoundException) {
return $this->notFound($request, $response);
}
}
/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
* @throws \Random\RandomException
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function details(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
ini()->load();
$code = $request->getAttributes()['code'];
try {
$playlist = ini()->getPlaylist($code);
$response->withHeader('Location', $playlist->pls);
} catch (PlaylistNotFoundException) {
return $this->notFound($request, $response);
}
$playlist->fetchContent();
$playlist->parse();
return $this->view($request, $response, 'details.twig', $playlist->toArray());
}
/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
*/
public function logo(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
$input = $request->getQueryParams()['url'] ?? null;
$logo = new ChannelLogo($input);
$logo->readFile() || $logo->fetch();
$logo->size() === 0 && $logo->setDefault();
$logo->store();
$body = $logo->raw();
$size = $logo->size();
$mime = $logo->mimeType();
$response->getBody()->write($body);
return $response->withHeader('Content-Type', $mime)
->withHeader('Content-Length', $size);
}
}