Skip to content

Commit 221b611

Browse files
committed
inactive plugins now can have generated specs
1 parent 5cfce34 commit 221b611

3 files changed

Lines changed: 33 additions & 21 deletions

File tree

Annotations/AnnotationGenerator.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Matomo\Dependencies\OpenApiDocs\phpDocumentor\Reflection\DocBlock\Tags\Param;
1616
use Matomo\Dependencies\OpenApiDocs\phpDocumentor\Reflection\DocBlock\Tags\TagWithType;
1717
use Matomo\Dependencies\OpenApiDocs\phpDocumentor\Reflection\DocBlockFactory;
18+
use Piwik\Exception\PluginNotFoundException;
1819
use Piwik\API\DocumentationGenerator;
1920
use Piwik\API\NoDefaultValue;
2021
use Piwik\API\Proxy;
@@ -98,13 +99,15 @@ public function __construct(DocumentationGenerator $generator)
9899
*
99100
* @return string[]|array[] The collection of all the lines which make up the generated annotations for the public API
100101
* endpoints defined by the plugin.
101-
* @throws \Piwik\Exception\PluginDeactivatedException If the plugin is not activated. It should be loaded.
102+
* @throws PluginNotFoundException If the plugin is not present in the filesystem.
102103
* @throws \Throwable
103104
*/
104105
public function generatePluginApiAnnotations(string $pluginName, bool $writeToFile = false): array
105106
{
106107
BaseValidator::check('plugin', $pluginName, [new NotEmpty()]);
107-
Manager::getInstance()->checkIsPluginActivated($pluginName);
108+
if (!Manager::getInstance()->isPluginInFilesystem($pluginName)) {
109+
throw new PluginNotFoundException($pluginName);
110+
}
108111

109112
$rules = require $this->currentPluginDir . '/Annotations/config.php';
110113
$pluginAnnotationDir = $this->currentPluginDir . OpenApiDocs::GENERATED_ANNOTATIONS_PATH;
@@ -1180,24 +1183,27 @@ protected function determineResponses(array $rules, string $plugin, string $meth
11801183
$responseSchema = !empty($responseInfo['type']) ? $this->buildSchemaObjectArray($responseInfo['type']) : [];
11811184

11821185
$mediaTypes = [];
1183-
// This simply reuses the example URLs used by the current documentation, but some endpoints don't work because authentication is required
1184-
$exampleUrls = $this->getApplicableDemoExampleUrls($plugin, $method, $paramsData);
1185-
foreach ($exampleUrls as $type => $url) {
1186-
$exampleValue = $this->getExampleIfAvailable($url);
1187-
// If the example lookup failed, try making the same request locally using a local token.
1188-
if (empty($exampleValue)) {
1189-
$exampleValue = $this->getExampleIfAvailable($url, true);
1190-
}
1191-
if (strlen($exampleValue) > self::EXAMPLE_CHAR_LIMIT) {
1192-
$exampleValue = $this->cutExampleCloseToCharLimit($exampleValue, $type);
1193-
}
1186+
$exampleUrls = [];
1187+
if (Manager::getInstance()->isPluginActivated($plugin)) {
1188+
// Only fetch live examples for activated plugins since their endpoints can be executed safely.
1189+
$exampleUrls = $this->getApplicableDemoExampleUrls($plugin, $method, $paramsData);
1190+
foreach ($exampleUrls as $type => $url) {
1191+
$exampleValue = $this->getExampleIfAvailable($url);
1192+
// If the example lookup failed, try making the same request locally using a local token.
1193+
if (empty($exampleValue)) {
1194+
$exampleValue = $this->getExampleIfAvailable($url, true);
1195+
}
1196+
if (strlen($exampleValue) > self::EXAMPLE_CHAR_LIMIT) {
1197+
$exampleValue = $this->cutExampleCloseToCharLimit($exampleValue, $type);
1198+
}
11941199

1195-
// Skip if there was no example response
1196-
if (empty($exampleValue)) {
1197-
continue;
1198-
}
1200+
// Skip if there was no example response
1201+
if (empty($exampleValue)) {
1202+
continue;
1203+
}
11991204

1200-
$mediaTypes[] = $this->buildMediaTypePropertiesArray($type, $exampleValue, $responseSchema);
1205+
$mediaTypes[] = $this->buildMediaTypePropertiesArray($type, $exampleValue, $responseSchema);
1206+
}
12011207
}
12021208

12031209
// Check if any example files exist even though there aren't any example URLs

Annotations/ApiMethodInfoExtractor.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Piwik\Plugins\OpenApiDocs\Annotations;
1313

14+
use Piwik\Exception\PluginNotFoundException;
1415
use Piwik\API\Proxy;
1516
use Piwik\API\Request;
1617
use Piwik\Plugin\Manager;
@@ -42,7 +43,9 @@ public function extractMethodInfo(string $pluginName, bool $writeToFile = false)
4243
$methodInfoArray = [];
4344
foreach ($pluginNames as $plugin) {
4445
BaseValidator::check('pluginName', $plugin, [new NotEmpty()]);
45-
Manager::getInstance()->checkIsPluginActivated($plugin);
46+
if (!Manager::getInstance()->isPluginInFilesystem($plugin)) {
47+
throw new PluginNotFoundException($plugin);
48+
}
4649

4750
$className = Request::getClassNameAPI($plugin);
4851

Specs/SpecGenerator.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Matomo\Dependencies\OpenApiDocs\OpenApi\Annotations\OpenApi;
1313
use Matomo\Dependencies\OpenApiDocs\OpenApi\Generator;
1414
use Piwik\Container\StaticContainer;
15+
use Piwik\Exception\PluginNotFoundException;
1516
use Piwik\Log\LoggerInterface;
1617
use Piwik\Log\NullLogger;
1718
use Piwik\Plugin\Manager;
@@ -59,7 +60,7 @@ public function generatePluginDoc(string $pluginName, string $format = 'json', s
5960
* @return string
6061
* @throws \Piwik\Exception\DI\DependencyException
6162
* @throws \Piwik\Exception\DI\NotFoundException
62-
* @throws \Piwik\Exception\PluginDeactivatedException
63+
* @throws PluginNotFoundException
6364
* @throws \Exception
6465
*/
6566
public function generateSpec(array $pluginNames, string $format = 'json', string $version = OpenApiDocs::DEFAULT_SPEC_VERSION, bool $writeToFile = false): string
@@ -70,7 +71,9 @@ public function generateSpec(array $pluginNames, string $format = 'json', string
7071
$pluginDirs = [];
7172
foreach ($pluginNames as $pluginName) {
7273
BaseValidator::check('pluginName', $pluginName, [new NotEmpty()]);
73-
Manager::getInstance()->checkIsPluginActivated($pluginName);
74+
if (!Manager::getInstance()->isPluginInFilesystem($pluginName)) {
75+
throw new PluginNotFoundException($pluginName);
76+
}
7477

7578
$pluginAnnotationsSource = $currentPluginDir . '/tmp/annotations/' . $pluginName . 'GeneratedAnnotations.php';
7679
try {

0 commit comments

Comments
 (0)