-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI.php
More file actions
116 lines (99 loc) · 3.74 KB
/
API.php
File metadata and controls
116 lines (99 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\OpenApiDocs;
use Piwik\Piwik;
use Piwik\Plugin\Manager;
use Piwik\Plugins\OpenApiDocs\Specs\SpecGenerator;
/**
* API for plugin OpenApiDocs
*
* Exposes endpoints to fetch pre-generated OpenAPI specs or generate plugin-specific
* OpenAPI docs on demand.
*
* @method static \Piwik\Plugins\OpenApiDocs\API getInstance()
*/
class API extends \Piwik\Plugin\API
{
/**
* Get the pre-generated single OpenAPI spec file if it exists. This endpoint only reads
* the generated JSON file and does not trigger spec generation.
*
* /index.php?module=API&method=OpenApiDocs.getMatomoOpenApiSpec
*
* @return array<string, mixed> The decoded OpenAPI specification payload.
* @throws \Exception If the file is missing, unreadable, or contains invalid JSON.
*/
public function getMatomoOpenApiSpec(string $format = 'json'): array
{
Piwik::checkUserHasSomeViewAccess();
if (strtolower($format) !== 'json') {
throw new \Exception(
Piwik::translate(
'General_ExceptionInvalidReportRendererFormat',
[$format, 'json']
)
);
}
$filePath = $this->getMatomoSpecFilePath();
if (!$this->isSpecFileReadable($filePath)) {
throw new \Exception('OpenAPI spec file was not found. Generate it first via openapidocs:generate-spec-file.');
}
$specContents = $this->readSpecFile($filePath);
if ($specContents === false) {
throw new \Exception('OpenAPI spec file could not be read.');
}
$decodedSpec = json_decode($specContents, true);
if (!is_array($decodedSpec) || json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception('OpenAPI spec file contains invalid JSON.');
}
return $decodedSpec;
}
protected function getMatomoSpecFilePath(): string
{
$currentPluginDir = Manager::getInstance()::getPluginDirectory('OpenApiDocs');
return $currentPluginDir . OpenApiDocs::GENERATED_SPECS_PATH . 'matomo_openapi_spec_v' . OpenApiDocs::DEFAULT_SPEC_VERSION . '.json';
}
protected function isSpecFileReadable(string $filePath): bool
{
return is_file($filePath) && is_readable($filePath);
}
/**
* @param string $filePath
* @return string|false
*/
protected function readSpecFile(string $filePath)
{
return file_get_contents($filePath);
}
/**
* Get the generated API documentation data for the specified plugin.
*
* /index.php?module=API&method=OpenApiDocs.getGeneratedOpenApiSpec&plugin=CustomAlerts
*
* @param string $plugin Name of the plugin to get the JSON for. E.g. TagManager or CustomerAlerts
* @param string $format String to indicate JSON or YAML.
* @return string | array
* @throws \Exception
*/
public function getGeneratedOpenApiSpec(string $plugin, string $format)
{
Piwik::checkUserHasSomeViewAccess();
// Return an error if format is something other than JSON or YAML
$allowedFormats = ['json', 'yaml'];
if (!in_array(strtolower($format), $allowedFormats)) {
throw new \Exception(
Piwik::translate(
'General_ExceptionInvalidReportRendererFormat',
[$format, implode(', ', $allowedFormats)]
)
);
}
$docString = (new SpecGenerator())->generatePluginDoc($plugin, $format);
return strtolower($format) === 'json' ? json_decode($docString, true) : $docString;
}
}