-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI.php
More file actions
133 lines (115 loc) · 4.28 KB
/
API.php
File metadata and controls
133 lines (115 loc) · 4.28 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?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;
use Piwik\Plugins\OpenApiDocs\Specs\PathResolver;
/**
* 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 a pre-generated 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.getOpenApiSpec&spec=CustomAlerts
*
* @param string $pluginName Plugin name used in the generated filename.
* @param string $format Output format. Only `json` is supported.
* @return array<string, mixed> The decoded OpenAPI specification payload.
* @throws \Exception If the file is missing, unreadable, or contains invalid JSON.
*/
public function getOpenApiSpec(string $pluginName, string $format = 'json'): array
{
Piwik::checkUserHasSomeViewAccess();
$this->validateJsonFormat($format);
if (
!Manager::getInstance()->isValidPluginName($pluginName)
|| !Manager::getInstance()->isPluginInFilesystem($pluginName)
) {
throw new \Exception('Invalid plugin name: ' . $pluginName);
}
$filePath = $this->getSpecFilePath($pluginName);
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 getSpecFilePath(string $pluginName): string
{
return $this->getSpecPathResolver()->getSpecFilePath($pluginName);
}
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);
}
protected function validateJsonFormat(string $format): void
{
if (strtolower($format) !== 'json') {
throw new \Exception(
Piwik::translate(
'General_ExceptionInvalidReportRendererFormat',
[$format, 'json']
)
);
}
}
protected function getSpecPathResolver(): PathResolver
{
return new PathResolver();
}
/**
* 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;
}
}