Framework/src/commands/generate_openapi.php
Funky Waddle 54303282d7
Some checks failed
CI / PHP ${{ matrix.php }} (8.1) (push) Has been cancelled
CI / PHP ${{ matrix.php }} (8.2) (push) Has been cancelled
CI / PHP ${{ matrix.php }} (8.3) (push) Has been cancelled
Too many things
2026-01-06 11:02:05 -06:00

61 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
use Phred\Console\Command;
use Symfony\Component\Console\Input\InputInterface as Input;
use Symfony\Component\Console\Output\OutputInterface as Output;
use OpenApi\Generator;
return new class extends Command {
protected string $command = 'generate:openapi';
protected string $description = 'Generate OpenAPI JSON documentation from annotations.';
protected array $options = [
'output' => [
'mode' => 'option',
'shortcut' => 'o',
'valueRequired' => true,
'description' => 'Path to save the generated JSON file.',
'default' => 'public/openapi.json',
],
];
public function handle(Input $input, Output $output): int
{
$outputPath = $input->getOption('output');
$scanPaths = [
getcwd() . '/src',
getcwd() . '/modules',
];
// Filter scan paths to only existing directories
$existingPaths = array_filter($scanPaths, 'is_dir');
if (empty($existingPaths)) {
$output->writeln('<error>No source or module directories found to scan.</error>');
return 1;
}
$output->writeln('<info>Scanning for OpenAPI annotations...</info>');
try {
$openapi = Generator::scan($existingPaths);
$json = $openapi->toJson();
$fullOutputPath = getcwd() . '/' . ltrim($outputPath, '/');
$dir = dirname($fullOutputPath);
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
file_put_contents($fullOutputPath, $json);
$output->writeln("<info>OpenAPI documentation generated at: $outputPath</info>");
return 0;
} catch (\Exception $e) {
$output->writeln('<error>OpenAPI Generation failed: ' . $e->getMessage() . '</error>');
return 1;
}
}
};