diff --git a/src/API/Backends/Create.php b/src/API/Backends/Create.php deleted file mode 100644 index b87dd080..00000000 --- a/src/API/Backends/Create.php +++ /dev/null @@ -1,21 +0,0 @@ -getUri()->withHost('')->withPort(0)->withScheme(''); + $urlPath = $request->getUri()->getPath(); + + $response = [ + 'data' => [], + 'links' => [ + 'self' => (string)$apiUrl, + ], + ]; + + foreach (self::getBackends(blacklist: true) as $backend) { + $backend = array_filter( + $backend, + fn($key) => false === in_array($key, ['options', 'webhook'], true), + ARRAY_FILTER_USE_KEY + ); + + $backend['links'] = [ + 'self' => (string)$apiUrl->withPath($urlPath . '/' . $backend['name']), + ]; + + $response['data'][] = $backend; + } + + return api_response($response, HTTP_STATUS::HTTP_OK, []); + } + + public static function getBackends(string|null $name = null, bool $blacklist = false): array + { + $backends = []; + foreach (Config::get('servers', []) as $backendName => $backend) { + $backend = ['name' => $backendName, ...$backend]; + + if (true === $blacklist) { + foreach (self::BLACK_LIST as $hideValue) { + if (true === ag_exists($backend, $hideValue)) { + $backend = ag_set($backend, $hideValue, '__hidden__'); + } + } + } + + if (null !== ag($backend, 'import.lastSync')) { + $backend = ag_set($backend, 'import.lastSync', makeDate(ag($backend, 'import.lastSync'))); + } + + if (null !== ag($backend, 'export.lastSync')) { + $backend = ag_set($backend, 'export.lastSync', makeDate(ag($backend, 'export.lastSync'))); + } + + $backends[] = $backend; + } + + if (null !== $name) { + return array_filter($backends, fn($backend) => $backend['name'] === $name); + } + return $backends; + } +} diff --git a/src/API/Backends/View.php b/src/API/Backends/View.php new file mode 100644 index 00000000..6d2a3ca3 --- /dev/null +++ b/src/API/Backends/View.php @@ -0,0 +1,42 @@ +getUri()->withHost('')->withPort(0)->withScheme(''); + $data = array_pop($data); + + $response = [ + ...$data, + 'links' => [ + 'self' => (string)$apiUrl, + 'list' => (string)$apiUrl->withPath(parseConfigValue(self::URL)), + ], + ]; + + return api_response($response, HTTP_STATUS::HTTP_OK, []); + } + +} diff --git a/src/API/History/Index.php b/src/API/History/Index.php index 177ca9d0..007ce51c 100644 --- a/src/API/History/Index.php +++ b/src/API/History/Index.php @@ -267,7 +267,6 @@ final class Index ); $response = [ - '@self' => (string)$getUri, 'paging' => [ 'total' => (int)$total, 'perpage' => $perpage, @@ -276,15 +275,16 @@ final class Index 'next_page' => $page < @ceil($total / $perpage) ? $page + 1 : null, 'prev_page' => !empty($total) && $page > 1 ? $page - 1 : null, 'last_page' => @ceil($total / $perpage), - 'urls' => [ - 'first_url' => $firstUrl, - 'next_url' => $nextUrl, - 'prev_url' => $prevUrl, - 'last_url' => $lastUrl, - ], ], 'filters' => $filters, 'data' => [], + 'links' => [ + 'self' => (string)$getUri, + 'first_url' => $firstUrl, + 'next_url' => $nextUrl, + 'prev_url' => $prevUrl, + 'last_url' => $lastUrl, + ], ]; while ($row = $stmt->fetch()) { @@ -302,11 +302,16 @@ final class Index unset($item[iState::COLUMN_EXTRA]); } - $response['data'][] = [ - '@self' => (string)(new Uri())->withPath( + $item = [ + ...$item, + 'links' => [ + 'self' => (string)(new Uri())->withPath( rtrim($request->getUri()->getPath(), '/') . '/' . $entity->id )->withQuery(http_build_query($currentQuery)), - ] + $item; + ], + ]; + + $response['data'][] = $item; } return api_response($response, HTTP_STATUS::HTTP_OK, []); diff --git a/src/API/History/View.php b/src/API/History/View.php index eac2115c..a490110c 100644 --- a/src/API/History/View.php +++ b/src/API/History/View.php @@ -31,11 +31,21 @@ final readonly class View return api_error('Not found', HTTP_STATUS::HTTP_NOT_FOUND); } + $apiUrl = $request->getUri()->withHost('')->withPort(0)->withScheme(''); + $item = array_pop($item)->getAll(); $item[iState::COLUMN_WATCHED] = $entity->isWatched(); $item[iState::COLUMN_UPDATED] = makeDate($entity->updated); + $item = [ + ...$item, + 'links' => [ + 'self' => (string)$apiUrl, + 'list' => (string)$apiUrl->withPath(parseConfigValue(Index::URL)), + ], + ]; + return api_response($item, HTTP_STATUS::HTTP_OK, []); } } diff --git a/src/API/System/Env.php b/src/API/System/Env.php index a6199c35..505ea69c 100644 --- a/src/API/System/Env.php +++ b/src/API/System/Env.php @@ -27,8 +27,10 @@ final class Env public function __invoke(ServerRequestInterface $request, array $args = []): ResponseInterface { $response = [ - '@self' => (string)$request->getUri()->withHost('')->withPort(0)->withScheme(''), 'data' => [], + 'links' => [ + 'self' => (string)$request->getUri()->withHost('')->withPort(0)->withScheme(''), + ], ]; foreach ($_ENV as $key => $val) { diff --git a/src/API/Tasks/Index.php b/src/API/Tasks/Index.php index 9de4e871..3b3ff344 100644 --- a/src/API/Tasks/Index.php +++ b/src/API/Tasks/Index.php @@ -17,22 +17,28 @@ final class Index public function __invoke(ServerRequestInterface $request, array $args = []): ResponseInterface { - $response = [ - 'data' => [], - ]; - $apiUrl = $request->getUri()->withHost('')->withPort(0)->withScheme(''); $urlPath = rtrim($request->getUri()->getPath(), '/'); + $response = [ + 'data' => [], + 'links' => [ + 'self' => (string)$apiUrl, + ], + ]; + foreach (TasksCommand::getTasks() as $task) { - $response['data'][] = [ - '@self' => (string)$apiUrl->withPath($urlPath . '/' . ag($task, 'name')), - ...array_filter( - self::formatTask($task), - fn($k) => false === in_array($k, ['command', 'args']), - ARRAY_FILTER_USE_KEY - ) + $task = array_filter( + self::formatTask($task), + fn($k) => false === in_array($k, ['command', 'args']), + ARRAY_FILTER_USE_KEY + ); + + $task['links'] = [ + 'self' => (string)$apiUrl->withPath($urlPath . '/' . ag($task, 'name')), ]; + + $response['data'][] = $task; } return api_response($response, HTTP_STATUS::HTTP_OK, []); diff --git a/src/API/Tasks/View.php b/src/API/Tasks/View.php index dcae19c0..7663aa7d 100644 --- a/src/API/Tasks/View.php +++ b/src/API/Tasks/View.php @@ -19,12 +19,22 @@ final class View return api_error('No id was given.', HTTP_STATUS::HTTP_BAD_REQUEST); } + $apiUrl = $request->getUri()->withHost('')->withPort(0)->withScheme(''); + $task = TasksCommand::getTasks($id); if (empty($task)) { return api_error('Task not found.', HTTP_STATUS::HTTP_NOT_FOUND); } - return api_response(Index::formatTask($task), HTTP_STATUS::HTTP_OK); + $response = [ + ...Index::formatTask($task), + 'links' => [ + 'self' => (string)$apiUrl, + 'list' => (string)$apiUrl->withPath(parseConfigValue(Index::URL)), + ], + ]; + + return api_response($response, HTTP_STATUS::HTTP_OK); } } diff --git a/src/Libs/Extends/RouterStrategy.php b/src/Libs/Extends/RouterStrategy.php index a2de7f52..980b1dc7 100644 --- a/src/Libs/Extends/RouterStrategy.php +++ b/src/Libs/Extends/RouterStrategy.php @@ -4,18 +4,8 @@ declare(strict_types=1); namespace App\Libs\Extends; -use App\Libs\HTTP_STATUS; use League\Route\Strategy\ApplicationStrategy; -use League\Route\Strategy\OptionsHandlerInterface; -use Psr\Http\Message\ResponseInterface; -class RouterStrategy extends ApplicationStrategy implements OptionsHandlerInterface +class RouterStrategy extends ApplicationStrategy { - public function getOptionsCallable(array $methods): callable - { - return fn(): ResponseInterface => api_response(body: [], status: HTTP_STATUS::HTTP_NO_CONTENT, headers: [ - 'Allow' => implode(', ', $methods), - 'Access-Control-Allow-Methods' => implode(', ', $methods), - ]); - } }