]> git.mxchange.org Git - friendica.git/blob - src/Core/Config.php
f2b0d12ab02979d28d04ca0a423600ffb365bfc5
[friendica.git] / src / Core / Config.php
1 <?php
2 /**
3  * System Configuration Class
4  *
5  * @file include/Core/Config.php
6  *
7  * @brief Contains the class with methods for system configuration
8  */
9 namespace Friendica\Core;
10
11 use Friendica\App;
12 use Friendica\BaseObject;
13 use Friendica\Core\Config;
14
15 require_once 'include/dba.php';
16
17 /**
18  * @brief Arbitrary system configuration storage
19  *
20  * Note:
21  * If we ever would decide to return exactly the variable type as entered,
22  * we will have fun with the additional features. :-)
23  */
24 class Config extends BaseObject
25 {
26         /**
27          * @var Friendica\Core\Config\IConfigAdapter
28          */
29         private static $adapter = null;
30
31         public static function init()
32         {
33                 // Database isn't ready or populated yet
34                 if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
35                         return;
36                 }
37
38                 if (self::getApp()->getConfigValue('system', 'config_adapter') == 'preload') {
39                         self::$adapter = new Config\PreloadConfigAdapter();
40                 } else {
41                         self::$adapter = new Config\JITConfigAdapter();
42                 }
43         }
44
45         /**
46          * @brief Loads all configuration values of family into a cached storage.
47          *
48          * All configuration values of the system are stored in global cache
49          * which is available under the global variable $a->config
50          *
51          * @param string $family The category of the configuration value
52          *
53          * @return void
54          */
55         public static function load($family = "config")
56         {
57                 // Database isn't ready or populated yet
58                 if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
59                         return;
60                 }
61
62                 if (empty(self::$adapter)) {
63                         self::init();
64                 }
65
66                 self::$adapter->load($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 config value from the given category ($family)
74          * and the $key from a cached storage in $a->config[$uid].
75          * $instore is only used by the set_config function
76          * to determine if the key already exists in the DB
77          * If a key is found in the DB but doesn't exist in
78          * local config cache, pull it into the cache so we don't have
79          * to hit the DB again for this item.
80          *
81          * @param string  $family        The category of the configuration value
82          * @param string  $key           The configuration key to query
83          * @param mixed   $default_value optional, The value to return if key is not set (default: null)
84          * @param boolean $refresh       optional, If true the config is loaded from the db and not from the cache (default: false)
85          *
86          * @return mixed Stored value or null if it does not exist
87          */
88         public static function get($family, $key, $default_value = null, $refresh = false)
89         {
90                 // Database isn't ready or populated yet, fallback to file config
91                 if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
92                         return self::getApp()->getConfigValue($family, $key, $default_value);
93                 }
94
95                 if (empty(self::$adapter)) {
96                         self::init();
97                 }
98
99                 return self::$adapter->get($family, $key, $default_value, $refresh);
100         }
101
102         /**
103          * @brief Sets a configuration value for system config
104          *
105          * Stores a config value ($value) in the category ($family) under the key ($key)
106          * for the user_id $uid.
107          *
108          * Note: Please do not store booleans - convert to 0/1 integer values!
109          *
110          * @param string $family The category of the configuration value
111          * @param string $key    The configuration key to set
112          * @param mixed  $value  The value to store
113          *
114          * @return bool Operation success
115          */
116         public static function set($family, $key, $value)
117         {
118                 // Database isn't ready or populated yet
119                 if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
120                         return false;
121                 }
122
123                 if (empty(self::$adapter)) {
124                         self::init();
125                 }
126
127                 return self::$adapter->set($family, $key, $value);
128         }
129
130         /**
131          * @brief Deletes the given key from the system configuration.
132          *
133          * Removes the configured value from the stored cache in $a->config
134          * and removes it from the database.
135          *
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($family, $key)
142         {
143                 // Database isn't ready or populated yet
144                 if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
145                         return false;
146                 }
147
148                 if (empty(self::$adapter)) {
149                         self::init();
150                 }
151
152                 return self::$adapter->delete($family, $key);
153         }
154 }