You've already forked Php-Template
feat: add Swagger integration with OpenAPI annotations and endpoints
All checks were successful
🧪✨ Tests Workflow / 📝 ✨ Code Lint (push) Successful in 4m0s
🧪✨ Tests Workflow / 🛡️ 🔒 Library Audit (push) Successful in 4m11s
🧪✨ Tests Workflow / 🛡️ 🔒 License Check (push) Successful in 4m22s
🧪✨ Tests Workflow / 🧪 ✨ Database Migrations (push) Successful in 4m42s
🧪✨ Tests Workflow / 🐙 🔍 Code Sniffer (push) Successful in 4m27s
🧪✨ Tests Workflow / 🧪 ✅ Unit Tests (push) Successful in 2m55s
All checks were successful
🧪✨ Tests Workflow / 📝 ✨ Code Lint (push) Successful in 4m0s
🧪✨ Tests Workflow / 🛡️ 🔒 Library Audit (push) Successful in 4m11s
🧪✨ Tests Workflow / 🛡️ 🔒 License Check (push) Successful in 4m22s
🧪✨ Tests Workflow / 🧪 ✨ Database Migrations (push) Successful in 4m42s
🧪✨ Tests Workflow / 🐙 🔍 Code Sniffer (push) Successful in 4m27s
🧪✨ Tests Workflow / 🧪 ✅ Unit Tests (push) Successful in 2m55s
This commit is contained in:
34
src/Controllers/SwaggerController.php
Normal file
34
src/Controllers/SwaggerController.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Controllers;
|
||||
|
||||
use Nyholm\Psr7\Response;
|
||||
use Nyholm\Psr7\ServerRequest;
|
||||
use OpenApi\Generator;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
class SwaggerController extends Controller
|
||||
{
|
||||
public function get(ServerRequest $request): ResponseInterface
|
||||
{
|
||||
$openapi = new Generator()->generate([
|
||||
__DIR__ . '/../Controllers',
|
||||
__DIR__ . '/../Models',
|
||||
]);
|
||||
|
||||
$response = new Response();
|
||||
|
||||
if (
|
||||
$request->getHeaderLine('Accept') === 'application/json' ||
|
||||
str_contains($request->getUri()->getPath(), '.json')
|
||||
) {
|
||||
$response->getBody()->write($openapi->toJson());
|
||||
return $response->withHeader('Content-Type', 'application/json');
|
||||
}
|
||||
|
||||
$response->getBody()->write($openapi->toYaml());
|
||||
return $response->withHeader('Content-Type', 'application/x-yaml');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user