Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
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.

JITConfigAdapter.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Friendica\Core\Config\Adapter;
  3. use Friendica\Database\DBA;
  4. /**
  5. * JustInTime Configuration Adapter
  6. *
  7. * Default Config Adapter. Provides the best performance for pages loading few configuration variables.
  8. *
  9. * @author Hypolite Petovan <hypolite@mrpetovan.com>
  10. */
  11. class JITConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter
  12. {
  13. private $in_db;
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public function load($cat = "config")
  18. {
  19. $return = [];
  20. if (!$this->isConnected()) {
  21. return $return;
  22. }
  23. // We don't preload "system" anymore.
  24. // This reduces the number of database reads a lot.
  25. if ($cat === 'system') {
  26. return $return;
  27. }
  28. $configs = DBA::select('config', ['v', 'k'], ['cat' => $cat]);
  29. while ($config = DBA::fetch($configs)) {
  30. $key = $config['k'];
  31. $return[$key] = $config['v'];
  32. $this->in_db[$cat][$key] = true;
  33. }
  34. DBA::close($configs);
  35. return [$cat => $config];
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function get($cat, $key)
  41. {
  42. if (!$this->isConnected()) {
  43. return '!<unset>!';
  44. }
  45. $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
  46. if (DBA::isResult($config)) {
  47. // manage array value
  48. $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
  49. $this->in_db[$cat][$key] = true;
  50. return $value;
  51. } else {
  52. $this->in_db[$cat][$key] = false;
  53. return '!<unset>!';
  54. }
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function set($cat, $key, $value)
  60. {
  61. if (!$this->isConnected()) {
  62. return false;
  63. }
  64. // We store our setting values in a string variable.
  65. // So we have to do the conversion here so that the compare below works.
  66. // The exception are array values.
  67. $dbvalue = (!is_array($value) ? (string)$value : $value);
  68. $stored = $this->get($cat, $key);
  69. if (!isset($this->in_db[$cat])) {
  70. $this->in_db[$cat] = [];
  71. }
  72. if (!isset($this->in_db[$cat][$key])) {
  73. $this->in_db[$cat][$key] = false;
  74. }
  75. if (($stored === $dbvalue) && $this->in_db[$cat][$key]) {
  76. return true;
  77. }
  78. // manage array value
  79. $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
  80. $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
  81. $this->in_db[$cat][$key] = $result;
  82. return $result;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function delete($cat, $key)
  88. {
  89. if (!$this->isConnected()) {
  90. return false;
  91. }
  92. if (isset($this->cache[$cat][$key])) {
  93. unset($this->in_db[$cat][$key]);
  94. }
  95. $result = DBA::delete('config', ['cat' => $cat, 'k' => $key]);
  96. return $result;
  97. }
  98. }