]> git.mxchange.org Git - friendica.git/blob - src/Core/PConfig.php
Merge pull request #6600 from annando/false-notifications
[friendica.git] / src / Core / PConfig.php
1 <?php
2 /**
3  * User Configuration Class
4  *
5  * @file include/Core/PConfig.php
6  *
7  * @brief Contains the class with methods for user configuration
8  */
9 namespace Friendica\Core;
10
11 /**
12  * @brief Management of user configuration storage
13  * Note:
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.
17  */
18 class PConfig
19 {
20         /**
21          * @var Config\IPConfigAdapter
22          */
23         private static $adapter;
24
25         /**
26          * @var Config\IPConfigCache
27          */
28         private static $cache;
29
30         /**
31          * Initialize the config with only the cache
32          *
33          * @param Config\IPConfigCache $cache  The configuration cache
34          */
35         public static function init(Config\IPConfigCache $cache)
36         {
37                 self::$cache  = $cache;
38         }
39
40         /**
41          * Add the adapter for DB-backend
42          *
43          * @param Config\IPConfigAdapter $adapter
44          */
45         public static function setAdapter(Config\IPConfigAdapter $adapter)
46         {
47                 self::$adapter = $adapter;
48         }
49
50         /**
51          * @brief Loads all configuration values of a user's config family into a cached storage.
52          *
53          * All configuration values of the given user are stored with the $uid in
54          * the cache ( @see IPConfigCache )
55          *
56          * @param string $uid    The user_id
57          * @param string $family The category of the configuration value
58          *
59          * @return void
60          */
61         public static function load($uid, $family)
62         {
63                 if (!isset(self::$adapter)) {
64                         return;
65                 }
66
67                 self::$adapter->load($uid, $family);
68         }
69
70         /**
71          * @brief Get a particular user's config variable given the category name
72          * ($family) and a key.
73          *
74          * Get a particular user's config value from the given category ($family)
75          * and the $key with the $uid from a cached storage either from the self::$adapter
76          * (@see IConfigAdapter ) or from the static::$cache (@see IConfigCache ).
77          *
78          * @param string  $uid           The user_id
79          * @param string  $family        The category of the configuration value
80          * @param string  $key           The configuration key to query
81          * @param mixed   $default_value optional, The value to return if key is not set (default: null)
82          * @param boolean $refresh       optional, If true the config is loaded from the db and not from the cache (default: false)
83          *
84          * @return mixed Stored value or null if it does not exist
85          */
86         public static function get($uid, $family, $key, $default_value = null, $refresh = false)
87         {
88                 if (!isset(self::$adapter)) {
89                         return self::$cache->getP($uid, $family, $key, $default_value);
90                 }
91
92                 return self::$adapter->get($uid, $family, $key, $default_value, $refresh);
93         }
94
95         /**
96          * @brief Sets a configuration value for a user
97          *
98          * Stores a config value ($value) in the category ($family) under the key ($key)
99          * for the user_id $uid.
100          *
101          * @note  Please do not store booleans - convert to 0/1 integer values!
102          *
103          * @param string $uid    The user_id
104          * @param string $family The category of the configuration value
105          * @param string $key    The configuration key to set
106          * @param mixed  $value  The value to store
107          *
108          * @return bool Operation success
109          */
110         public static function set($uid, $family, $key, $value)
111         {
112                 if (!isset(self::$adapter)) {
113                         return self::$cache->setP($uid, $family, $key, $value);
114                 }
115
116                 return self::$adapter->set($uid, $family, $key, $value);
117         }
118
119         /**
120          * @brief Deletes the given key from the users's configuration.
121          *
122          * Removes the configured value from the stored cache in self::$config
123          * (@see ConfigCache ) and removes it from the database (@see IConfigAdapter )
124          * with the given $uid.
125          *
126          * @param string $uid    The user_id
127          * @param string $family The category of the configuration value
128          * @param string $key    The configuration key to delete
129          *
130          * @return mixed
131          */
132         public static function delete($uid, $family, $key)
133         {
134                 if (!isset(self::$adapter)) {
135                         return self::$cache->deleteP($uid, $family, $key);
136                 }
137
138                 return self::$adapter->delete($uid, $family, $key);
139         }
140 }