Backend
1 month ago
Factory.php
3 months ago
LegacyUserSettingsMigration.php
1 month ago
Storage.php
1 year ago
UserScopedSettingsAccessManager.php
1 month ago
Factory.php
122 lines
| 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 | namespace Piwik\Settings\Storage; |
| 10 | |
| 11 | use Piwik\Settings\Storage\Backend\BackendInterface; |
| 12 | use Piwik\SettingsServer; |
| 13 | /** |
| 14 | * Factory to create an instance of a storage. The storage can be created with different backends depending on the need. |
| 15 | */ |
| 16 | class Factory |
| 17 | { |
| 18 | // cache prevents multiple loading of storage |
| 19 | private $cache = array(); |
| 20 | /** |
| 21 | * Get a storage instance for plugin settings. |
| 22 | * |
| 23 | * The storage will hold values that belong to the given plugin name and user login. Be aware that instances |
| 24 | * for a specific plugin and login will be cached during one request for better performance. |
| 25 | * |
| 26 | * @param string $pluginName |
| 27 | * @param string $userLogin Use an empty string if settings should be not for a specific login |
| 28 | * @return Storage |
| 29 | */ |
| 30 | public function getPluginStorage($pluginName, $userLogin) |
| 31 | { |
| 32 | $id = $pluginName . '#' . $userLogin; |
| 33 | if (!isset($this->cache[$id])) { |
| 34 | $backend = new \Piwik\Settings\Storage\Backend\PluginSettingsTable($pluginName, $userLogin); |
| 35 | $this->cache[$id] = $this->makeStorage($backend); |
| 36 | } |
| 37 | return $this->cache[$id]; |
| 38 | } |
| 39 | /** |
| 40 | * @param string $section |
| 41 | * @return mixed |
| 42 | */ |
| 43 | public function getConfigStorage($section) |
| 44 | { |
| 45 | $id = 'config' . $section; |
| 46 | if (!isset($this->cache[$id])) { |
| 47 | $backend = new \Piwik\Settings\Storage\Backend\Config($section); |
| 48 | $this->cache[$id] = $this->makeStorage($backend); |
| 49 | } |
| 50 | return $this->cache[$id]; |
| 51 | } |
| 52 | /** |
| 53 | * Get a storage instance for measurable settings. |
| 54 | * |
| 55 | * The storage will hold values that belong to the given idSite and plugin name. Be aware that a storage instance |
| 56 | * for a specific site and plugin will be cached during one request for better performance. |
| 57 | * |
| 58 | * @param int $idSite If idSite is empty it will use a backend that never actually persists any value. Pass |
| 59 | * $idSite = 0 to create a storage for a site that is about to be created. |
| 60 | * @param string $pluginName |
| 61 | * @return Storage |
| 62 | */ |
| 63 | public function getMeasurableSettingsStorage($idSite, $pluginName) |
| 64 | { |
| 65 | $id = 'measurableSettings' . (int) $idSite . '#' . $pluginName; |
| 66 | if (empty($idSite)) { |
| 67 | return $this->getNonPersistentStorage($id . '#nonpersistent'); |
| 68 | } |
| 69 | if (!isset($this->cache[$id])) { |
| 70 | $backend = new \Piwik\Settings\Storage\Backend\MeasurableSettingsTable($idSite, $pluginName); |
| 71 | $this->cache[$id] = $this->makeStorage($backend); |
| 72 | } |
| 73 | return $this->cache[$id]; |
| 74 | } |
| 75 | /** |
| 76 | * Get a storage instance for settings that will be saved in the "site" table. |
| 77 | * |
| 78 | * The storage will hold values that belong to the given idSite. Be aware that a storage instance for a specific |
| 79 | * site will be cached during one request for better performance. |
| 80 | * |
| 81 | * @param int $idSite If idSite is empty it will use a backend that never actually persists any value. Pass |
| 82 | * $idSite = 0 to create a storage for a site that is about to be created. |
| 83 | * |
| 84 | * @param int $idSite |
| 85 | * @return Storage |
| 86 | */ |
| 87 | public function getSitesTable($idSite) |
| 88 | { |
| 89 | $id = 'sitesTable#' . $idSite; |
| 90 | if (empty($idSite)) { |
| 91 | return $this->getNonPersistentStorage($id . '#nonpersistent'); |
| 92 | } |
| 93 | if (!isset($this->cache[$id])) { |
| 94 | $backend = new \Piwik\Settings\Storage\Backend\SitesTable($idSite); |
| 95 | $this->cache[$id] = $this->makeStorage($backend); |
| 96 | } |
| 97 | return $this->cache[$id]; |
| 98 | } |
| 99 | /** |
| 100 | * Get a storage with a backend that will never persist or load any value. |
| 101 | * |
| 102 | * @param string $key |
| 103 | * @return Storage |
| 104 | */ |
| 105 | public function getNonPersistentStorage($key) |
| 106 | { |
| 107 | return new \Piwik\Settings\Storage\Storage(new \Piwik\Settings\Storage\Backend\NullBackend($key)); |
| 108 | } |
| 109 | /** |
| 110 | * Makes a new storage object based on a custom backend interface. |
| 111 | * |
| 112 | * @return Storage |
| 113 | */ |
| 114 | public function makeStorage(BackendInterface $backend) |
| 115 | { |
| 116 | if (SettingsServer::isTrackerApiRequest()) { |
| 117 | $backend = new \Piwik\Settings\Storage\Backend\Cache($backend); |
| 118 | } |
| 119 | return new \Piwik\Settings\Storage\Storage($backend); |
| 120 | } |
| 121 | } |
| 122 |