FieldConfig
6 years ago
Measurable
6 years ago
Plugin
6 years ago
Storage
6 years ago
FieldConfig.php
6 years ago
Setting.php
6 years ago
Settings.php
6 years ago
Settings.php
124 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | namespace Piwik\Settings; |
| 10 | |
| 11 | /** |
| 12 | * Base class of all settings providers. |
| 13 | * |
| 14 | * @api |
| 15 | */ |
| 16 | abstract class Settings |
| 17 | { |
| 18 | /** |
| 19 | * An array containing all available settings: Array ( [setting-name] => [setting] ) |
| 20 | * |
| 21 | * @var Setting[] |
| 22 | */ |
| 23 | private $settings = array(); |
| 24 | |
| 25 | protected $pluginName; |
| 26 | |
| 27 | /** |
| 28 | * By default the plugin name is shown in the UI when managing plugin settings. However, you can overwrite |
| 29 | * the displayed title by specifying a title. |
| 30 | * @var string |
| 31 | */ |
| 32 | protected $title = ''; |
| 33 | |
| 34 | public function __construct() |
| 35 | { |
| 36 | if (!isset($this->pluginName)) { |
| 37 | $classname = get_class($this); |
| 38 | $parts = explode('\\', $classname); |
| 39 | |
| 40 | if (count($parts) >= 3) { |
| 41 | $this->pluginName = $parts[2]; |
| 42 | } else { |
| 43 | throw new \Exception(sprintf('Plugin Settings must have a plugin name specified in %s, could not detect plugin name', $classname)); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | public function getTitle() |
| 49 | { |
| 50 | if (!empty($this->title)) { |
| 51 | return $this->title; |
| 52 | } |
| 53 | |
| 54 | return $this->pluginName; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @ignore |
| 59 | */ |
| 60 | public function getPluginName() |
| 61 | { |
| 62 | return $this->pluginName; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @ignore |
| 67 | * @return Setting |
| 68 | */ |
| 69 | public function getSetting($name) |
| 70 | { |
| 71 | if (array_key_exists($name, $this->settings)) { |
| 72 | return $this->settings[$name]; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Implemented by descendants. This method should define plugin settings (via the |
| 78 | * {@link addSetting()}) method and set the introduction text (via the |
| 79 | * {@link setIntroduction()}). |
| 80 | */ |
| 81 | abstract protected function init(); |
| 82 | |
| 83 | /** |
| 84 | * Returns the settings that can be displayed for the current user. |
| 85 | * |
| 86 | * @return Setting[] |
| 87 | */ |
| 88 | public function getSettingsWritableByCurrentUser() |
| 89 | { |
| 90 | return array_filter($this->settings, function (Setting $setting) { |
| 91 | return $setting->isWritableByCurrentUser(); |
| 92 | }); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Adds a new setting to the settings container. |
| 97 | * |
| 98 | * @param Setting $setting |
| 99 | * @throws \Exception If there is a setting with the same name that already exists. |
| 100 | * If the name contains non-alphanumeric characters. |
| 101 | */ |
| 102 | public function addSetting(Setting $setting) |
| 103 | { |
| 104 | $name = $setting->getName(); |
| 105 | |
| 106 | if (isset($this->settings[$name])) { |
| 107 | throw new \Exception(sprintf('A setting with name "%s" does already exist for plugin "%s"', $name, $this->pluginName)); |
| 108 | } |
| 109 | |
| 110 | $this->settings[$name] = $setting; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Saves (persists) the current setting values in the database. |
| 115 | */ |
| 116 | public function save() |
| 117 | { |
| 118 | foreach ($this->settings as $setting) { |
| 119 | $setting->save(); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | } |
| 124 |