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