]> git.mxchange.org Git - friendica.git/blob - src/Core/PConfig.php
Merge pull request #6209 from MrPetovan/task/move-config-to-php-array
[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 use Friendica\App;
12 use Friendica\BaseObject;
13
14 /**
15  * @brief Management of user configuration storage
16  * Note:
17  * Please do not store booleans - convert to 0/1 integer values
18  * The PConfig::get() functions return boolean false for keys that are unset,
19  * and this could lead to subtle bugs.
20  */
21 class PConfig extends BaseObject
22 {
23         /**
24          * @var Friendica\Core\Config\IPConfigAdapter
25          */
26         private static $adapter = null;
27
28         public static function init($uid)
29         {
30                 $a = self::getApp();
31
32                 // Database isn't ready or populated yet
33                 if (!$a->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
34                         return;
35                 }
36
37                 if ($a->getConfigValue('system', 'config_adapter') == 'preload') {
38                         self::$adapter = new Config\PreloadPConfigAdapter($uid);
39                 } else {
40                         self::$adapter = new Config\JITPConfigAdapter($uid);
41                 }
42         }
43
44         /**
45          * @brief Loads all configuration values of a user's config family into a cached storage.
46          *
47          * All configuration values of the given user are stored in global cache
48          * which is available under the global variable $a->config[$uid].
49          *
50          * @param string $uid    The user_id
51          * @param string $family The category of the configuration value
52          *
53          * @return void
54          */
55         public static function load($uid, $family)
56         {
57                 // Database isn't ready or populated yet
58                 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
59                         return;
60                 }
61
62                 if (empty(self::$adapter)) {
63                         self::init($uid);
64                 }
65
66                 self::$adapter->load($uid, $family);
67         }
68
69         /**
70          * @brief Get a particular user's config variable given the category name
71          * ($family) and a key.
72          *
73          * Get a particular user's config value from the given category ($family)
74          * and the $key from a cached storage in $a->config[$uid].
75          *
76          * @param string  $uid           The user_id
77          * @param string  $family        The category of the configuration value
78          * @param string  $key           The configuration key to query
79          * @param mixed   $default_value optional, The value to return if key is not set (default: null)
80          * @param boolean $refresh       optional, If true the config is loaded from the db and not from the cache (default: false)
81          *
82          * @return mixed Stored value or null if it does not exist
83          */
84         public static function get($uid, $family, $key, $default_value = null, $refresh = false)
85         {
86                 // Database isn't ready or populated yet
87                 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
88                         return;
89                 }
90
91                 if (empty(self::$adapter)) {
92                         self::init($uid);
93                 }
94
95                 return self::$adapter->get($uid, $family, $key, $default_value, $refresh);
96         }
97
98         /**
99          * @brief Sets a configuration value for a user
100          *
101          * Stores a config value ($value) in the category ($family) under the key ($key)
102          * for the user_id $uid.
103          *
104          * @note Please do not store booleans - convert to 0/1 integer values!
105          *
106          * @param string $uid    The user_id
107          * @param string $family The category of the configuration value
108          * @param string $key    The configuration key to set
109          * @param string $value  The value to store
110          *
111          * @return bool Operation success
112          */
113         public static function set($uid, $family, $key, $value)
114         {
115                 // Database isn't ready or populated yet
116                 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
117                         return false;
118                 }
119
120                 if (empty(self::$adapter)) {
121                         self::init($uid);
122                 }
123
124                 return self::$adapter->set($uid, $family, $key, $value);
125         }
126
127         /**
128          * @brief Deletes the given key from the users's configuration.
129          *
130          * Removes the configured value from the stored cache in $a->config[$uid]
131          * and removes it from the database.
132          *
133          * @param string $uid    The user_id
134          * @param string $family The category of the configuration value
135          * @param string $key    The configuration key to delete
136          *
137          * @return mixed
138          */
139         public static function delete($uid, $family, $key)
140         {
141                 // Database isn't ready or populated yet
142                 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
143                         return false;
144                 }
145
146                 if (empty(self::$adapter)) {
147                         self::init($uid);
148                 }
149
150                 return self::$adapter->delete($uid, $family, $key);
151         }
152 }