FeaturesExtensionStorages.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace Drupal\features;
  3. use Drupal\Core\Config\InstallStorage;
  4. use Drupal\Core\Config\StorageInterface;
  5. use Drupal\Core\Extension\Extension;
  6. /**
  7. * Wraps FeaturesInstallStorage to support multiple configuration
  8. * directories.
  9. */
  10. class FeaturesExtensionStorages implements FeaturesExtensionStoragesInterface {
  11. /**
  12. * The target storage.
  13. *
  14. * @var \Drupal\Core\Config\StorageInterface
  15. */
  16. protected $configStorage;
  17. /**
  18. * The extension storages.
  19. *
  20. * @var \Drupal\Core\Config\StorageInterface[]
  21. */
  22. protected $extensionStorages;
  23. /**
  24. * Configuration provided by extension storages.
  25. *
  26. * @var array
  27. */
  28. protected $configurationLists;
  29. /**
  30. * Constructs a new FeaturesExtensionStorages object.
  31. *
  32. * @param \Drupal\Core\Config\StorageInterface $config_storage
  33. * The configuration storage.
  34. */
  35. public function __construct(StorageInterface $config_storage) {
  36. $this->configStorage = $config_storage;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function getExtensionStorages() {
  42. return $this->extensionStorages;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function addStorage($directory = InstallStorage::CONFIG_INSTALL_DIRECTORY) {
  48. $this->extensionStorages[$directory] = new FeaturesInstallStorage($this->configStorage, $directory);
  49. $this->reset();
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function read($name) {
  55. $list = $this->listAllByDirectory('');
  56. if (isset($list[$name])) {
  57. $directory = $list[$name];
  58. return $this->extensionStorages[$directory]->read($name);
  59. }
  60. return FALSE;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function listAll($prefix = '') {
  66. return array_keys($this->listAllByDirectory($prefix));
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function listExtensionConfig(Extension $extension) {
  72. $extension_config = [];
  73. foreach ($this->extensionStorages as $directory => $extension_storage) {
  74. $extension_config = array_merge($extension_config, array_keys($extension_storage->getComponentNames([
  75. $extension->getName() => $extension,
  76. ])));
  77. }
  78. return $extension_config;
  79. }
  80. /**
  81. * Resets packages and configuration assignment.
  82. */
  83. protected function reset() {
  84. $this->configurationLists = [];
  85. }
  86. /**
  87. * Returns a list of all configuration available from extensions.
  88. *
  89. * @param string $prefix
  90. * (optional) The prefix to search for. If omitted, all configuration object
  91. * names that exist are returned.
  92. *
  93. * @return array
  94. * An array with configuration item names as keys and configuration
  95. * directories as values.
  96. */
  97. protected function listAllByDirectory($prefix = '') {
  98. if (!isset($this->configurationLists[$prefix])) {
  99. $this->configurationLists[$prefix] = [];
  100. foreach ($this->extensionStorages as $directory => $extension_storage) {
  101. $this->configurationLists[$prefix] += array_fill_keys($extension_storage->listAll($prefix), $directory);
  102. }
  103. }
  104. return $this->configurationLists[$prefix];
  105. }
  106. }