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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
<?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\Plugin;
use Piwik\CacheId;
use Piwik\Container\StaticContainer;
use Piwik\Plugin;
use Piwik\Cache as PiwikCache;
use Piwik\Settings\Measurable\MeasurableSettings;
use Piwik\Settings\Plugin\UserSettings;
use Piwik\Settings\Plugin\SystemSettings;
/**
* Base class of all plugin settings providers. Plugins that define their own configuration settings
* can extend this class to easily make their settings available to Piwik users.
*
* Descendants of this class should implement the {@link init()} method and call the
* {@link addSetting()} method for each of the plugin's settings.
*
* For an example, see the {@link Piwik\Plugins\ExampleSettingsPlugin\ExampleSettingsPlugin} plugin.
*/
class SettingsProvider
{
/**
* @var Plugin\Manager
*/
private $pluginManager;
public function __construct(Plugin\Manager $pluginManager)
{
$this->pluginManager = $pluginManager;
}
/**
*
* Get user settings implemented by a specific plugin (if implemented by this plugin).
*/
public function getSystemSettings(string $pluginName): ?SystemSettings
{
$plugin = $this->getLoadedAndActivated($pluginName);
if ($plugin) {
$settings = $plugin->findComponent('SystemSettings', 'Piwik\\Settings\\Plugin\\SystemSettings');
if ($settings) {
return StaticContainer::get($settings);
}
}
return null;
}
/**
* Get user settings implemented by a specific plugin (if implemented by this plugin).
*/
public function getUserSettings(string $pluginName): ?UserSettings
{
$plugin = $this->getLoadedAndActivated($pluginName);
if ($plugin) {
$settings = $plugin->findComponent('UserSettings', 'Piwik\\Settings\\Plugin\\UserSettings');
if ($settings) {
return StaticContainer::get($settings);
}
}
return null;
}
/**
* Returns all available system settings. A plugin has to specify a file named `SystemSettings.php` containing a
* class named `SystemSettings` that extends `Piwik\Settings\Plugin\SystemSettings` in order to be considered as
* a system setting. Otherwise the settings for a plugin won't be available.
*
* @return SystemSettings[] An array containing array([pluginName] => [setting instance]).
*/
public function getAllSystemSettings(): array
{
$cacheId = CacheId::languageAware('AllSystemSettings');
$cache = PiwikCache::getTransientCache();
if (!$cache->contains($cacheId)) {
$pluginNames = $this->pluginManager->getActivatedPlugins();
$byPluginName = array();
foreach ($pluginNames as $plugin) {
$component = $this->getSystemSettings($plugin);
if (!empty($component)) {
$byPluginName[$plugin] = $component;
}
}
$cache->save($cacheId, $byPluginName);
}
return $cache->fetch($cacheId);
}
/**
* Returns all available user settings. A plugin has to specify a file named `UserSettings.php` containing a class
* named `UserSettings` that extends `Piwik\Settings\Plugin\UserSettings` in order to be considered as a plugin
* setting. Otherwise the settings for a plugin won't be available.
*
* @return UserSettings[] An array containing array([pluginName] => [setting instance]).
*/
public function getAllUserSettings(): array
{
$cacheId = CacheId::languageAware('AllUserSettings');
$cache = PiwikCache::getTransientCache();
if (!$cache->contains($cacheId)) {
$pluginNames = $this->pluginManager->getActivatedPlugins();
$byPluginName = array();
foreach ($pluginNames as $plugin) {
$component = $this->getUserSettings($plugin);
if (!empty($component)) {
$byPluginName[$plugin] = $component;
}
}
$cache->save($cacheId, $byPluginName);
}
return $cache->fetch($cacheId);
}
/**
* @api
*
* Get measurable settings for a specific plugin.
*
* @param string $pluginName The name of a plugin.
* @param int $idSite The ID of a site. If a site is about to be created pass idSite = 0.
* @param string|null $idType If null, idType will be detected automatically if the site already exists. Only
* needed to set a value when idSite = 0 (this is the case when a site is about)
* to be created.
*
* @return MeasurableSettings|null Returns null if no MeasurableSettings implemented by this plugin or when plugin
* is not loaded and activated. Returns an instance of the settings otherwise.
*/
public function getMeasurableSettings($pluginName, $idSite, $idType = null)
{
$plugin = $this->getLoadedAndActivated($pluginName);
if ($plugin) {
$component = $plugin->findComponent('MeasurableSettings', 'Piwik\\Settings\\Measurable\\MeasurableSettings');
if ($component) {
return StaticContainer::getContainer()->make($component, [
'idSite' => $idSite,
'idMeasurableType' => $idType,
]);
}
}
return null;
}
/**
* @api
*
* Get all available measurable settings implemented by loaded and activated plugins.
*
* @param int $idSite The ID of a site. If a site is about to be created pass idSite = 0.
* @param string|null $idMeasurableType If null, idType will be detected automatically if the site already exists.
* Only needed to set a value when idSite = 0 (this is the case when a site
* is about) to be created.
*
* @return MeasurableSettings[]
*/
public function getAllMeasurableSettings($idSite, $idMeasurableType = null)
{
$pluginNames = $this->pluginManager->getActivatedPlugins();
$byPluginName = array();
foreach ($pluginNames as $plugin) {
$component = $this->getMeasurableSettings($plugin, $idSite, $idMeasurableType);
if (!empty($component)) {
$byPluginName[$plugin] = $component;
}
}
return $byPluginName;
}
private function getLoadedAndActivated(string $pluginName): ?Plugin
{
if (!$this->pluginManager->isPluginLoaded($pluginName)) {
return null;
}
try {
if (!$this->pluginManager->isPluginActivated($pluginName)) {
return null;
}
$plugin = $this->pluginManager->getLoadedPlugin($pluginName);
} catch (\Exception $e) {
// we are not allowed to use possible settings from this plugin, plugin is not active
return null;
}
return $plugin;
}
}
|