MemoryStorage.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace Drupal\Core\Config;
  3. /**
  4. * Provides an in memory configuration storage.
  5. */
  6. class MemoryStorage implements StorageInterface {
  7. /**
  8. * The configuration, an object shared by reference across collections.
  9. *
  10. * @var \ArrayAccess
  11. */
  12. protected $config;
  13. /**
  14. * The storage collection.
  15. *
  16. * @var string
  17. */
  18. protected $collection;
  19. /**
  20. * Constructs a new MemoryStorage.
  21. *
  22. * @param string $collection
  23. * (optional) The collection to store configuration in. Defaults to the
  24. * default collection.
  25. */
  26. public function __construct($collection = StorageInterface::DEFAULT_COLLECTION) {
  27. $this->collection = $collection;
  28. $this->config = new \ArrayObject();
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function exists($name) {
  34. return isset($this->config[$this->collection][$name]);
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function read($name) {
  40. if ($this->exists($name)) {
  41. return $this->config[$this->collection][$name];
  42. }
  43. return FALSE;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function readMultiple(array $names) {
  49. return array_intersect_key($this->config[$this->collection], array_flip($names));
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function write($name, array $data) {
  55. $this->config[$this->collection][$name] = $data;
  56. return TRUE;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function delete($name) {
  62. if (isset($this->config[$this->collection][$name])) {
  63. unset($this->config[$this->collection][$name]);
  64. // Remove the collection if it is empty.
  65. if (empty($this->config[$this->collection])) {
  66. $this->config->offsetUnset($this->collection);
  67. }
  68. return TRUE;
  69. }
  70. return FALSE;
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function rename($name, $new_name) {
  76. if (!$this->exists($name)) {
  77. return FALSE;
  78. }
  79. $this->config[$this->collection][$new_name] = $this->config[$this->collection][$name];
  80. unset($this->config[$this->collection][$name]);
  81. return TRUE;
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function encode($data) {
  87. return $data;
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function decode($raw) {
  93. return $raw;
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function listAll($prefix = '') {
  99. if (empty($this->config[$this->collection])) {
  100. // If the collection is empty no keys are set.
  101. return [];
  102. }
  103. $names = array_keys($this->config[$this->collection]);
  104. if ($prefix !== '') {
  105. $names = array_filter($names, function ($name) use ($prefix) {
  106. return strpos($name, $prefix) === 0;
  107. });
  108. }
  109. return $names;
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function deleteAll($prefix = '') {
  115. if (!$this->config->offsetExists($this->collection)) {
  116. // There's nothing to delete.
  117. return FALSE;
  118. }
  119. if ($prefix === '') {
  120. $this->config->offsetUnset($this->collection);
  121. return TRUE;
  122. }
  123. $success = FALSE;
  124. foreach (array_keys($this->config[$this->collection]) as $name) {
  125. if (strpos($name, $prefix) === 0) {
  126. $success = TRUE;
  127. unset($this->config[$this->collection][$name]);
  128. }
  129. }
  130. // Remove the collection if it is empty.
  131. if (empty($this->config[$this->collection])) {
  132. $this->config->offsetUnset($this->collection);
  133. }
  134. return $success;
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function createCollection($collection) {
  140. $collection = new static($collection);
  141. $collection->config = $this->config;
  142. return $collection;
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function getAllCollectionNames() {
  148. $collection_names = [];
  149. foreach ($this->config as $collection_name => $data) {
  150. // Exclude the default collection and empty collections.
  151. if ($collection_name !== StorageInterface::DEFAULT_COLLECTION && !empty($data)) {
  152. $collection_names[] = $collection_name;
  153. }
  154. }
  155. sort($collection_names);
  156. return $collection_names;
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function getCollectionName() {
  162. return $this->collection;
  163. }
  164. }