Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there)
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

PConfig.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * User Configuration Class
  4. *
  5. * @file include/Core/PConfig.php
  6. *
  7. * @brief Contains the class with methods for user configuration
  8. */
  9. namespace Friendica\Core;
  10. use Friendica\App;
  11. use Friendica\BaseObject;
  12. require_once 'include/dba.php';
  13. /**
  14. * @brief Management of user configuration storage
  15. * Note:
  16. * Please do not store booleans - convert to 0/1 integer values
  17. * The PConfig::get() functions return boolean false for keys that are unset,
  18. * and this could lead to subtle bugs.
  19. */
  20. class PConfig extends BaseObject
  21. {
  22. /**
  23. * @var Friendica\Core\Config\IPConfigAdapter
  24. */
  25. private static $adapter = null;
  26. public static function init($uid)
  27. {
  28. $a = self::getApp();
  29. // Database isn't ready or populated yet
  30. if (!$a->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
  31. return;
  32. }
  33. if ($a->getConfigValue('system', 'config_adapter') == 'preload') {
  34. self::$adapter = new Config\PreloadPConfigAdapter($uid);
  35. } else {
  36. self::$adapter = new Config\JITPConfigAdapter($uid);
  37. }
  38. }
  39. /**
  40. * @brief Loads all configuration values of a user's config family into a cached storage.
  41. *
  42. * All configuration values of the given user are stored in global cache
  43. * which is available under the global variable $a->config[$uid].
  44. *
  45. * @param string $uid The user_id
  46. * @param string $family The category of the configuration value
  47. *
  48. * @return void
  49. */
  50. public static function load($uid, $family)
  51. {
  52. // Database isn't ready or populated yet
  53. if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
  54. return;
  55. }
  56. if (empty(self::$adapter)) {
  57. self::init($uid);
  58. }
  59. self::$adapter->load($uid, $family);
  60. }
  61. /**
  62. * @brief Get a particular user's config variable given the category name
  63. * ($family) and a key.
  64. *
  65. * Get a particular user's config value from the given category ($family)
  66. * and the $key from a cached storage in $a->config[$uid].
  67. *
  68. * @param string $uid The user_id
  69. * @param string $family The category of the configuration value
  70. * @param string $key The configuration key to query
  71. * @param mixed $default_value optional, The value to return if key is not set (default: null)
  72. * @param boolean $refresh optional, If true the config is loaded from the db and not from the cache (default: false)
  73. *
  74. * @return mixed Stored value or null if it does not exist
  75. */
  76. public static function get($uid, $family, $key, $default_value = null, $refresh = false)
  77. {
  78. // Database isn't ready or populated yet
  79. if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
  80. return;
  81. }
  82. if (empty(self::$adapter)) {
  83. self::init($uid);
  84. }
  85. return self::$adapter->get($uid, $family, $key, $default_value, $refresh);
  86. }
  87. /**
  88. * @brief Sets a configuration value for a user
  89. *
  90. * Stores a config value ($value) in the category ($family) under the key ($key)
  91. * for the user_id $uid.
  92. *
  93. * @note Please do not store booleans - convert to 0/1 integer values!
  94. *
  95. * @param string $uid The user_id
  96. * @param string $family The category of the configuration value
  97. * @param string $key The configuration key to set
  98. * @param string $value The value to store
  99. *
  100. * @return bool Operation success
  101. */
  102. public static function set($uid, $family, $key, $value)
  103. {
  104. // Database isn't ready or populated yet
  105. if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
  106. return false;
  107. }
  108. if (empty(self::$adapter)) {
  109. self::init($uid);
  110. }
  111. return self::$adapter->set($uid, $family, $key, $value);
  112. }
  113. /**
  114. * @brief Deletes the given key from the users's configuration.
  115. *
  116. * Removes the configured value from the stored cache in $a->config[$uid]
  117. * and removes it from the database.
  118. *
  119. * @param string $uid The user_id
  120. * @param string $family The category of the configuration value
  121. * @param string $key The configuration key to delete
  122. *
  123. * @return mixed
  124. */
  125. public static function delete($uid, $family, $key)
  126. {
  127. // Database isn't ready or populated yet
  128. if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
  129. return false;
  130. }
  131. if (empty(self::$adapter)) {
  132. self::init($uid);
  133. }
  134. return self::$adapter->delete($uid, $family, $key);
  135. }
  136. }