3 * User Configuration Class
5 * @file include/Core/PConfig.php
7 * @brief Contains the class with methods for user configuration
9 namespace Friendica\Core;
12 * @brief Management of user configuration storage
14 * Please do not store booleans - convert to 0/1 integer values
15 * The PConfig::get() functions return boolean false for keys that are unset,
16 * and this could lead to subtle bugs.
21 * @var Config\IPConfigAdapter
23 private static $adapter;
26 * @var Config\IPConfigCache
28 private static $cache;
31 * Initialize the config with only the cache
33 * @param Config\IPConfigCache $cache The configuration cache
35 public static function init(Config\IPConfigCache $cache)
37 self::$cache = $cache;
41 * Add the adapter for DB-backend
43 * @param Config\IPConfigAdapter $adapter
45 public static function setAdapter(Config\IPConfigAdapter $adapter)
47 self::$adapter = $adapter;
51 * @brief Loads all configuration values of a user's config family into a cached storage.
53 * All configuration values of the given user are stored in global cache
54 * which is available under the global variable self::$config[$uid].
56 * @param string $uid The user_id
57 * @param string $family The category of the configuration value
61 public static function load($uid, $family)
63 if (!isset(self::$adapter)) {
67 self::$adapter->load($uid, $family);
71 * @brief Get a particular user's config variable given the category name
72 * ($family) and a key.
74 * Get a particular user's config value from the given category ($family)
75 * and the $key from a cached storage in self::$config[$uid].
77 * @param string $uid The user_id
78 * @param string $family The category of the configuration value
79 * @param string $key The configuration key to query
80 * @param mixed $default_value optional, The value to return if key is not set (default: null)
81 * @param boolean $refresh optional, If true the config is loaded from the db and not from the cache (default: false)
83 * @return mixed Stored value or null if it does not exist
85 public static function get($uid, $family, $key, $default_value = null, $refresh = false)
87 if (!isset(self::$adapter)) {
88 return self::$cache->getP($uid, $family, $key, $default_value);
91 return self::$adapter->get($uid, $family, $key, $default_value, $refresh);
95 * @brief Sets a configuration value for a user
97 * Stores a config value ($value) in the category ($family) under the key ($key)
98 * for the user_id $uid.
100 * @note Please do not store booleans - convert to 0/1 integer values!
102 * @param string $uid The user_id
103 * @param string $family The category of the configuration value
104 * @param string $key The configuration key to set
105 * @param mixed $value The value to store
107 * @return bool Operation success
109 public static function set($uid, $family, $key, $value)
111 if (!isset(self::$adapter)) {
112 return self::$cache->setP($uid, $family, $key, $value);
115 return self::$adapter->set($uid, $family, $key, $value);
119 * @brief Deletes the given key from the users's configuration.
121 * Removes the configured value from the stored cache in self::$config[$uid]
122 * and removes it from the database.
124 * @param string $uid The user_id
125 * @param string $family The category of the configuration value
126 * @param string $key The configuration key to delete
130 public static function delete($uid, $family, $key)
132 if (!isset(self::$adapter)) {
133 return self::$cache->deleteP($uid, $family, $key);
136 return self::$adapter->delete($uid, $family, $key);