-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpecGenerationService.php
More file actions
88 lines (74 loc) · 2.74 KB
/
SpecGenerationService.php
File metadata and controls
88 lines (74 loc) · 2.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
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
declare(strict_types=1);
namespace Piwik\Plugins\OpenApiDocs\Generation;
use Piwik\Plugins\OpenApiDocs\Annotations\AnnotationGenerator;
use Piwik\Plugins\OpenApiDocs\OpenApiDocs;
use Piwik\Plugins\OpenApiDocs\Specs\SpecGenerator;
class SpecGenerationService
{
/**
* @var AnnotationGenerator
*/
private $annotationGenerator;
/**
* @var SpecGenerator
*/
private $specGenerator;
public function __construct(AnnotationGenerator $annotationGenerator, SpecGenerator $specGenerator)
{
$this->annotationGenerator = $annotationGenerator;
$this->specGenerator = $specGenerator;
}
/**
* Generate an OpenAPI spec for one or more comma-separated plugin names.
*
* @param string $pluginNames Comma-separated plugin names to include in the generated spec.
* @param string $format Output format for the spec, for example `json` or `yaml`.
* @param string $version Version string written into the generated OpenAPI spec.
* @param bool $writeToFile Whether the generated spec should also be written to the plugin tmp specs directory.
* @param bool $addAnnotations Whether API annotations should be regenerated before building the spec.
* @return string The generated OpenAPI spec contents.
* @throws \RuntimeException If no non-empty plugin names are provided.
*/
public function generateSpecForPlugins(
string $pluginNames,
string $format = 'json',
string $version = OpenApiDocs::DEFAULT_SPEC_VERSION,
bool $writeToFile = false,
bool $addAnnotations = false
): string {
$parsedPluginNames = $this->getPluginNames($pluginNames);
if ($addAnnotations) {
$this->generateAnnotations($parsedPluginNames);
}
return $this->specGenerator->generateSpec($parsedPluginNames, $format, $version, $writeToFile);
}
/**
* @param string[] $pluginNames
*/
private function generateAnnotations(array $pluginNames): void
{
foreach ($pluginNames as $pluginName) {
$this->annotationGenerator->generatePluginApiAnnotations($pluginName, true);
}
}
/**
* @return string[]
*/
private function getPluginNames(string $pluginNames): array
{
$plugins = array_filter(array_map('trim', explode(',', $pluginNames)), static function (string $pluginName): bool {
return $pluginName !== '';
});
if (empty($plugins)) {
throw new \RuntimeException('At least one plugin name is required.');
}
return array_values($plugins);
}
}