]> git.mxchange.org Git - friendica.git/blob - src/Core/Config.php
3) Introducing ConfigFactory
[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\Core\Config\IConfigAdapter;
12 use Friendica\Core\Config\IConfigCache;
13
14 /**
15  * @brief Arbitrary system configuration storage
16  *
17  * Note:
18  * If we ever would decide to return exactly the variable type as entered,
19  * we will have fun with the additional features. :-)
20  */
21 class Config
22 {
23         /**
24          * @var IConfigAdapter
25          */
26         private static $adapter;
27
28         /**
29          * @var IConfigCache
30          */
31         private static $config;
32
33         /**
34          * Initialize the config with only the cache
35          *
36          * @param IConfigCache $config  The configuration cache
37          */
38         public static function init($config)
39         {
40                 self::$config  = $config;
41         }
42
43         /**
44          * Add the adapter for DB-backend
45          *
46          * @param $adapter
47          */
48         public static function setAdapter($adapter)
49         {
50                 self::$adapter = $adapter;
51         }
52
53         /**
54          * @brief Loads all configuration values of family into a cached storage.
55          *
56          * All configuration values of the system are stored in global cache
57          * which is available under the global variable self::$config
58          *
59          * @param string $family The category of the configuration value
60          *
61          * @return void
62          */
63         public static function load($family = "config")
64         {
65                 if (!isset(self::$adapter)) {
66                         return;
67                 }
68
69                 self::$adapter->load($family);
70         }
71
72         /**
73          * @brief Get a particular user's config variable given the category name
74          * ($family) and a key.
75          *
76          * Get a particular config value from the given category ($family)
77          * and the $key from a cached storage in static::config[$uid].
78          * $instore is only used by the set_config function
79          * to determine if the key already exists in the DB
80          * If a key is found in the DB but doesn't exist in
81          * local config cache, pull it into the cache so we don't have
82          * to hit the DB again for this item.
83          *
84          * @param string  $family        The category of the configuration value
85          * @param string  $key           The configuration key to query
86          * @param mixed   $default_value optional, The value to return if key is not set (default: null)
87          * @param boolean $refresh       optional, If true the config is loaded from the db and not from the cache (default: false)
88          *
89          * @return mixed Stored value or null if it does not exist
90          */
91         public static function get($family, $key, $default_value = null, $refresh = false)
92         {
93                 if (!isset(self::$adapter)) {
94                         return self::$config->get($family, $key, $default_value);
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                 if (!isset(self::$adapter)) {
117                         self::$config->set($family, $key, $value);
118                         return true;
119                 }
120
121                 return self::$adapter->set($family, $key, $value);
122         }
123
124         /**
125          * @brief Deletes the given key from the system configuration.
126          *
127          * Removes the configured value from the stored cache in Config::$config
128          * and removes it from the database.
129          *
130          * @param string $family The category of the configuration value
131          * @param string $key    The configuration key to delete
132          *
133          * @return mixed
134          */
135         public static function delete($family, $key)
136         {
137                 if (!isset(self::$adapter)) {
138                         self::$config->delete($family, $key);
139                 }
140
141                 return self::$adapter->delete($family, $key);
142         }
143 }