Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Configuration.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace Friendica\Core\Config;
  3. /**
  4. * This class is responsible for all system-wide configuration values in Friendica
  5. * There are two types of storage
  6. * - The Config-Files (loaded into the FileCache @see Cache\IConfigCache )
  7. * - The Config-DB-Table (per Config-DB-adapter @see Adapter\IConfigAdapter )
  8. */
  9. class Configuration
  10. {
  11. /**
  12. * @var Cache\IConfigCache
  13. */
  14. private $configCache;
  15. /**
  16. * @var Adapter\IConfigAdapter
  17. */
  18. private $configAdapter;
  19. /**
  20. * @param Cache\IConfigCache $configCache The configuration cache (based on the config-files)
  21. * @param Adapter\IConfigAdapter $configAdapter The configuration DB-backend
  22. */
  23. public function __construct(Cache\IConfigCache $configCache, Adapter\IConfigAdapter $configAdapter)
  24. {
  25. $this->configCache = $configCache;
  26. $this->configAdapter = $configAdapter;
  27. $this->load();
  28. }
  29. /**
  30. * Returns the Config Cache
  31. *
  32. * @return Cache\IConfigCache
  33. */
  34. public function getCache()
  35. {
  36. return $this->configCache;
  37. }
  38. /**
  39. * @brief Loads all configuration values of family into a cached storage.
  40. *
  41. * All configuration values of the system are stored in the cache ( @see IConfigCache )
  42. *
  43. * @param string $cat The category of the configuration value
  44. *
  45. * @return void
  46. */
  47. public function load($cat = 'config')
  48. {
  49. // If not connected, do nothing
  50. if (!$this->configAdapter->isConnected()) {
  51. return;
  52. }
  53. // load the whole category out of the DB into the cache
  54. $this->configCache->load($this->configAdapter->load($cat), true);
  55. }
  56. /**
  57. * @brief Get a particular user's config variable given the category name
  58. * ($cat) and a $key.
  59. *
  60. * Get a particular config value from the given category ($cat)
  61. * and the $key from a cached storage either from the $this->configAdapter
  62. * (@see IConfigAdapter ) or from the $this->configCache (@see IConfigCache ).
  63. *
  64. * @param string $cat The category of the configuration value
  65. * @param string $key The configuration key to query
  66. * @param mixed $default_value optional, The value to return if key is not set (default: null)
  67. * @param boolean $refresh optional, If true the config is loaded from the db and not from the cache (default: false)
  68. *
  69. * @return mixed Stored value or null if it does not exist
  70. */
  71. public function get($cat, $key, $default_value = null, $refresh = false)
  72. {
  73. // Return the value of the cache if found and no refresh is forced
  74. if (!$refresh && $this->configCache->has($cat, $key)) {
  75. return $this->configCache->get($cat, $key);
  76. }
  77. // if we don't find the value in the cache and the adapter isn't ready, return the default value
  78. if (!$this->configAdapter->isConnected()) {
  79. return $default_value;
  80. }
  81. // load DB value to cache
  82. $dbvalue = $this->configAdapter->get($cat, $key);
  83. if ($dbvalue !== '!<unset>!') {
  84. $this->configCache->set($cat, $key, $dbvalue);
  85. return $dbvalue;
  86. } else {
  87. return $default_value;
  88. }
  89. }
  90. /**
  91. * @brief Sets a configuration value for system config
  92. *
  93. * Stores a config value ($value) in the category ($cat) under the key ($key)
  94. *
  95. * Note: Please do not store booleans - convert to 0/1 integer values!
  96. *
  97. * @param string $cat The category of the configuration value
  98. * @param string $key The configuration key to set
  99. * @param mixed $value The value to store
  100. *
  101. * @return bool Operation success
  102. */
  103. public function set($cat, $key, $value)
  104. {
  105. // set the cache first
  106. $cached = $this->configCache->set($cat, $key, $value);
  107. // If there is no connected adapter, we're finished
  108. if (!$this->configAdapter->isConnected()) {
  109. return $cached;
  110. }
  111. $stored = $this->configAdapter->set($cat, $key, $value);
  112. return $cached && $stored;
  113. }
  114. /**
  115. * @brief Deletes the given key from the system configuration.
  116. *
  117. * Removes the configured value from the stored cache in $this->configCache
  118. * (@see ConfigCache ) and removes it from the database (@see IConfigAdapter ).
  119. *
  120. * @param string $cat The category of the configuration value
  121. * @param string $key The configuration key to delete
  122. *
  123. * @return bool
  124. */
  125. public function delete($cat, $key)
  126. {
  127. $cacheRemoved = $this->configCache->delete($cat, $key);
  128. if (!$this->configAdapter->isConnected()) {
  129. return $cacheRemoved;
  130. }
  131. $storeRemoved = $this->configAdapter->delete($cat, $key);
  132. return $cacheRemoved || $storeRemoved;
  133. }
  134. }