You are here

class ConfigSyncLister in Configuration Synchronizer 8

Same name and namespace in other branches
  1. 8.2 src/ConfigSyncLister.php \Drupal\config_sync\ConfigSyncLister

Provides methods related to listing configuration changes.

Hierarchy

Expanded class hierarchy of ConfigSyncLister

1 string reference to 'ConfigSyncLister'
config_sync.services.yml in ./config_sync.services.yml
config_sync.services.yml
1 service uses ConfigSyncLister
config_sync.lister in ./config_sync.services.yml
Drupal\config_sync\ConfigSyncLister

File

src/ConfigSyncLister.php, line 17

Namespace

Drupal\config_sync
View source
class ConfigSyncLister implements ConfigSyncListerInterface {
  use StringTranslationTrait;

  /**
   * The configuration collector.
   *
   * @var \Drupal\config_provider\Plugin\ConfigCollectorInterface
   */
  protected $configCollector;

  /**
   * The configuration update lister.
   *
   * @var \Drupal\config_update\ConfigListInterface
   */
  protected $configUpdateLister;

  /**
   * The active configuration storage.
   *
   * @var \Drupal\Core\Config\StorageInterface
   */
  protected $activeStorage;

  /**
   * The snapshot config storage for values from the extension storage.
   *
   * @var \Drupal\Core\Config\StorageInterface
   */
  protected $snapshotExtensionStorage;

  /**
   * The configuration manager.
   *
   * @var \Drupal\Core\Config\ConfigManagerInterface
   */
  protected $configManager;

  /**
   * List of current config entity type labels, keyed by entity type.
   *
   * This is not set up until ConfigLister::listTypes() has been called.
   *
   * @var array
   */
  protected $configTypes = [];

  /**
   * Constructs a ConfigSyncLister object.
   *
   * @param \Drupal\config_provider\Plugin\ConfigCollectorInterface $config_collector
   *   The config collector.
   * @param \Drupal\config_update\ConfigListInterface $config_update_lister
   *   The configuration update lister.
   * @param \Drupal\Core\Config\StorageInterface $active_storage
   *   The active storage.
   * @param \Drupal\Core\Config\StorageInterface $snapshot_extension_storage
   *   The snapshot storage for the items from the extension storage.
   * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
   *   The configuration manager.
   */
  public function __construct(ConfigCollectorInterface $config_collector, ConfigUpdateListerInterface $config_update_lister, StorageInterface $active_storage, StorageInterface $snapshot_extension_storage, ConfigManagerInterface $config_manager) {
    $this->configCollector = $config_collector;
    $this->configUpdateLister = $config_update_lister;
    $this->activeStorage = $active_storage;
    $this->snapshotExtensionStorage = $snapshot_extension_storage;
    $this->configManager = $config_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function getExtensionChangelists(array $extension_names = []) {
    $changelist = [];

    // If no extensions were specified, use all installed extensions.
    if (empty($extension_names)) {
      $installed_extensions = $this->activeStorage
        ->read('core.extension');
      foreach (array(
        'module',
        'theme',
      ) as $type) {
        if (!empty($installed_extensions[$type])) {
          $extension_names[$type] = array_keys($installed_extensions[$type]);
        }
      }
    }
    foreach ($extension_names as $type => $names) {
      foreach ($names as $name) {
        if ($extension_changelist = $this
          ->getExtensionChangelist($type, $name)) {
          if (!isset($changelist[$type])) {
            $changelist[$type] = [];
          }
          $changelist[$type][$name] = $extension_changelist;
        }
      }
    }
    return $changelist;
  }

  /**
   * {@inheritdoc}
   */
  public function getExtensionChangelist($type, $name) {
    $pathname = $this
      ->drupalGetFilename($type, $name);
    $extension = new Extension(\Drupal::root(), $type, $pathname);
    $extensions = [
      $name => $extension,
    ];

    /* @var \Drupal\config_provider\InMemoryStorage $installable_config */
    $installable_config = $this->configCollector
      ->getInstallableConfig($extensions);

    // Set up a storage comparer.
    $storage_comparer = new StorageComparer($installable_config, $this->snapshotExtensionStorage, $this->configManager);
    $storage_comparer
      ->createChangelist();
    $changelist = $storage_comparer
      ->getChangelist();

    // We're only concerned with create and update lists.
    unset($changelist['delete']);
    $changelist = array_filter($changelist);
    $return = [];

    // Convert the changelist into a format that includes the item label.
    foreach ($changelist as $change_type => $item_names) {
      foreach ($item_names as $item_name) {

        // Figure out what type of config it is, and get the ID.
        $config_type = $this->configUpdateLister
          ->getTypeNameByConfigName($item_name);
        if (!$config_type) {

          // This is simple config.
          $label = $item_name;
        }
        else {
          $config = $installable_config
            ->read($item_name);
          $definition = $this->configUpdateLister
            ->getType($config_type);
          $key = $definition
            ->getKey('label') ?: $definition
            ->getKey('id');
          $label = $config[$key];
        }
        $return[$change_type][$item_name] = $label;
      }
    }
    return $return;
  }

  /**
   * {@inheritdoc}
   */
  public function listConfigTypes() {
    if (empty($this->configTypes)) {
      $definitions = $this->configUpdateLister
        ->listTypes();
      $config_types = array_map(function (EntityTypeInterface $definition) {
        return $definition
          ->getLabel();
      }, $definitions);
      $config_types['system_simple'] = $this
        ->t('Simple configuration');

      // Sort the entity types by label.
      uasort($config_types, 'strnatcasecmp');
      $this->configTypes = $config_types;
    }
    return $this->configTypes;
  }

  /**
   * Wraps the function drupal_get_filename().
   *
   * @param $type
   *   The type of the item; one of 'core', 'profile', 'module', 'theme', or
   *   'theme_engine'.
   * @param $name
   *   The name of the item for which the filename is requested. Ignored for
   *   $type 'core'.
   * @param $filename
   *   (Optional) The filename of the item if it is to be set explicitly rather
   *   than by consulting the database.
   *
   * @return
   *   The filename of the requested item or NULL if the item is not found.
   */
  protected function drupalGetFilename($type, $name, $filename = NULL) {
    return drupal_get_filename($type, $name, $filename);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigSyncLister::$activeStorage protected property The active configuration storage.
ConfigSyncLister::$configCollector protected property The configuration collector.
ConfigSyncLister::$configManager protected property The configuration manager.
ConfigSyncLister::$configTypes protected property List of current config entity type labels, keyed by entity type.
ConfigSyncLister::$configUpdateLister protected property The configuration update lister.
ConfigSyncLister::$snapshotExtensionStorage protected property The snapshot config storage for values from the extension storage.
ConfigSyncLister::drupalGetFilename protected function Wraps the function drupal_get_filename().
ConfigSyncLister::getExtensionChangelist public function Returns a change list for a given module or theme. Overrides ConfigSyncListerInterface::getExtensionChangelist
ConfigSyncLister::getExtensionChangelists public function Returns a change list for all installed extensions. Overrides ConfigSyncListerInterface::getExtensionChangelists
ConfigSyncLister::listConfigTypes public function
ConfigSyncLister::__construct public function Constructs a ConfigSyncLister object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.