ConfigLister.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace Drupal\config_update;
  3. use Drupal\Core\Config\ExtensionInstallStorage;
  4. use Drupal\Core\Config\StorageInterface;
  5. use Drupal\Core\Entity\EntityTypeManagerInterface;
  6. use Drupal\Core\Site\Settings;
  7. use Drupal\Core\Extension\Extension;
  8. /**
  9. * Provides methods related to config listing.
  10. */
  11. class ConfigLister implements ConfigListInterface {
  12. /**
  13. * List of current config entity types, keyed by prefix.
  14. *
  15. * This is not set up until ConfigLister::listTypes() has been called.
  16. *
  17. * @var string[]
  18. */
  19. protected $typesByPrefix = [];
  20. /**
  21. * List of current config entity type definitions, keyed by entity type.
  22. *
  23. * This is not set up until ConfigLister::listTypes() has been called.
  24. *
  25. * @var \Drupal\Core\Entity\EntityTypeInterface[]
  26. */
  27. protected $definitions = [];
  28. /**
  29. * The entity manager.
  30. *
  31. * @var \Drupal\Core\Entity\EntityManagerInterface
  32. */
  33. protected $entityManager;
  34. /**
  35. * The active config storage.
  36. *
  37. * @var \Drupal\Core\Config\StorageInterface
  38. */
  39. protected $activeConfigStorage;
  40. /**
  41. * The extension config storage.
  42. *
  43. * @var \Drupal\Core\Config\ExtensionInstallStorage
  44. */
  45. protected $extensionConfigStorage;
  46. /**
  47. * The extension config storage for optional config.
  48. *
  49. * @var \Drupal\Core\Config\ExtensionInstallStorage
  50. */
  51. protected $extensionOptionalConfigStorage;
  52. /**
  53. * Constructs a ConfigLister.
  54. *
  55. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
  56. * The entity manager.
  57. * @param \Drupal\Core\Config\StorageInterface $active_config_storage
  58. * The active config storage.
  59. * @param \Drupal\Core\Config\ExtensionInstallStorage $extension_config_storage
  60. * The extension config storage.
  61. * @param \Drupal\Core\Config\ExtensionInstallStorage $extension_optional_config_storage
  62. * The extension config storage for optional config items.
  63. */
  64. public function __construct(EntityTypeManagerInterface $entity_manager, StorageInterface $active_config_storage, ExtensionInstallStorage $extension_config_storage, ExtensionInstallStorage $extension_optional_config_storage) {
  65. $this->entityManager = $entity_manager;
  66. $this->activeConfigStorage = $active_config_storage;
  67. $this->extensionConfigStorage = $extension_config_storage;
  68. $this->extensionOptionalConfigStorage = $extension_optional_config_storage;
  69. }
  70. /**
  71. * Sets up and returns the entity definitions list.
  72. */
  73. public function listTypes() {
  74. if (count($this->definitions)) {
  75. return $this->definitions;
  76. }
  77. foreach ($this->entityManager->getDefinitions() as $entity_type => $definition) {
  78. if ($definition->isSubclassOf('Drupal\Core\Config\Entity\ConfigEntityInterface')) {
  79. $this->definitions[$entity_type] = $definition;
  80. $prefix = $definition->getConfigPrefix();
  81. $this->typesByPrefix[$prefix] = $entity_type;
  82. }
  83. }
  84. ksort($this->definitions);
  85. return $this->definitions;
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function getType($name) {
  91. $definitions = $this->listTypes();
  92. return isset($definitions[$name]) ? $definitions[$name] : NULL;
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function getTypeByPrefix($prefix) {
  98. $definitions = $this->listTypes();
  99. return isset($this->typesByPrefix[$prefix]) ? $definitions[$this->typesByPrefix[$prefix]] : NULL;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function getTypeNameByConfigName($name) {
  105. $definitions = $this->listTypes();
  106. foreach ($this->typesByPrefix as $prefix => $entity_type) {
  107. if (strpos($name, $prefix) === 0) {
  108. return $entity_type;
  109. }
  110. }
  111. return NULL;
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function listConfig($list_type, $name) {
  117. $active_list = [];
  118. $install_list = [];
  119. $optional_list = [];
  120. $definitions = $this->listTypes();
  121. switch ($list_type) {
  122. case 'type':
  123. if ($name == 'system.all') {
  124. $active_list = $this->activeConfigStorage->listAll();
  125. $install_list = $this->extensionConfigStorage->listAll();
  126. $optional_list = $this->extensionOptionalConfigStorage->listAll();
  127. }
  128. elseif ($name == 'system.simple') {
  129. // Listing is done by prefixes, and simple config doesn't have one.
  130. // So list all and filter out all known prefixes.
  131. $active_list = $this->omitKnownPrefixes($this->activeConfigStorage->listAll());
  132. $install_list = $this->omitKnownPrefixes($this->extensionConfigStorage->listAll());
  133. $optional_list = $this->omitKnownPrefixes($this->extensionOptionalConfigStorage->listAll());
  134. }
  135. elseif (isset($this->definitions[$name])) {
  136. $definition = $this->definitions[$name];
  137. $prefix = $definition->getConfigPrefix();
  138. $active_list = $this->activeConfigStorage->listAll($prefix);
  139. $install_list = $this->extensionConfigStorage->listAll($prefix);
  140. $optional_list = $this->extensionOptionalConfigStorage->listAll($prefix);
  141. }
  142. break;
  143. case 'profile':
  144. $name = Settings::get('install_profile');
  145. // Intentional fall-through here to the 'module' or 'theme' case.
  146. case 'module':
  147. case 'theme':
  148. $active_list = $this->activeConfigStorage->listAll();
  149. $install_list = $this->listProvidedItems($list_type, $name);
  150. $optional_list = $this->listProvidedItems($list_type, $name, TRUE);
  151. break;
  152. }
  153. return [$active_list, $install_list, $optional_list];
  154. }
  155. /**
  156. * Returns a list of the install storage items for an extension.
  157. *
  158. * @param string $type
  159. * Type of extension ('module', etc.).
  160. * @param string $name
  161. * Machine name of extension.
  162. * @param bool $do_optional
  163. * FALSE (default) to list config/install items, TRUE to list
  164. * config/optional items.
  165. *
  166. * @return string[]
  167. * List of config items provided by this extension.
  168. */
  169. protected function listProvidedItems($type, $name, $do_optional = FALSE) {
  170. $pathname = drupal_get_filename($type, $name);
  171. $component = new Extension(\Drupal::root(), $type, $pathname);
  172. if ($do_optional) {
  173. $names = $this->extensionOptionalConfigStorage->getComponentNames([$component]);
  174. }
  175. else {
  176. $names = $this->extensionConfigStorage->getComponentNames([$component]);
  177. }
  178. return array_keys($names);
  179. }
  180. /**
  181. * Omits config with known prefixes from a list of config names.
  182. */
  183. protected function omitKnownPrefixes($list) {
  184. $prefixes = array_keys($this->typesByPrefix);
  185. $list = array_combine($list, $list);
  186. foreach ($list as $name) {
  187. foreach ($prefixes as $prefix) {
  188. if (strpos($name, $prefix) === 0) {
  189. unset($list[$name]);
  190. }
  191. }
  192. }
  193. return array_values($list);
  194. }
  195. }