mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-13 23:56:46 +02:00
- Added JWT authentication support in AuthService and JwtService. - Implemented token generation and refresh mechanisms. - Updated ApiAuthMiddleware to handle authentication for protected routes. - Created ApiController and BaseApiController for standardized API responses. - Developed MediaController for managing media items with pagination and search capabilities. - Introduced DocsController for serving API documentation via Swagger UI. - Added routes for API documentation and media management. - Improved error handling and response formatting across API endpoints. - Updated composer.json to include necessary JWT and Swagger UI dependencies.
108 lines
3.0 KiB
PHP
108 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Api;
|
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
use OpenApi\Annotations as OA;
|
|
|
|
/**
|
|
* @OA\Info(
|
|
* title="Media Library API",
|
|
* version="1.0.0",
|
|
* description="API documentation for the Media Library application"
|
|
* )
|
|
* @OA\Server(
|
|
* url="/api",
|
|
* description="API Server"
|
|
* )
|
|
* @OA\SecurityScheme(
|
|
* securityScheme="bearerAuth",
|
|
* type="http",
|
|
* scheme="bearer",
|
|
* bearerFormat="JWT"
|
|
* )
|
|
*/
|
|
use App\Controllers\Api\ApiController;
|
|
|
|
class DocsController extends ApiController
|
|
{
|
|
private $basePath;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->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');
|
|
}
|
|
}
|