#!/usr/bin/env php
<?php
// bin/generate-openapi
declare(strict_types=1);
use apivalk\apivalk\Documentation\OpenAPI\Object\ComponentsObject;
use apivalk\apivalk\Documentation\OpenAPI\Object\InfoObject;
use apivalk\apivalk\Documentation\OpenAPI\Object\SecuritySchemeObject;
use apivalk\apivalk\Documentation\OpenAPI\Object\ServerObject;
use apivalk\apivalk\Documentation\OpenAPI\OpenAPIGenerator;
$rootDir = \dirname(__DIR__);
require $rootDir . '/vendor/autoload.php';
$apivalk = App\Bootstrap\ApivalkBootstrap::create($rootDir);
// 1. Describe the API
$info = new InfoObject(
'Example API',
'1.0.0',
'Example',
'Internal reference API powered by Apivalk'
);
// 2. Declare security schemes so Swagger UI knows how to authorize
$components = new ComponentsObject();
$components->setSecuritySchemes([
new SecuritySchemeObject(
'http', // type
'BearerAuth', // name — must match RouteAuthorization('BearerAuth', ...)
'JWT Bearer',
'header',
'bearer',
'JWT',
null,
null
),
]);
// 3. List the servers clients can use
$servers = [
new ServerObject('https://api.example.com', 'Production'),
new ServerObject('http://localhost:8080', 'Local'),
];
// 4. Generate
$generator = new OpenAPIGenerator($apivalk, $info, $servers, $components);
$json = $generator->generate('json');
// 5. Write to disk
$outputPath = $rootDir . '/public/openapi.json';
\file_put_contents($outputPath, $json);
\fwrite(\STDERR, \sprintf("Wrote %s (%d bytes)\n", $outputPath, \strlen($json)));