ManagedStorage.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace Drupal\Core\Config;
  3. /**
  4. * The managed storage defers all the storage method calls to the manager.
  5. *
  6. * The reason for deferring all the method calls is that the storage interface
  7. * is the API but we potentially need to do an expensive transformation before
  8. * the storage can be used so we can't do it in the constructor but we also
  9. * don't know which method is called first.
  10. *
  11. * This class is not meant to be extended and is final to make sure the
  12. * assumptions that the storage is retrieved only once are upheld.
  13. */
  14. final class ManagedStorage implements StorageInterface {
  15. /**
  16. * The decorated storage.
  17. *
  18. * @var \Drupal\Core\Config\StorageInterface
  19. */
  20. protected $storage;
  21. /**
  22. * The storage manager to get the storage to decorate.
  23. *
  24. * @var \Drupal\Core\Config\StorageManagerInterface
  25. */
  26. protected $manager;
  27. /**
  28. * ManagedStorage constructor.
  29. *
  30. * @param \Drupal\Core\Config\StorageManagerInterface $manager
  31. * The storage manager.
  32. */
  33. public function __construct(StorageManagerInterface $manager) {
  34. $this->manager = $manager;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function exists($name) {
  40. return $this->getStorage()->exists($name);
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function read($name) {
  46. return $this->getStorage()->read($name);
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function readMultiple(array $names) {
  52. return $this->getStorage()->readMultiple($names);
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function write($name, array $data) {
  58. return $this->getStorage()->write($name, $data);
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function delete($name) {
  64. return $this->getStorage()->delete($name);
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function rename($name, $new_name) {
  70. return $this->getStorage()->rename($name, $new_name);
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function encode($data) {
  76. return $this->getStorage()->encode($data);
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function decode($raw) {
  82. return $this->getStorage()->decode($raw);
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function listAll($prefix = '') {
  88. return $this->getStorage()->listAll($prefix);
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function deleteAll($prefix = '') {
  94. return $this->getStorage()->deleteAll($prefix);
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function createCollection($collection) {
  100. // We return the collection directly.
  101. // This means that the collection will not be an instance of ManagedStorage
  102. // But this doesn't matter because the storage is retrieved from the
  103. // manager only the first time it is accessed.
  104. return $this->getStorage()->createCollection($collection);
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function getAllCollectionNames() {
  110. return $this->getStorage()->getAllCollectionNames();
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function getCollectionName() {
  116. return $this->getStorage()->getCollectionName();
  117. }
  118. /**
  119. * Get the decorated storage from the manager if necessary.
  120. *
  121. * @return \Drupal\Core\Config\StorageInterface
  122. * The config storage.
  123. */
  124. protected function getStorage() {
  125. // Get the storage from the manager the first time it is needed.
  126. if (!isset($this->storage)) {
  127. $this->storage = $this->manager->getStorage();
  128. }
  129. return $this->storage;
  130. }
  131. }