State.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace Drupal\Core\State;
  3. use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
  4. /**
  5. * Provides the state system using a key value store.
  6. */
  7. class State implements StateInterface {
  8. /**
  9. * The key value store to use.
  10. *
  11. * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
  12. */
  13. protected $keyValueStore;
  14. /**
  15. * Static state cache.
  16. *
  17. * @var array
  18. */
  19. protected $cache = [];
  20. /**
  21. * Constructs a State object.
  22. *
  23. * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory
  24. * The key value store to use.
  25. */
  26. public function __construct(KeyValueFactoryInterface $key_value_factory) {
  27. $this->keyValueStore = $key_value_factory->get('state');
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function get($key, $default = NULL) {
  33. $values = $this->getMultiple([$key]);
  34. return isset($values[$key]) ? $values[$key] : $default;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function getMultiple(array $keys) {
  40. $values = [];
  41. $load = [];
  42. foreach ($keys as $key) {
  43. // Check if we have a value in the cache.
  44. if (isset($this->cache[$key])) {
  45. $values[$key] = $this->cache[$key];
  46. }
  47. // Load the value if we don't have an explicit NULL value.
  48. elseif (!array_key_exists($key, $this->cache)) {
  49. $load[] = $key;
  50. }
  51. }
  52. if ($load) {
  53. $loaded_values = $this->keyValueStore->getMultiple($load);
  54. foreach ($load as $key) {
  55. // If we find a value, even one that is NULL, add it to the cache and
  56. // return it.
  57. if (isset($loaded_values[$key]) || array_key_exists($key, $loaded_values)) {
  58. $values[$key] = $loaded_values[$key];
  59. $this->cache[$key] = $loaded_values[$key];
  60. }
  61. else {
  62. $this->cache[$key] = NULL;
  63. }
  64. }
  65. }
  66. return $values;
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function set($key, $value) {
  72. $this->cache[$key] = $value;
  73. $this->keyValueStore->set($key, $value);
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function setMultiple(array $data) {
  79. foreach ($data as $key => $value) {
  80. $this->cache[$key] = $value;
  81. }
  82. $this->keyValueStore->setMultiple($data);
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function delete($key) {
  88. $this->deleteMultiple([$key]);
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function deleteMultiple(array $keys) {
  94. foreach ($keys as $key) {
  95. unset($this->cache[$key]);
  96. }
  97. $this->keyValueStore->deleteMultiple($keys);
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function resetCache() {
  103. $this->cache = [];
  104. }
  105. }