MemoryStorage.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Drupal\Core\KeyValueStore;
  3. /**
  4. * Defines a default key/value store implementation.
  5. */
  6. class MemoryStorage extends StorageBase {
  7. /**
  8. * The actual storage of key-value pairs.
  9. *
  10. * @var array
  11. */
  12. protected $data = [];
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public function has($key) {
  17. return array_key_exists($key, $this->data);
  18. }
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function get($key, $default = NULL) {
  23. return array_key_exists($key, $this->data) ? $this->data[$key] : $default;
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function getMultiple(array $keys) {
  29. return array_intersect_key($this->data, array_flip($keys));
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function getAll() {
  35. return $this->data;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function set($key, $value) {
  41. $this->data[$key] = $value;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function setIfNotExists($key, $value) {
  47. if (!isset($this->data[$key])) {
  48. $this->data[$key] = $value;
  49. return TRUE;
  50. }
  51. return FALSE;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function setMultiple(array $data) {
  57. $this->data = $data + $this->data;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function rename($key, $new_key) {
  63. $this->data[$new_key] = $this->data[$key];
  64. unset($this->data[$key]);
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function delete($key) {
  70. unset($this->data[$key]);
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function deleteMultiple(array $keys) {
  76. foreach ($keys as $key) {
  77. unset($this->data[$key]);
  78. }
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function deleteAll() {
  84. $this->data = [];
  85. }
  86. }