From 72649c95228b435b7acfb8bab3b7ddf69f58ef06 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sat, 22 Jul 2023 22:47:55 +0200 Subject: [PATCH] API: Load api info from yaml --- docker/Dockerfile | 1 + resources/api/openapi.yml | 2 +- src/Controllers/Api/IndexController.php | 20 +++++++++++++++----- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 8af6ae34..616b1413 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -26,6 +26,7 @@ COPY config/ /app/config COPY db/ /app/db COPY includes/ /app/includes COPY public/ /app/public +COPY resources/api /app/resources/api COPY resources/views /app/resources/views COPY src/ /app/src COPY storage/ /app/storage diff --git a/resources/api/openapi.yml b/resources/api/openapi.yml index 1f9eeedf..ffb6466c 100644 --- a/resources/api/openapi.yml +++ b/resources/api/openapi.yml @@ -4,7 +4,7 @@ info: version: 0.0.1-beta title: Engelsystem description: | - This API description is as stable as a **beta** version might be. + This API is as stable as a **beta** version might be. It could burst into flames and morph into a monster at any second! (But we try to keep it somewhat consistent, at least during events). contact: diff --git a/src/Controllers/Api/IndexController.php b/src/Controllers/Api/IndexController.php index 61a1b7ad..a52f9536 100644 --- a/src/Controllers/Api/IndexController.php +++ b/src/Controllers/Api/IndexController.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Engelsystem\Controllers\Api; use Engelsystem\Http\Response; +use League\OpenAPIValidation\PSR7\ValidatorBuilder as OpenApiValidatorBuilder; class IndexController extends ApiController { @@ -25,13 +26,22 @@ class IndexController extends ApiController public function indexV0(): Response { + $openApiDefinition = app()->get('path.resources.api') . '/openapi.yml'; + $schema = (new OpenApiValidatorBuilder()) + ->fromYamlFile($openApiDefinition) + ->getResponseValidator() + ->getSchema(); + $info = $schema->info; + $paths = []; + foreach ($schema->paths->getIterator() as $path => $item) { + $paths[] = $path; + } + return $this->response ->withContent(json_encode([ - 'version' => '0.0.1-beta', - 'description' => 'Beta API, might break any second.', - 'paths' => [ - '/news', - ], + 'version' => $info->version, + 'description' => $info->description, + 'paths' => $paths, ])); }