Files
casadoc/public/userportal/api/openapi-gen.php
T
2026-07-27 21:04:07 +03:00

32 lines
1.0 KiB
PHP

<?php
// Generate the OpenAPI spec from @OA annotations in this directory.
//
// swagger-php is a build tool and is NOT a project dependency. Install it once, e.g.:
// composer require --working-dir=/tmp/swg zircote/swagger-php:^4.7 doctrine/annotations
// SWAGGER_AUTOLOAD=/tmp/swg/vendor/autoload.php php public/userportal/api/openapi-gen.php
//
// TokenAnalyser is used so docblock annotations in procedural (class-less) files are read.
$autoload = getenv('SWAGGER_AUTOLOAD') ?: __DIR__ . '/../../../vendor/autoload.php';
require $autoload;
use OpenApi\Generator;
use OpenApi\Analysers\TokenAnalyser;
if (!class_exists(Generator::class)) {
fwrite(STDERR, "swagger-php is not installed. See the file header.\n");
exit(1);
}
$generator = new Generator();
if (class_exists(TokenAnalyser::class)) {
$generator->setAnalyser(new TokenAnalyser());
}
$openapi = $generator->generate([__DIR__]);
$out = __DIR__ . '/openapi.yaml';
file_put_contents($out, $openapi->toYaml());
echo "OpenAPI written to: {$out}\n";