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