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.

PreloadConfigAdapter.php 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Friendica\Core\Config\Adapter;
  3. use Friendica\Database\DBA;
  4. /**
  5. * Preload Configuration Adapter
  6. *
  7. * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
  8. *
  9. * @author Hypolite Petovan <hypolite@mrpetovan.com>
  10. */
  11. class PreloadConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter
  12. {
  13. private $config_loaded = false;
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public function load($cat = 'config')
  18. {
  19. $return = [];
  20. if (!$this->isConnected()) {
  21. return $return;
  22. }
  23. if ($this->config_loaded) {
  24. return $return;
  25. }
  26. $configs = DBA::select('config', ['cat', 'v', 'k']);
  27. while ($config = DBA::fetch($configs)) {
  28. $return[$config['k']] = $config['v'];
  29. }
  30. DBA::close($configs);
  31. $this->config_loaded = true;
  32. return [$cat => $return];
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function get($cat, $key)
  38. {
  39. if (!$this->isConnected()) {
  40. return null;
  41. }
  42. $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
  43. if (DBA::isResult($config)) {
  44. // manage array value
  45. $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
  46. return $value;
  47. } else {
  48. return '!<unset>!';
  49. }
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function set($cat, $key, $value)
  55. {
  56. if (!$this->isConnected()) {
  57. return false;
  58. }
  59. // We store our setting values as strings.
  60. // So we have to do the conversion here so that the compare below works.
  61. // The exception are array values.
  62. $compare_value = !is_array($value) ? (string)$value : $value;
  63. if ($this->get($cat, $key) === $compare_value) {
  64. return true;
  65. }
  66. // manage array value
  67. $dbvalue = is_array($value) ? serialize($value) : $value;
  68. $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
  69. return $result;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function delete($cat, $key)
  75. {
  76. if (!$this->isConnected()) {
  77. return false;
  78. }
  79. $result = DBA::delete('config', ['cat' => $cat, 'k' => $key]);
  80. return $result;
  81. }
  82. }