ConfigCollectionInfo.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Drupal\Core\Config;
  3. use Symfony\Component\EventDispatcher\Event;
  4. /**
  5. * Gets information on all the possible configuration collections.
  6. */
  7. class ConfigCollectionInfo extends Event {
  8. /**
  9. * Configuration collection information keyed by collection name.
  10. *
  11. * The value is either the configuration factory override that is responsible
  12. * for the collection or null if there is not one.
  13. *
  14. * @var array
  15. */
  16. protected $collections = [];
  17. /**
  18. * Adds a collection to the list of possible collections.
  19. *
  20. * @param string $collection
  21. * Collection name to add.
  22. * @param \Drupal\Core\Config\ConfigFactoryOverrideInterface $override_service
  23. * (optional) The configuration factory override service responsible for the
  24. * collection.
  25. *
  26. * @throws \InvalidArgumentException
  27. * Exception thrown if $collection is equal to
  28. * \Drupal\Core\Config\StorageInterface::DEFAULT_COLLECTION.
  29. */
  30. public function addCollection($collection, ConfigFactoryOverrideInterface $override_service = NULL) {
  31. if ($collection == StorageInterface::DEFAULT_COLLECTION) {
  32. throw new \InvalidArgumentException('Can not add the default collection to the ConfigCollectionInfo object');
  33. }
  34. $this->collections[$collection] = $override_service;
  35. }
  36. /**
  37. * Gets the list of possible collection names.
  38. *
  39. * @param bool $include_default
  40. * (Optional) Include the default collection. Defaults to TRUE.
  41. *
  42. * @return array
  43. * The list of possible collection names.
  44. */
  45. public function getCollectionNames($include_default = TRUE) {
  46. $collection_names = array_keys($this->collections);
  47. sort($collection_names);
  48. if ($include_default) {
  49. array_unshift($collection_names, StorageInterface::DEFAULT_COLLECTION);
  50. }
  51. return $collection_names;
  52. }
  53. /**
  54. * Gets the config factory override service responsible for the collection.
  55. *
  56. * @param string $collection
  57. * The configuration collection.
  58. *
  59. * @return \Drupal\Core\Config\ConfigFactoryOverrideInterface|null
  60. * The override service responsible for the collection if one exists. NULL
  61. * if not.
  62. */
  63. public function getOverrideService($collection) {
  64. return isset($this->collections[$collection]) ? $this->collections[$collection] : NULL;
  65. }
  66. }