You are here

class ConfigSyncSnapshotter in Configuration Synchronizer 8.2

Same name and namespace in other branches
  1. 8 src/ConfigSyncSnapshotter.php \Drupal\config_sync\ConfigSyncSnapshotter

The ConfigSyncSnapshotter provides helper functions for taking snapshots of extension-provided configuration.

Hierarchy

Expanded class hierarchy of ConfigSyncSnapshotter

1 string reference to 'ConfigSyncSnapshotter'
config_sync.services.yml in ./config_sync.services.yml
config_sync.services.yml
1 service uses ConfigSyncSnapshotter
config_sync.snapshotter in ./config_sync.services.yml
Drupal\config_sync\ConfigSyncSnapshotter

File

src/ConfigSyncSnapshotter.php, line 17

Namespace

Drupal\config_sync
View source
class ConfigSyncSnapshotter implements ConfigSyncSnapshotterInterface {
  use ConfigSnapshotStorageTrait;
  use ConfigSyncActiveStoragesTrait;
  use ConfigSyncExtensionsTrait;

  /**
   * The app root for the current operation.
   *
   * @var string
   */
  protected $root;

  /**
   * The configuration collector.
   *
   * @var \Drupal\config_sync\Plugin\SyncConfigCollectorInterface
   */
  protected $configCollector;

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

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

  /**
   * The configuration synchronizer lister.
   *
   * @var \Drupal\config_sync\configSyncListerInterface
   */
  protected $configSyncLister;

  /**
   * The module extension list.
   *
   * @var \Drupal\Core\Extension\ModuleExtensionList
   */
  protected $moduleExtensionList;

  /**
   * Constructs a ConfigSyncSnapshotter object.
   *
   * @param string $root
   *   The app root.
   * @param \Drupal\config_provider\Plugin\SyncConfigCollectorInterface $config_collector
   *   The config collector.
   * @param \Drupal\Core\Config\StorageInterface $provider_storage
   *   The provider configuration storage.
   * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
   *   The configuration manager.
   * @param \Drupal\Core\Config\StorageInterface $active_storage
   *   The active configuration store where the list of enabled modules and
   *   themes is stored.
   * @param \Drupal\config_sync\ConfigSyncListerInterface $config_sync_lister
   *   The configuration synchronizer lister.
   * @param \Drupal\Core\Extension\ModuleExtensionList $module_extension_list
   *   The module extension list.
   */
  public function __construct($root, SyncConfigCollectorInterface $config_collector, StorageInterface $provider_storage, ConfigManagerInterface $config_manager, StorageInterface $active_storage, ConfigSyncListerInterface $config_sync_lister, ModuleExtensionList $module_extension_list) {
    $this->root = $root;
    $this->configCollector = $config_collector;
    $this->providerStorage = $provider_storage;
    $this->configManager = $config_manager;
    $this->activeStorages[$active_storage
      ->getCollectionName()] = $active_storage;
    $this->configSyncLister = $config_sync_lister;
    $this->moduleExtensionList = $module_extension_list;
  }

  /**
   * {@inheritdoc}
   */
  public function refreshExtensionSnapshot($type, array $names, $mode) {
    foreach ($names as $name) {
      $extensions = [];
      $pathname = $this
        ->drupalGetFilename($type, $name);
      $extensions[$name] = new Extension($this->root, $type, $pathname);
      $snapshot_storage = $this
        ->getConfigSnapshotStorage(ConfigSyncSnapshotterInterface::CONFIG_SNAPSHOT_SET, $type, $name);
      switch ($mode) {

        // On install, snapshot configuration in two stages. First, snapshot
        // unaltered configuration. Then, below, apply alters.
        case ConfigSyncSnapshotterInterface::SNAPSHOT_MODE_INSTALL:
          $this->configCollector
            ->addConfigForSnapshotting($extensions);
          break;

        // On import, snapshot fully altered configuration.
        case ConfigSyncSnapshotterInterface::SNAPSHOT_MODE_IMPORT:
          $this->configCollector
            ->addInstallableConfig($extensions);
          break;
      }

      // Create the snapshot.
      $this->configManager
        ->createSnapshot($this->providerStorage, $snapshot_storage);

      // Conditionally alter the previously added configuration.
      if ($mode === ConfigSyncSnapshotterInterface::SNAPSHOT_MODE_INSTALL) {
        $this->configCollector
          ->alterConfigSnapshots($extensions);
      }
    }
    $this
      ->snapshotNewItems();
  }

  /**
   * Snapshot items that are installed but haven't yet been snapshotted.
   *
   * Certain configuration items are installed subsequent to the installation
   * of the modules that provide them. This is true of optional configuration
   * when the conditions for installation are met subsequent to the initial
   * installation of the providing module.
   *
   * To cover these cases, we detect and snapshot such items.
   */
  protected function snapshotNewItems() {
    $extension_changelists = $this->configSyncLister
      ->getExtensionChangelists();

    // Populate the provider storage with all available configuration.
    $this->configCollector
      ->addInstallableConfig();
    foreach ($extension_changelists as $type => $extensions) {
      foreach ($extensions as $name => $collection_changelists) {
        $snapshot_storage = $this
          ->getConfigSnapshotStorage(ConfigSyncSnapshotterInterface::CONFIG_SNAPSHOT_SET, $type, $name);
        foreach ($collection_changelists as $collection => $operation_types) {

          // Switch collection storages if necessary.
          if ($collection !== $snapshot_storage
            ->getCollectionName()) {
            $snapshot_storage = $snapshot_storage
              ->createCollection($collection);
          }

          // Create operations indicate the configuration is provided but
          // hasn't been snapshotted.
          if (isset($operation_types['create'])) {
            foreach (array_keys($operation_types['create']) as $config_id) {

              // If the item exists but there's no snapshot, create one.
              if ($this
                ->getActiveStorages($collection)
                ->exists($config_id)) {
                $snapshot_storage
                  ->write($config_id, $this->providerStorage
                  ->read($config_id));
              }
            }
          }
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function createSnapshot() {
    $extension_names = $this
      ->getSyncExtensions();
    foreach ($extension_names as $type => $names) {
      if ($type === 'module') {
        $names = $this
          ->listModulesInDependencyOrder($names);
      }
      $this
        ->refreshExtensionSnapshot($type, $names, ConfigSyncSnapshotterInterface::SNAPSHOT_MODE_INSTALL);
    }
  }

  /**
   * Returns a list of specified modules sorted in order of dependency.
   *
   * @param string[] $module_list
   *   An array of module names.
   *
   * @return string[]
   *   An array of module names.
   */
  protected function listModulesInDependencyOrder($module_list) {
    $module_list = array_combine($module_list, $module_list);

    // Get a list of modules with dependency weights as values.
    $module_data = $this->moduleExtensionList
      ->getList();

    // Set the actual module weights.
    $module_list = array_map(function ($module) use ($module_data) {
      return $module_data[$module]->sort;
    }, $module_list);

    // Sort the module list by their weights (reverse).
    arsort($module_list);
    return array_keys($module_list);
  }

  /**
   * Wrapper for 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 path is requested. Ignored for
   *   $type 'core'.
   *
   * @return string
   *   The path to the requested item or an empty string if the item is not
   *   found.
   */
  protected function drupalGetFilename($type, $name) {
    return drupal_get_filename($type, $name);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigSnapshotStorageTrait::getConfigSnapshotStorage protected function Returns a configuration snapshot storage service.
ConfigSyncActiveStoragesTrait::$activeStorages protected property The active configuration storages, keyed by collection.
ConfigSyncActiveStoragesTrait::getActiveStorages protected function Gets the configuration storage that provides the active configuration.
ConfigSyncExtensionsTrait::getSyncExtensions protected function
ConfigSyncSnapshotter::$configCollector protected property The configuration collector.
ConfigSyncSnapshotter::$configManager protected property The configuration manager.
ConfigSyncSnapshotter::$configSyncLister protected property The configuration synchronizer lister.
ConfigSyncSnapshotter::$moduleExtensionList protected property The module extension list.
ConfigSyncSnapshotter::$providerStorage protected property The provider configuration storage.
ConfigSyncSnapshotter::$root protected property The app root for the current operation.
ConfigSyncSnapshotter::createSnapshot public function Takes a snapshot of configuration from all installed modules and themes. Overrides ConfigSyncSnapshotterInterface::createSnapshot
ConfigSyncSnapshotter::drupalGetFilename protected function Wrapper for drupal_get_filename().
ConfigSyncSnapshotter::listModulesInDependencyOrder protected function Returns a list of specified modules sorted in order of dependency.
ConfigSyncSnapshotter::refreshExtensionSnapshot public function Takes a snapshot of configuration from specified modules or themes. Overrides ConfigSyncSnapshotterInterface::refreshExtensionSnapshot
ConfigSyncSnapshotter::snapshotNewItems protected function Snapshot items that are installed but haven't yet been snapshotted.
ConfigSyncSnapshotter::__construct public function Constructs a ConfigSyncSnapshotter object.
ConfigSyncSnapshotterInterface::CONFIG_SNAPSHOT_SET constant Install snapshot mode.
ConfigSyncSnapshotterInterface::SNAPSHOT_MODE_IMPORT constant Import snapshot mode.
ConfigSyncSnapshotterInterface::SNAPSHOT_MODE_INSTALL constant Install snapshot mode.