]> git.mxchange.org Git - friendica.git/blob - src/Core/Config.php
Merge pull request #6209 from MrPetovan/task/move-config-to-php-array
[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 /**
16  * @brief Arbitrary system configuration storage
17  *
18  * Note:
19  * If we ever would decide to return exactly the variable type as entered,
20  * we will have fun with the additional features. :-)
21  */
22 class Config extends BaseObject
23 {
24         /**
25          * @var Friendica\Core\Config\IConfigAdapter
26          */
27         private static $adapter = null;
28
29         public static function init()
30         {
31                 // Database isn't ready or populated yet
32                 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
33                         return;
34                 }
35
36                 if (self::getApp()->getConfigValue('system', 'config_adapter') == 'preload') {
37                         self::$adapter = new Config\PreloadConfigAdapter();
38                 } else {
39                         self::$adapter = new Config\JITConfigAdapter();
40                 }
41         }
42
43         /**
44          * @brief Loads all configuration values of family into a cached storage.
45          *
46          * All configuration values of the system are stored in global cache
47          * which is available under the global variable $a->config
48          *
49          * @param string $family The category of the configuration value
50          *
51          * @return void
52          */
53         public static function load($family = "config")
54         {
55                 // Database isn't ready or populated yet
56                 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
57                         return;
58                 }
59
60                 if (empty(self::$adapter)) {
61                         self::init();
62                 }
63
64                 self::$adapter->load($family);
65         }
66
67         /**
68          * @brief Get a particular user's config variable given the category name
69          * ($family) and a key.
70          *
71          * Get a particular config value from the given category ($family)
72          * and the $key from a cached storage in $a->config[$uid].
73          * $instore is only used by the set_config function
74          * to determine if the key already exists in the DB
75          * If a key is found in the DB but doesn't exist in
76          * local config cache, pull it into the cache so we don't have
77          * to hit the DB again for this item.
78          *
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($family, $key, $default_value = null, $refresh = false)
87         {
88                 // Database isn't ready or populated yet, fallback to file config
89                 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
90                         return self::getApp()->getConfigValue($family, $key, $default_value);
91                 }
92
93                 if (empty(self::$adapter)) {
94                         self::init();
95                 }
96
97                 return self::$adapter->get($family, $key, $default_value, $refresh);
98         }
99
100         /**
101          * @brief Sets a configuration value for system config
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 $family The category of the configuration value
109          * @param string $key    The configuration key to set
110          * @param mixed  $value  The value to store
111          *
112          * @return bool Operation success
113          */
114         public static function set($family, $key, $value)
115         {
116                 // Database isn't ready or populated yet
117                 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
118                         return false;
119                 }
120
121                 if (empty(self::$adapter)) {
122                         self::init();
123                 }
124
125                 return self::$adapter->set($family, $key, $value);
126         }
127
128         /**
129          * @brief Deletes the given key from the system configuration.
130          *
131          * Removes the configured value from the stored cache in $a->config
132          * and removes it from the database.
133          *
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($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();
148                 }
149
150                 return self::$adapter->delete($family, $key);
151         }
152 }