]> git.mxchange.org Git - friendica.git/blob - src/Core/Config.php
Merge pull request #4540 from MrPetovan/task/4520-load-whole-config
[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\BaseObject;
12 use Friendica\Core\Config;
13
14 require_once 'include/dba.php';
15
16 /**
17  * @brief Arbitrary system configuration storage
18  *
19  * Note:
20  * If we ever would decide to return exactly the variable type as entered,
21  * we will have fun with the additional features. :-)
22  */
23 class Config extends BaseObject
24 {
25         /**
26          * @var Friendica\Core\Config\IConfigAdapter
27          */
28         private static $adapter = null;
29
30         public static function init()
31         {
32                 if (self::getApp()->getConfigValue('system', 'config_adapter') == 'preload') {
33                         self::$adapter = new Config\PreloadConfigAdapter();
34                 } else {
35                         self::$adapter = new Config\JITConfigAdapter();
36                 }
37         }
38
39         /**
40          * @brief Loads all configuration values of family into a cached storage.
41          *
42          * All configuration values of the system are stored in global cache
43          * which is available under the global variable $a->config
44          *
45          * @param string $family The category of the configuration value
46          *
47          * @return void
48          */
49         public static function load($family = "config")
50         {
51                 if (empty(self::$adapter)) {
52                         self::init();
53                 }
54
55                 self::$adapter->load($family);
56         }
57
58         /**
59          * @brief Get a particular user's config variable given the category name
60          * ($family) and a key.
61          *
62          * Get a particular config value from the given category ($family)
63          * and the $key from a cached storage in $a->config[$uid].
64          * $instore is only used by the set_config function
65          * to determine if the key already exists in the DB
66          * If a key is found in the DB but doesn't exist in
67          * local config cache, pull it into the cache so we don't have
68          * to hit the DB again for this item.
69          *
70          * @param string  $family        The category of the configuration value
71          * @param string  $key           The configuration key to query
72          * @param mixed   $default_value optional, The value to return if key is not set (default: null)
73          * @param boolean $refresh       optional, If true the config is loaded from the db and not from the cache (default: false)
74          *
75          * @return mixed Stored value or null if it does not exist
76          */
77         public static function get($family, $key, $default_value = null, $refresh = false)
78         {
79                 if (empty(self::$adapter)) {
80                         self::init();
81                 }
82
83                 return self::$adapter->get($family, $key, $default_value, $refresh);
84         }
85
86         /**
87          * @brief Sets a configuration value for system config
88          *
89          * Stores a config value ($value) in the category ($family) under the key ($key)
90          * for the user_id $uid.
91          *
92          * Note: Please do not store booleans - convert to 0/1 integer values!
93          *
94          * @param string $family The category of the configuration value
95          * @param string $key    The configuration key to set
96          * @param mixed  $value  The value to store
97          *
98          * @return mixed Stored $value or false if the database update failed
99          */
100         public static function set($family, $key, $value)
101         {
102                 if (empty(self::$adapter)) {
103                         self::init();
104                 }
105
106                 return self::$adapter->set($family, $key, $value);
107         }
108
109         /**
110          * @brief Deletes the given key from the system configuration.
111          *
112          * Removes the configured value from the stored cache in $a->config
113          * and removes it from the database.
114          *
115          * @param string $family The category of the configuration value
116          * @param string $key    The configuration key to delete
117          *
118          * @return mixed
119          */
120         public static function delete($family, $key)
121         {
122                 if (empty(self::$adapter)) {
123                         self::init();
124                 }
125
126                 return self::$adapter->delete($family, $key);
127         }
128 }