-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginListProvider.php
More file actions
106 lines (86 loc) · 2.77 KB
/
PluginListProvider.php
File metadata and controls
106 lines (86 loc) · 2.77 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
<?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\API\Proxy;
use Piwik\API\Request;
use Piwik\Piwik;
use Piwik\Plugin\Manager;
class PluginListProvider
{
/**
* @var Manager
*/
private $pluginManager;
public function __construct(?Manager $pluginManager = null)
{
$this->pluginManager = $pluginManager ?? Manager::getInstance();
}
/**
* @return string[]
*/
public function getAllowedPlugins(): array
{
$pluginNames = array_values($this->pluginManager->getActivatedPlugins());
$this->dispatchUpdatePluginListEvent($pluginNames);
$pluginNames = array_values(array_unique($pluginNames));
return array_values(array_filter($pluginNames, function ($pluginName): bool {
return is_string($pluginName) && $this->shouldIncludeEventProvidedPlugin($pluginName);
}));
}
/**
* @return array<string, array{description: string}>
*/
public function getAllowedPluginMetadata(): array
{
$metadata = [];
foreach ($this->getAllowedPlugins() as $pluginName) {
try {
$description = $this->getPluginDescription($pluginName);
} catch (\Throwable $e) {
$description = '';
}
$metadata[$pluginName] = [
'description' => $description,
];
}
return $metadata;
}
private function shouldIncludeEventProvidedPlugin(string $pluginName): bool
{
if (!$this->pluginManager->isPluginInFilesystem($pluginName)) {
return false;
}
return $this->pluginHasApiFile($pluginName);
}
protected function pluginHasApiFile(string $pluginName): bool
{
return is_file(Manager::getPluginDirectory($pluginName) . '/API.php');
}
protected function getPluginDescription(string $pluginName): string
{
$apiClassName = Request::getClassNameAPI($pluginName);
Proxy::getInstance()->registerClass($apiClassName);
$documentation = Proxy::getInstance()->getMetadata()[$apiClassName]['__documentation'] ?? '';
return is_string($documentation) ? trim(strip_tags($documentation)) : '';
}
/**
* @param string[] $pluginNames
*/
private function dispatchUpdatePluginListEvent(array &$pluginNames): void
{
$this->postEvent('OpenApiDocs.updatePluginList', [&$pluginNames]);
}
/**
* @param array<int, mixed> $params
*/
protected function postEvent(string $eventName, array $params): void
{
Piwik::postEvent($eventName, $params);
}
}