ConfigLister.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. // Return list if it has already been calculated.
  75. if (count($this->definitions)) {
  76. return $this->definitions;
  77. }
  78. // Calculate and return the list.
  79. foreach ($this->entityManager->getDefinitions() as $entity_type => $definition) {
  80. if ($definition->entityClassImplements('Drupal\Core\Config\Entity\ConfigEntityInterface')) {
  81. $this->definitions[$entity_type] = $definition;
  82. $prefix = $definition->getConfigPrefix();
  83. $this->typesByPrefix[$prefix] = $entity_type;
  84. }
  85. }
  86. ksort($this->definitions);
  87. return $this->definitions;
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function getType($name) {
  93. $definitions = $this->listTypes();
  94. return isset($definitions[$name]) ? $definitions[$name] : NULL;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function getTypeByPrefix($prefix) {
  100. $definitions = $this->listTypes();
  101. return isset($this->typesByPrefix[$prefix]) ? $definitions[$this->typesByPrefix[$prefix]] : NULL;
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function getTypeNameByConfigName($name) {
  107. $definitions = $this->listTypes();
  108. foreach ($this->typesByPrefix as $prefix => $entity_type) {
  109. if (strpos($name, $prefix . '.') === 0) {
  110. return $entity_type;
  111. }
  112. }
  113. return NULL;
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function listConfig($list_type, $name) {
  119. $active_list = [];
  120. $install_list = [];
  121. $optional_list = [];
  122. $definitions = $this->listTypes();
  123. switch ($list_type) {
  124. case 'type':
  125. if ($name == 'system.all') {
  126. $active_list = $this->activeConfigStorage->listAll();
  127. $install_list = $this->extensionConfigStorage->listAll();
  128. $optional_list = $this->extensionOptionalConfigStorage->listAll();
  129. }
  130. elseif ($name == 'system.simple') {
  131. // Listing is done by prefixes, and simple config doesn't have one.
  132. // So list all and filter out all known prefixes.
  133. $active_list = $this->omitKnownPrefixes($this->activeConfigStorage->listAll());
  134. $install_list = $this->omitKnownPrefixes($this->extensionConfigStorage->listAll());
  135. $optional_list = $this->omitKnownPrefixes($this->extensionOptionalConfigStorage->listAll());
  136. }
  137. elseif (isset($this->definitions[$name])) {
  138. $definition = $this->definitions[$name];
  139. $prefix = $definition->getConfigPrefix();
  140. $active_list = $this->activeConfigStorage->listAll($prefix);
  141. $install_list = $this->extensionConfigStorage->listAll($prefix);
  142. $optional_list = $this->extensionOptionalConfigStorage->listAll($prefix);
  143. }
  144. break;
  145. case 'profile':
  146. $name = $this->getProfileName();
  147. // Intentional fall-through here to the 'module' or 'theme' case.
  148. case 'module':
  149. case 'theme':
  150. $active_list = $this->activeConfigStorage->listAll();
  151. $install_list = $this->listProvidedItems($list_type, $name);
  152. $optional_list = $this->listProvidedItems($list_type, $name, TRUE);
  153. break;
  154. }
  155. // This only seems to be a problem in unit tests, where a mock object
  156. // is returning NULL instead of an empy array for some reason.
  157. if (!is_array($optional_list)) {
  158. $optional_list = [];
  159. }
  160. return [$active_list, $install_list, $optional_list];
  161. }
  162. /**
  163. * Returns a list of the install storage items for an extension.
  164. *
  165. * @param string $type
  166. * Type of extension ('module', etc.).
  167. * @param string $name
  168. * Machine name of extension.
  169. * @param bool $do_optional
  170. * FALSE (default) to list config/install items, TRUE to list
  171. * config/optional items.
  172. *
  173. * @return string[]
  174. * List of config items provided by this extension.
  175. */
  176. protected function listProvidedItems($type, $name, $do_optional = FALSE) {
  177. $pathname = drupal_get_filename($type, $name);
  178. $component = new Extension(\Drupal::root(), $type, $pathname);
  179. if ($do_optional) {
  180. $names = $this->extensionOptionalConfigStorage->getComponentNames([$component]);
  181. }
  182. else {
  183. $names = $this->extensionConfigStorage->getComponentNames([$component]);
  184. }
  185. return array_keys($names);
  186. }
  187. /**
  188. * Omits config with known prefixes from a list of config names.
  189. */
  190. protected function omitKnownPrefixes($list) {
  191. $prefixes = array_keys($this->typesByPrefix);
  192. $list = array_combine($list, $list);
  193. foreach ($list as $name) {
  194. foreach ($prefixes as $prefix) {
  195. if (strpos($name, $prefix . '.') === 0) {
  196. unset($list[$name]);
  197. }
  198. }
  199. }
  200. return array_values($list);
  201. }
  202. /**
  203. * Returns the name of the install profile.
  204. *
  205. * For backwards compatibility with pre/post 8.3.x, tries to get it from
  206. * either configuration or settings.
  207. *
  208. * @return string
  209. * The name of the install profile.
  210. */
  211. protected function getProfileName() {
  212. // Code adapted from DrupalKernel::getInstalProfile() in Core.
  213. // In Core 8.3.x or later, read from config.
  214. $config = $this->activeConfigStorage->read('core.extension');
  215. if (!empty($config['profile'])) {
  216. return $config['profile'];
  217. }
  218. else {
  219. // If system_update_8300() has not yet run, use settings.
  220. return Settings::get('install_profile');
  221. }
  222. }
  223. }