3 * User Configuration Class
5 * @file include/Core/PConfig.php
7 * @brief Contains the class with methods for user configuration
9 namespace Friendica\Core;
11 use Friendica\Core\Config\IPConfigCache;
14 * @brief Management of user configuration storage
16 * Please do not store booleans - convert to 0/1 integer values
17 * The PConfig::get() functions return boolean false for keys that are unset,
18 * and this could lead to subtle bugs.
23 * @var \Friendica\Core\Config\IPConfigAdapter
25 private static $adapter;
30 private static $config;
33 * Initialize the config with only the cache
35 * @param IPConfigCache $config The configuration cache
37 public static function init($config)
39 self::$config = $config;
43 * Add the adapter for DB-backend
47 public static function setAdapter($adapter)
49 self::$adapter = $adapter;
53 * @brief Loads all configuration values of a user's config family into a cached storage.
55 * All configuration values of the given user are stored in global cache
56 * which is available under the global variable self::$config[$uid].
58 * @param string $uid The user_id
59 * @param string $family The category of the configuration value
62 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
64 public static function load($uid, $family)
66 if (!isset(self::$adapter)) {
70 self::$adapter->load($uid, $family);
74 * @brief Get a particular user's config variable given the category name
75 * ($family) and a key.
77 * Get a particular user's config value from the given category ($family)
78 * and the $key from a cached storage in self::$config[$uid].
80 * @param string $uid The user_id
81 * @param string $family The category of the configuration value
82 * @param string $key The configuration key to query
83 * @param mixed $default_value optional, The value to return if key is not set (default: null)
84 * @param boolean $refresh optional, If true the config is loaded from the db and not from the cache (default: false)
86 * @return mixed Stored value or null if it does not exist
88 public static function get($uid, $family, $key, $default_value = null, $refresh = false)
90 if (!isset(self::$adapter)) {
91 return self::$config->getP($uid, $family, $key, $default_value);
94 return self::$adapter->get($uid, $family, $key, $default_value, $refresh);
98 * @brief Sets a configuration value for a user
100 * Stores a config value ($value) in the category ($family) under the key ($key)
101 * for the user_id $uid.
103 * @note Please do not store booleans - convert to 0/1 integer values!
105 * @param string $uid The user_id
106 * @param string $family The category of the configuration value
107 * @param string $key The configuration key to set
108 * @param mixed $value The value to store
110 * @return bool Operation success
112 public static function set($uid, $family, $key, $value)
114 if (!isset(self::$adapter)) {
115 return self::$config->setP($uid, $family, $key, $value);
118 return self::$adapter->set($uid, $family, $key, $value);
122 * @brief Deletes the given key from the users's configuration.
124 * Removes the configured value from the stored cache in self::$config[$uid]
125 * and removes it from the database.
127 * @param string $uid The user_id
128 * @param string $family The category of the configuration value
129 * @param string $key The configuration key to delete
132 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
134 public static function delete($uid, $family, $key)
136 if (!isset(self::$adapter)) {
137 return self::$config->deleteP($uid, $family, $key);
140 return self::$adapter->delete($uid, $family, $key);