3 namespace Friendica\Core\Config;
6 * This class is responsible for the user-specific configuration values in Friendica
7 * The values are set through the Config-DB-Table (per Config-DB-adapter @see Adapter\IPConfigAdapter )
9 * The configuration cache (@see Cache\IPConfigCache ) is used for temporary caching of database calls. This will
10 * increase the performance.
15 * @var Cache\IPConfigCache
20 * @var Adapter\IPConfigAdapter
22 private $configAdapter;
25 * @param Cache\IPConfigCache $configCache The configuration cache
26 * @param Adapter\IPConfigAdapter $configAdapter The configuration DB-backend
28 public function __construct(Cache\IPConfigCache $configCache, Adapter\IPConfigAdapter $configAdapter)
30 $this->configCache = $configCache;
31 $this->configAdapter = $configAdapter;
35 * @brief Loads all configuration values of a user's config family into a cached storage.
37 * All configuration values of the given user are stored with the $uid in
38 * the cache ( @see IPConfigCache )
40 * @param string $uid The user_id
41 * @param string $cat The category of the configuration value
45 public function load($uid, $cat = 'config')
47 // If not connected, do nothing
48 if (!$this->configAdapter->isConnected()) {
52 // load the whole category out of the DB into the cache
53 $this->configCache->loadP($uid, $this->configAdapter->load($uid, $cat));
57 * @brief Get a particular user's config variable given the category name
60 * Get a particular user's config value from the given category ($cat)
61 * and the $key with the $uid from a cached storage either from the $this->configAdapter
62 * (@see IConfigAdapter ) or from the $this->configCache (@see IConfigCache ).
64 * @param string $uid The user_id
65 * @param string $cat The category of the configuration value
66 * @param string $key The configuration key to query
67 * @param mixed $default_value optional, The value to return if key is not set (default: null)
68 * @param boolean $refresh optional, If true the config is loaded from the db and not from the cache (default: false)
70 * @return mixed Stored value or null if it does not exist
72 public function get($uid, $cat, $key, $default_value = null, $refresh = false)
74 // Return the value of the cache if found and no refresh is forced
75 if (!$refresh && $this->configCache->hasP($uid, $cat, $key)) {
76 return $this->configCache->getP($uid, $cat, $key);
79 // if we don't find the value in the cache and the adapter isn't ready, return the default value
80 if (!$this->configAdapter->isConnected()) {
81 return $default_value;
84 // load DB value to cache
85 $dbvalue = $this->configAdapter->get($uid, $cat, $key);
87 if ($dbvalue !== '!<unset>!') {
88 $this->configCache->setP($uid, $cat, $key, $dbvalue);
91 return $default_value;
96 * @brief Sets a configuration value for a user
98 * Stores a config value ($value) in the category ($family) under the key ($key)
99 * for the user_id $uid.
101 * @note Please do not store booleans - convert to 0/1 integer values!
103 * @param string $uid The user_id
104 * @param string $cat The category of the configuration value
105 * @param string $key The configuration key to set
106 * @param mixed $value The value to store
108 * @return bool Operation success
110 public function set($uid, $cat, $key, $value)
112 // set the cache first
113 $cached = $this->configCache->setP($uid, $cat, $key, $value);
115 // If there is no connected adapter, we're finished
116 if (!$this->configAdapter->isConnected()) {
120 $stored = $this->configAdapter->set($uid, $cat, $key, $value);
122 return $cached && $stored;
126 * @brief Deletes the given key from the users's configuration.
128 * Removes the configured value from the stored cache in $this->configCache
129 * (@see ConfigCache ) and removes it from the database (@see IConfigAdapter )
130 * with the given $uid.
132 * @param string $uid The user_id
133 * @param string $cat The category of the configuration value
134 * @param string $key The configuration key to delete
138 public function delete($uid, $cat, $key)
140 $cacheRemoved = $this->configCache->deleteP($uid, $cat, $key);
142 if (!$this->configAdapter->isConnected()) {
143 return $cacheRemoved;
146 $storeRemoved = $this->configAdapter->delete($uid, $cat, $key);
148 return $cacheRemoved || $storeRemoved;