Skip to content

Commit 3ffd5d0

Browse files
committed
Added plugin list service with event to hook in to
1 parent 4561d34 commit 3ffd5d0

3 files changed

Lines changed: 72 additions & 20 deletions

File tree

API.php

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,31 @@
1010
namespace Piwik\Plugins\OpenApiDocs;
1111

1212
use Piwik\Piwik;
13+
use Piwik\Plugins\OpenApiDocs\Generation\PluginListProvider;
1314
use Piwik\Plugin\Manager;
1415
use Piwik\Plugins\OpenApiDocs\Specs\SpecGenerator;
1516
use Piwik\Plugins\OpenApiDocs\Specs\PathResolver;
1617

1718
/**
1819
* Provides Reporting API endpoints for reading OpenAPI plugin configuration and specifications.
1920
*
20-
* Exposes endpoints to return the configured plugin whitelist, read pre-generated spec files,
21+
* Exposes endpoints to return the effective plugin list for spec generation, read pre-generated spec files,
2122
* or generate plugin OpenAPI specifications on demand.
2223
*
2324
* @method static \Piwik\Plugins\OpenApiDocs\API getInstance()
2425
*/
2526
class API extends \Piwik\Plugin\API
2627
{
2728
/**
28-
* Returns the plugin names configured for OpenApiDocs spec generation.
29+
* Returns the plugin names used for OpenApiDocs spec generation.
2930
*
30-
* @return array<int, string> The configured whitelist of plugin names from
31-
* `config/plugins.php`.
31+
* @return array<int, string>
3232
*/
3333
public function getPluginWhitelist(): array
3434
{
3535
Piwik::checkUserHasSomeViewAccess();
3636

37-
$pluginWhitelist = $this->loadPluginWhitelist();
38-
if (!is_array($pluginWhitelist)) {
39-
throw new \Exception('OpenApiDocs plugin whitelist config is invalid.');
40-
}
41-
42-
return $pluginWhitelist;
37+
return PluginListProvider::getPluginsForSpecGeneration();
4338
}
4439

4540
/**
@@ -88,14 +83,6 @@ protected function getSpecFilePath(string $pluginName): string
8883
return $this->getSpecPathResolver()->getSpecFilePath($pluginName);
8984
}
9085

91-
/**
92-
* @return mixed
93-
*/
94-
protected function loadPluginWhitelist()
95-
{
96-
return require __DIR__ . '/config/plugins.php';
97-
}
98-
9986
protected function isSpecFileReadable(string $filePath): bool
10087
{
10188
return is_file($filePath) && is_readable($filePath);
@@ -126,7 +113,6 @@ protected function getSpecPathResolver(): PathResolver
126113
{
127114
return new PathResolver();
128115
}
129-
130116
/**
131117
* Generates an OpenAPI specification for one or more plugins and returns it immediately.
132118
*

Generation/PluginListProvider.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/**
4+
* Matomo - free/libre analytics platform
5+
*
6+
* @link https://matomo.org
7+
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Piwik\Plugins\OpenApiDocs\Generation;
13+
14+
use Piwik\Plugin\Manager;
15+
use Piwik\Piwik;
16+
use Piwik\Plugins\OpenApiDocs\OpenApiDocs;
17+
18+
class PluginListProvider
19+
{
20+
/**
21+
* @return string[]
22+
*/
23+
public static function getPluginsForSpecGeneration(): array
24+
{
25+
$pluginNames = [];
26+
27+
foreach (Manager::getInstance()->getInstalledPluginsName() as $pluginName) {
28+
if (!self::shouldIncludePlugin($pluginName)) {
29+
continue;
30+
}
31+
32+
$pluginNames[] = $pluginName;
33+
}
34+
35+
self::dispatchUpdatePluginListEvent($pluginNames);
36+
37+
return array_values(array_unique($pluginNames));
38+
}
39+
40+
private static function shouldIncludePlugin(string $pluginName): bool
41+
{
42+
if (in_array($pluginName, OpenApiDocs::PLUGIN_BLOCKLIST, true)) {
43+
return false;
44+
}
45+
46+
$pluginManager = Manager::getInstance();
47+
48+
if (
49+
!$pluginManager->isPluginActivated($pluginName)
50+
|| !$pluginManager->isPluginInFilesystem($pluginName)
51+
) {
52+
return false;
53+
}
54+
55+
return is_file(Manager::getPluginDirectory($pluginName) . '/API.php');
56+
}
57+
58+
/**
59+
* @param string[] $pluginNames
60+
*/
61+
private static function dispatchUpdatePluginListEvent(array &$pluginNames): void
62+
{
63+
Piwik::postEvent('OpenApiDocs.updatePluginList', [&$pluginNames]);
64+
}
65+
}

Tasks.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Piwik\Config;
1515
use Piwik\Log\LoggerInterface;
16+
use Piwik\Plugins\OpenApiDocs\Generation\PluginListProvider;
1617
use Piwik\Plugins\OpenApiDocs\Generation\SpecGenerationService;
1718

1819
class Tasks extends \Piwik\Plugin\Tasks
@@ -42,7 +43,7 @@ public function schedule()
4243

4344
public function generateConfiguredPluginSpecs(): void
4445
{
45-
$pluginNames = require __DIR__ . '/config/plugins.php';
46+
$pluginNames = PluginListProvider::getPluginsForSpecGeneration();
4647

4748
foreach ($pluginNames as $pluginName) {
4849
try {

0 commit comments

Comments
 (0)