|
10 | 10 | namespace Piwik\Plugins\OpenApiDocs; |
11 | 11 |
|
12 | 12 | use Piwik\Piwik; |
| 13 | +use Piwik\Plugin\Manager; |
13 | 14 | use Piwik\Plugins\OpenApiDocs\Specs\SpecGenerator; |
14 | 15 |
|
15 | 16 | /** |
16 | 17 | * API for plugin OpenApiDocs |
17 | 18 | * |
| 19 | + * Exposes endpoints to fetch pre-generated OpenAPI specs or generate plugin-specific |
| 20 | + * OpenAPI docs on demand. |
| 21 | + * |
18 | 22 | * @method static \Piwik\Plugins\OpenApiDocs\API getInstance() |
19 | 23 | */ |
20 | 24 | class API extends \Piwik\Plugin\API |
21 | 25 | { |
| 26 | + /** |
| 27 | + * Get the pre-generated single OpenAPI spec file if it exists. This endpoint only reads |
| 28 | + * the generated JSON file and does not trigger spec generation. |
| 29 | + * |
| 30 | + * /index.php?module=API&method=OpenApiDocs.getMatomoOpenApiSpec |
| 31 | + * |
| 32 | + * @return array<string, mixed> The decoded OpenAPI specification payload. |
| 33 | + * @throws \Exception If the file is missing, unreadable, or contains invalid JSON. |
| 34 | + */ |
| 35 | + public function getMatomoOpenApiSpec(): array |
| 36 | + { |
| 37 | + $currentPluginDir = Manager::getInstance()::getPluginDirectory('OpenApiDocs'); |
| 38 | + $filePath = $currentPluginDir . OpenApiDocs::GENERATED_SPECS_PATH . 'matomo_openapi_spec_v' . OpenApiDocs::DEFAULT_SPEC_VERSION . '.json'; |
| 39 | + |
| 40 | + if (!is_file($filePath) || !is_readable($filePath)) { |
| 41 | + throw new \Exception('OpenAPI spec file was not found. Generate it first via openapidocs:generate-spec-file.'); |
| 42 | + } |
| 43 | + |
| 44 | + $specContents = file_get_contents($filePath); |
| 45 | + if ($specContents === false) { |
| 46 | + throw new \Exception('OpenAPI spec file could not be read.'); |
| 47 | + } |
| 48 | + |
| 49 | + $decodedSpec = json_decode($specContents, true); |
| 50 | + if (!is_array($decodedSpec) || json_last_error() !== JSON_ERROR_NONE) { |
| 51 | + throw new \Exception('OpenAPI spec file contains invalid JSON.'); |
| 52 | + } |
| 53 | + |
| 54 | + return $decodedSpec; |
| 55 | + } |
| 56 | + |
22 | 57 | /** |
23 | 58 | * Get the generated API documentation data for the specified plugin. |
24 | 59 | * |
|
0 commit comments