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 // Return the value of the cache if found and no refresh is forced
83 if (!$refresh && $this->configCache->has($cat, $key)) {
84 return $this->configCache->get($cat, $key);
87 // if we don't find the value in the cache and the adapter isn't ready, return the default value
88 if (!$this->configAdapter->isConnected()) {
89 return $default_value;
92 // load DB value to cache
93 $dbvalue = $this->configAdapter->get($cat, $key);
95 if ($dbvalue !== '!<unset>!') {
96 $this->configCache->set($cat, $key, $dbvalue);
99 return $default_value;
104 * @brief Sets a configuration value for system config
106 * Stores a config value ($value) in the category ($cat) under the key ($key)
108 * Note: Please do not store booleans - convert to 0/1 integer values!
110 * @param string $cat The category of the configuration value
111 * @param string $key The configuration key to set
112 * @param mixed $value The value to store
114 * @return bool Operation success
116 public function set($cat, $key, $value)
118 // set the cache first
119 $cached = $this->configCache->set($cat, $key, $value);
121 // If there is no connected adapter, we're finished
122 if (!$this->configAdapter->isConnected()) {
126 $stored = $this->configAdapter->set($cat, $key, $value);
128 return $cached && $stored;
132 * @brief Deletes the given key from the system configuration.
134 * Removes the configured value from the stored cache in $this->configCache
135 * (@see ConfigCache ) and removes it from the database (@see IConfigAdapter ).
137 * @param string $cat The category of the configuration value
138 * @param string $key The configuration key to delete
142 public function delete($cat, $key)
144 $cacheRemoved = $this->configCache->delete($cat, $key);
146 if (!$this->configAdapter->isConnected()) {
147 return $cacheRemoved;
150 $storeRemoved = $this->configAdapter->delete($cat, $key);
152 return $cacheRemoved || $storeRemoved;