basePath = dirname(dirname(dirname(dirname(__DIR__)))); } /** * @OA\Get( * path="/api/docs", * summary="API Documentation", * tags={"Documentation"}, * @OA\Response( * response=200, * description="Returns the Swagger UI interface" * ) * ) */ public function showDocs(Request $request, Response $response): Response { $swaggerUiPath = __DIR__ . '/../../../vendor/swagger-api/swagger-ui/dist'; if (!file_exists($swaggerUiPath)) { return $this->error($response, 'Swagger UI not found. Please run: composer require swagger-api/swagger-ui', 404); } // Serve the Swagger UI $html = file_get_contents($swaggerUiPath . '/index.html'); // Update the URL to point to our OpenAPI JSON endpoint $html = str_replace( 'url: "https://petstore.swagger.io/v2/swagger.json"', 'urls: [ {url: "/api-docs.json", name: "Media Library API"} ], "dom_id": "#swagger-ui", deepLinking: true, presets: [ SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset ], layout: "StandaloneLayout",', $html ); $response->getBody()->write($html); return $response->withHeader('Content-Type', 'text/html'); } /** * @OA\Get( * path="/api-docs.json", * summary="OpenAPI Specification", * tags={"Documentation"}, * @OA\Response( * response=200, * description="Returns the OpenAPI specification" * ) * ) */ public function getOpenApiSpec(Request $request, Response $response): Response { $openapi = \OpenApi\Generator::scan([ __DIR__ . '/../../../app', __DIR__ . '/../../../routes', __DIR__ . '/../../../src' ], [ 'exclude' => [ __DIR__ . '/../../../database/migrations', __DIR__ . '/../../../vendor', __DIR__ . '/../../../tests' ] ]); $response->getBody()->write(json_encode($openapi)); return $response->withHeader('Content-Type', 'application/json'); } }