|
| 1 | +<?php |
| 2 | + |
| 3 | +/* Icinga Notifications Web | (c) 2025 Icinga GmbH | GPLv2 */ |
| 4 | + |
| 5 | +namespace Icinga\Module\Notifications\Clicommands; |
| 6 | + |
| 7 | +use FilesystemIterator; |
| 8 | +use Icinga\Application\Icinga; |
| 9 | +use Icinga\Cli\Command; |
| 10 | +use Icinga\Module\Notifications\Api\OpenApiPreprocessor\AddGlobal401Response; |
| 11 | +use Icinga\Module\Notifications\Common\PsrLogger; |
| 12 | +use OpenApi\Generator; |
| 13 | +use RecursiveDirectoryIterator; |
| 14 | +use RecursiveIteratorIterator; |
| 15 | +use RuntimeException; |
| 16 | +use SplFileInfo; |
| 17 | +use Throwable; |
| 18 | + |
| 19 | +class OpenapiCommand extends Command |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Generate an OpenAPI JSON file from PHP attributes. |
| 23 | + * |
| 24 | + * This command scans a directory for PHP files and generates an OpenAPI JSON file using swagger-php. |
| 25 | + * The paths are relative to the notifications module directory. |
| 26 | + * |
| 27 | + * USAGE: |
| 28 | + * |
| 29 | + * icingacli notifications openapi generate [OPTIONS] |
| 30 | + * |
| 31 | + * OPTIONS |
| 32 | + * |
| 33 | + * --dir <path/> Set the path to the directory to scan for PHP files. |
| 34 | +* Default: /library/Notifications/Api/ |
| 35 | + * |
| 36 | + * --exclude <comma seperated strings> Exclude files matching these strings. Wildcard is `*` |
| 37 | + * |
| 38 | + * --include <comma seperated strings> Include files matching these strings. Wildcard is `*` |
| 39 | + * |
| 40 | + * --output <path/> Set the path to the output file. |
| 41 | + * Default: /doc/api/api-v1-public.json |
| 42 | + * |
| 43 | + * --api-version <version string> Set the API version. |
| 44 | + * Default: v1 |
| 45 | + * If the output path is set the --api-version option is ignored. |
| 46 | + * |
| 47 | + * --oad-version <version string> Set the OpenAPI version. |
| 48 | + * Default: 3.1.0 |
| 49 | + */ |
| 50 | + public function generateAction(): void |
| 51 | + { |
| 52 | + $directoryInNotifications = $this->params->get('dir', '/library/Notifications/Api/'); |
| 53 | + $exclude = $this->params->get('exclude'); |
| 54 | + $include = $this->params->get('include'); |
| 55 | + $outputPath = $this->params->get('output'); |
| 56 | + $apiVersion = $this->params->get('api-version', 'v1'); |
| 57 | + $oadVersion = $this->params->get('oad-version', '3.1.0'); |
| 58 | + |
| 59 | + $notificationsPath = Icinga::app()->getModuleManager()->getModule('notifications')->getBaseDir(); |
| 60 | + $directory = $notificationsPath . $directoryInNotifications; |
| 61 | + |
| 62 | + $baseDirectory = realpath($directory); |
| 63 | + if ($baseDirectory === false || ! is_dir($baseDirectory)) { |
| 64 | + throw new RuntimeException("Invalid directory: {$directory}"); |
| 65 | + } |
| 66 | + |
| 67 | + $exclude = isset($exclude) ? array_map('trim', explode(',', $exclude)) : []; |
| 68 | + $include = isset($include) ? array_map('trim', explode(',', $include)) : []; |
| 69 | + $outputPath = $notificationsPath . ($outputPath ?? '/doc/api/api-' . $apiVersion . '-public.json'); |
| 70 | + |
| 71 | + $files = $this->collectPhpFiles($baseDirectory, $exclude, $include); |
| 72 | + |
| 73 | + echo "→ Scanning directory: $baseDirectory\n"; |
| 74 | + echo "→ Found " . count($files) . " PHP files\n"; |
| 75 | + |
| 76 | + $generator = new Generator(new PsrLogger()); |
| 77 | + $generator->setVersion($oadVersion); |
| 78 | + $generator->getProcessorPipeline()->add(new AddGlobal401Response()); |
| 79 | + |
| 80 | + try { |
| 81 | + $openapi = $generator->generate($files); |
| 82 | + |
| 83 | + $json = $openapi->toJson( |
| 84 | + JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_IGNORE | JSON_PRETTY_PRINT |
| 85 | + ); |
| 86 | + |
| 87 | + $dir = dirname($outputPath); |
| 88 | + if (! is_dir($dir)) { |
| 89 | + mkdir($dir, 0755, true); |
| 90 | + } |
| 91 | + |
| 92 | + file_put_contents($outputPath, $json); |
| 93 | + |
| 94 | + echo "OpenAPI documentation written to: $outputPath\n"; |
| 95 | + } catch (Throwable $e) { |
| 96 | + fwrite(STDERR, "Error generating OpenAPI: " . $e->getMessage() . "\n"); |
| 97 | + exit(1); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Recursively scan a directory for PHP files. |
| 103 | + */ |
| 104 | + protected function collectPhpFiles(string $baseDirectory, array $exclude, array $include): array |
| 105 | + { |
| 106 | + $baseDirectory = rtrim($baseDirectory, '/') . '/'; |
| 107 | + if (! is_dir($baseDirectory)) { |
| 108 | + throw new RuntimeException("Directory $baseDirectory does not exist"); |
| 109 | + } |
| 110 | + if (! is_readable($baseDirectory)) { |
| 111 | + throw new RuntimeException("Directory $baseDirectory is not readable"); |
| 112 | + } |
| 113 | + |
| 114 | + $files = []; |
| 115 | + $iterator = new RecursiveIteratorIterator( |
| 116 | + new RecursiveDirectoryIterator($baseDirectory, FilesystemIterator::SKIP_DOTS) |
| 117 | + ); |
| 118 | + |
| 119 | + /** @var SplFileInfo $file */ |
| 120 | + foreach ($iterator as $file) { |
| 121 | + if (! $file->isFile() || $file->getExtension() !== 'php') { |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + $path = $file->getPathname(); |
| 126 | + |
| 127 | + if ($exclude !== [] && $this->matchesAnyPattern($path, $exclude)) { |
| 128 | + continue; |
| 129 | + } |
| 130 | + |
| 131 | + if ($include !== [] && ! $this->matchesAnyPattern($path, $include)) { |
| 132 | + continue; |
| 133 | + } |
| 134 | + |
| 135 | + $files[] = $path; |
| 136 | + } |
| 137 | + |
| 138 | + if (empty($files)) { |
| 139 | + throw new RuntimeException("No PHP files found in $baseDirectory"); |
| 140 | + } |
| 141 | + |
| 142 | + return $files; |
| 143 | + } |
| 144 | + |
| 145 | + protected function matchesAnyPattern(string $string, array $patterns): bool |
| 146 | + { |
| 147 | + foreach ($patterns as $pattern) { |
| 148 | + // Escape regex special chars except for '*' |
| 149 | + $regex = '/^' . str_replace('\*', '.*', preg_quote($pattern, '/')) . '$/'; |
| 150 | + if (preg_match($regex, $string)) { |
| 151 | + return true; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return false; |
| 156 | + } |
| 157 | +} |
0 commit comments