diff options
author | Thomas Steur <thomas.steur@gmail.com> | 2016-03-10 00:55:45 +0300 |
---|---|---|
committer | Thomas Steur <thomas.steur@gmail.com> | 2016-04-11 05:11:33 +0300 |
commit | b52ae4e7e488e0474d67c54578e1d6c1aa066bff (patch) | |
tree | f94b02f774cbc24faaa18f29ee1e19fef8b338af /core/Settings/Storage/Factory.php | |
parent | 6ba622a68a26792af8cc22131f488f7ff5189d2c (diff) |
refs #7983 let plugins add or remove fields to websites and better settings api
Diffstat (limited to 'core/Settings/Storage/Factory.php')
-rw-r--r-- | core/Settings/Storage/Factory.php | 107 |
1 files changed, 100 insertions, 7 deletions
diff --git a/core/Settings/Storage/Factory.php b/core/Settings/Storage/Factory.php index 87d54ad3b1..5c1c6ca645 100644 --- a/core/Settings/Storage/Factory.php +++ b/core/Settings/Storage/Factory.php @@ -9,20 +9,113 @@ namespace Piwik\Settings\Storage; -use Piwik\Settings\Storage; +use Piwik\Settings\Storage\Backend\BackendInterface; use Piwik\SettingsServer; -use Piwik\Tracker\SettingsStorage; +/** + * Factory to create an instance of a storage. The storage can be created with different backends depending on the need. + * + * @package Piwik\Settings\Storage + */ class Factory { - public static function make($pluginName) + // cache prevents multiple loading of storage + private $cache = array(); + + /** + * Get a storage instance for plugin settings. + * + * The storage will hold values that belong to the given plugin name and user login. Be aware that instances + * for a specific plugin and login will be cached during one request for better performance. + * + * @param string $pluginName + * @param string $userLogin Use an empty string if settings should be not for a specific login + * @return Storage + */ + public function getPluginStorage($pluginName, $userLogin) + { + $id = $pluginName . '#' . $userLogin; + + if (!isset($this->cache[$id])) { + $backend = new Backend\PluginSettingsTable($pluginName, $userLogin); + $this->cache[$id] = $this->makeStorage($backend); + } + + return $this->cache[$id]; + } + + /** + * Get a storage instance for measurable settings. + * + * The storage will hold values that belong to the given idSite and plugin name. Be aware that a storage instance + * for a specific site and plugin will be cached during one request for better performance. + * + * @param int $idSite If idSite is empty it will use a backend that never actually persists any value. Pass + * $idSite = 0 to create a storage for a site that is about to be created. + * @param string $pluginName + * @return Storage + */ + public function getMeasurableSettingsStorage($idSite, $pluginName) + { + $id = 'measurableSettings' . (int) $idSite . '#' . $pluginName; + + if (empty($idSite)) { + return $this->getNonPersistentStorage($id . '#nonpersistent'); + } + + if (!isset($this->cache[$id])) { + $backend = new Backend\MeasurableSettingsTable($idSite, $pluginName); + $this->cache[$id] = $this->makeStorage($backend); + } + + return $this->cache[$id]; + } + + /** + * Get a storage instance for settings that will be saved in the "site" table. + * + * The storage will hold values that belong to the given idSite. Be aware that a storage instance for a specific + * site will be cached during one request for better performance. + * + * @param int $idSite If idSite is empty it will use a backend that never actually persists any value. Pass + * $idSite = 0 to create a storage for a site that is about to be created. + * + * @param int $idSite + * @return Storage + */ + public function getSitesTable($idSite) + { + $id = 'sitesTable#' . $idSite; + + if (empty($idSite)) { + return $this->getNonPersistentStorage($id . '#nonpersistent'); + } + + if (!isset($this->cache[$id])) { + $backend = new Backend\SitesTable($idSite); + $this->cache[$id] = $this->makeStorage($backend); + } + + return $this->cache[$id]; + } + + /** + * Get a storage with a backend that will never persist or load any value. + * + * @param string $key + * @return Storage + */ + public function getNonPersistentStorage($key) + { + return new Storage(new Backend\Null($key)); + } + + private function makeStorage(BackendInterface $backend) { if (SettingsServer::isTrackerApiRequest()) { - $storage = new SettingsStorage($pluginName); - } else { - $storage = new Storage($pluginName); + $backend = new Backend\Cache($backend); } - return $storage; + return new Storage($backend); } } |