3 namespace Friendica\Core\Config;
6 * This class is responsible for all system-wide configuration values in Friendica
7 * There are two types of storage
8 * - The Config-Files (loaded into the FileCache @see Cache\IConfigCache )
9 * - The Config-DB-Table (per Config-DB-adapter @see Adapter\IConfigAdapter )
14 * @var Cache\IConfigCache
19 * @var Adapter\IConfigAdapter
21 private $configAdapter;
24 * @param Cache\IConfigCache $configCache The configuration cache (based on the config-files)
25 * @param Adapter\IConfigAdapter $configAdapter The configuration DB-backend
27 public function __construct(Cache\IConfigCache $configCache, Adapter\IConfigAdapter $configAdapter)
29 $this->configCache = $configCache;
30 $this->configAdapter = $configAdapter;
36 * Returns the Config Cache
38 * @return Cache\IConfigCache
40 public function getCache()
42 return $this->configCache;
46 * @brief Loads all configuration values of family into a cached storage.
48 * All configuration values of the system are stored in the cache ( @see IConfigCache )
50 * @param string $cat The category of the configuration value
54 public function load($cat = 'config')
56 // If not connected, do nothing
57 if (!$this->configAdapter->isConnected()) {
61 // load the whole category out of the DB into the cache
62 $this->configCache->load($this->configAdapter->load($cat), true);
66 * @brief Get a particular user's config variable given the category name
69 * Get a particular config value from the given category ($cat)
70 * and the $key from a cached storage either from the $this->configAdapter
71 * (@see IConfigAdapter ) or from the $this->configCache (@see IConfigCache ).
73 * @param string $cat The category of the configuration value
74 * @param string $key The configuration key to query
75 * @param mixed $default_value optional, The value to return if key is not set (default: null)
76 * @param boolean $refresh optional, If true the config is loaded from the db and not from the cache (default: false)
78 * @return mixed Stored value or null if it does not exist
80 public function get($cat, $key, $default_value = null, $refresh = false)
82 // if the value isn't loaded or refresh is needed, load it to the cache
83 if ($this->configAdapter->isConnected() &&
84 (!$this->configAdapter->isLoaded($cat, $key) ||
87 $dbvalue = $this->configAdapter->get($cat, $key);
89 if (isset($dbvalue)) {
90 $this->configCache->set($cat, $key, $dbvalue);
95 // use the config cache for return
96 $result = $this->configCache->get($cat, $key);
98 return (isset($result)) ? $result : $default_value;
102 * @brief Sets a configuration value for system config
104 * Stores a config value ($value) in the category ($cat) under the key ($key)
106 * Note: Please do not store booleans - convert to 0/1 integer values!
108 * @param string $cat The category of the configuration value
109 * @param string $key The configuration key to set
110 * @param mixed $value The value to store
112 * @return bool Operation success
114 public function set($cat, $key, $value)
116 // set the cache first
117 $cached = $this->configCache->set($cat, $key, $value);
119 // If there is no connected adapter, we're finished
120 if (!$this->configAdapter->isConnected()) {
124 $stored = $this->configAdapter->set($cat, $key, $value);
126 return $cached && $stored;
130 * @brief Deletes the given key from the system configuration.
132 * Removes the configured value from the stored cache in $this->configCache
133 * (@see ConfigCache ) and removes it from the database (@see IConfigAdapter ).
135 * @param string $cat The category of the configuration value
136 * @param string $key The configuration key to delete
140 public function delete($cat, $key)
142 $cacheRemoved = $this->configCache->delete($cat, $key);
144 if (!$this->configAdapter->isConnected()) {
145 return $cacheRemoved;
148 $storeRemoved = $this->configAdapter->delete($cat, $key);
150 return $cacheRemoved || $storeRemoved;