]> git.mxchange.org Git - friendica.git/blob - src/Core/PConfig.php
8782a20d234239a048cff2ddfb078276704002e8
[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 $config;
29
30         /**
31          * Initialize the config with only the cache
32          *
33          * @param Config\IPConfigCache $config  The configuration cache
34          */
35         public static function init($config)
36         {
37                 self::$config  = $config;
38         }
39
40         /**
41          * Add the adapter for DB-backend
42          *
43          * @param Config\IPConfigAdapter $adapter
44          */
45         public static function setAdapter($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 in global cache
54          * which is available under the global variable self::$config[$uid].
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 from a cached storage in self::$config[$uid].
76          *
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)
82          *
83          * @return mixed Stored value or null if it does not exist
84          */
85         public static function get($uid, $family, $key, $default_value = null, $refresh = false)
86         {
87                 if (!isset(self::$adapter)) {
88                         return self::$config->getP($uid, $family, $key, $default_value);
89                 }
90
91                 return self::$adapter->get($uid, $family, $key, $default_value, $refresh);
92         }
93
94         /**
95          * @brief Sets a configuration value for a user
96          *
97          * Stores a config value ($value) in the category ($family) under the key ($key)
98          * for the user_id $uid.
99          *
100          * @note  Please do not store booleans - convert to 0/1 integer values!
101          *
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
106          *
107          * @return bool Operation success
108          */
109         public static function set($uid, $family, $key, $value)
110         {
111                 if (!isset(self::$adapter)) {
112                         return self::$config->setP($uid, $family, $key, $value);
113                 }
114
115                 return self::$adapter->set($uid, $family, $key, $value);
116         }
117
118         /**
119          * @brief Deletes the given key from the users's configuration.
120          *
121          * Removes the configured value from the stored cache in self::$config[$uid]
122          * and removes it from the database.
123          *
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
127          *
128          * @return mixed
129          */
130         public static function delete($uid, $family, $key)
131         {
132                 if (!isset(self::$adapter)) {
133                         return self::$config->deleteP($uid, $family, $key);
134                 }
135
136                 return self::$adapter->delete($uid, $family, $key);
137         }
138 }