ConfigCollectionInfo.php 2.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
<?php

namespace Drupal\Core\Config;

use Symfony\Component\EventDispatcher\Event;

/**
 * Gets information on all the possible configuration collections.
 */
class ConfigCollectionInfo extends Event {

  /**
   * Configuration collection information keyed by collection name.
   *
   * The value is either the configuration factory override that is responsible
   * for the collection or null if there is not one.
   *
   * @var array
   */
  protected $collections = array();

  /**
   * Adds a collection to the list of possible collections.
   *
   * @param string $collection
   *   Collection name to add.
27
   * @param \Drupal\Core\Config\ConfigFactoryOverrideInterface $override_service
28 29 30 31 32
   *   (optional) The configuration factory override service responsible for the
   *   collection.
   *
   * @throws \InvalidArgumentException
   *   Exception thrown if $collection is equal to
33
   *   \Drupal\Core\Config\StorageInterface::DEFAULT_COLLECTION.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
   */
  public function addCollection($collection, ConfigFactoryOverrideInterface $override_service = NULL) {
    if ($collection == StorageInterface::DEFAULT_COLLECTION) {
      throw new \InvalidArgumentException('Can not add the default collection to the ConfigCollectionInfo object');
    }
    $this->collections[$collection] = $override_service;
  }

  /**
   * Gets the list of possible collection names.
   *
   * @param bool $include_default
   *   (Optional) Include the default collection. Defaults to TRUE.
   *
   * @return array
   *   The list of possible collection names.
   */
  public function getCollectionNames($include_default = TRUE) {
    $collection_names = array_keys($this->collections);
    sort($collection_names);
    if ($include_default) {
      array_unshift($collection_names, StorageInterface::DEFAULT_COLLECTION);
    }
    return $collection_names;
  }

  /**
   * Gets the config factory override service responsible for the collection.
   *
   * @param string $collection
   *   The configuration collection.
   *
66
   * @return \Drupal\Core\Config\ConfigFactoryOverrideInterface|null
67 68 69 70 71 72 73 74
   *   The override service responsible for the collection if one exists. NULL
   *   if not.
   */
  public function getOverrideService($collection) {
    return isset($this->collections[$collection]) ? $this->collections[$collection] : NULL;
  }

}