StorageBase.php 976 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace Drupal\Core\KeyValueStore;
  3. /**
  4. * Provides a base class for key/value storage implementations.
  5. */
  6. abstract class StorageBase implements KeyValueStoreInterface {
  7. /**
  8. * The name of the collection holding key and value pairs.
  9. *
  10. * @var string
  11. */
  12. protected $collection;
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public function __construct($collection) {
  17. $this->collection = $collection;
  18. }
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function getCollectionName() {
  23. return $this->collection;
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function get($key, $default = NULL) {
  29. $values = $this->getMultiple([$key]);
  30. return isset($values[$key]) ? $values[$key] : $default;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function setMultiple(array $data) {
  36. foreach ($data as $key => $value) {
  37. $this->set($key, $value);
  38. }
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function delete($key) {
  44. $this->deleteMultiple([$key]);
  45. }
  46. }