-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI.php
More file actions
148 lines (127 loc) · 4.88 KB
/
API.php
File metadata and controls
148 lines (127 loc) · 4.88 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?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\Plugins\OpenApiDocs\Generation\PluginListProvider;
use Piwik\Plugin\Manager;
use Piwik\Plugins\OpenApiDocs\Specs\SpecGenerator;
use Piwik\Plugins\OpenApiDocs\Specs\PathResolver;
/**
* Provides Reporting API endpoints for reading OpenAPI plugin configuration and specifications.
*
* Exposes endpoints to return the effective plugin list for spec generation, read pre-generated spec files,
* or generate plugin OpenAPI specifications on demand.
*
* @method static \Piwik\Plugins\OpenApiDocs\API getInstance()
*/
class API extends \Piwik\Plugin\API
{
/**
* Returns the plugin names used for OpenApiDocs spec generation.
*
* @return array<int, string>
*/
public function getAllowedPlugins(): array
{
Piwik::checkUserHasSomeViewAccess();
return $this->getPluginListProvider()->getAllowedPlugins();
}
/**
* Returns a previously generated OpenAPI specification for a plugin.
*
* Reads the stored JSON spec file for the requested plugin and does not trigger
* spec generation.
*
* @param string $pluginName The plugin name whose generated OpenAPI spec file should be read.
* @param string $format The response format to read. Only `json` is supported.
* @return array<string, mixed> The decoded OpenAPI specification data from the generated JSON file.
*/
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();
}
protected function getPluginListProvider(): PluginListProvider
{
return new PluginListProvider();
}
/**
* Generates an OpenAPI specification for one or more plugins and returns it immediately.
*
* @param string $plugin The plugin name to generate, or a comma-separated list of plugin names.
* @param string $format The response format to generate. Supported values are `json` and `yaml`.
* @return array<string, mixed>|string The generated OpenAPI specification as decoded JSON data for
* `json`, or as a YAML string for `yaml`.
*/
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;
}
}