[
'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('No source or module directories found to scan.');
return 1;
}
$output->writeln('Scanning for OpenAPI annotations...');
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("OpenAPI documentation generated at: $outputPath");
return 0;
} catch (\Exception $e) {
$output->writeln('OpenAPI Generation failed: ' . $e->getMessage() . '');
return 1;
}
}
};