You are here

class ConfigSyncCommands in Configuration Synchronizer 8

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

Drush integration for the Configuration Synchronizer module.

Hierarchy

  • class \Drupal\config_sync\Commands\ConfigSyncCommands extends \Drush\Commands\DrushCommands

Expanded class hierarchy of ConfigSyncCommands

1 string reference to 'ConfigSyncCommands'
drush.services.yml in ./drush.services.yml
drush.services.yml
1 service uses ConfigSyncCommands
config_sync.commands in ./drush.services.yml
\Drupal\config_sync\Commands\ConfigSyncCommands

File

src/Commands/ConfigSyncCommands.php, line 20

Namespace

Drupal\config_sync\Commands
View source
class ConfigSyncCommands extends DrushCommands {

  /**
   * The config synchronisation lister service.
   *
   * @var \Drupal\config_sync\ConfigSyncListerInterface
   */
  protected $configSyncLister;

  /**
   * The config synchronization initializer service.
   *
   * @var \Drupal\config_sync\ConfigSyncInitializerInterface
   */
  protected $configSyncInitializer;

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

  /**
   * The merged storage.
   *
   * @var \Drupal\Core\Config\StorageInterface
   */
  protected $mergedStorage;

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

  /**
   * The service containing Drush commands for regular core config imports.
   *
   * @var \Drush\Drupal\Commands\config\ConfigImportCommands
   */
  protected $configImportCommands;

  /**
   * The config snapshotter service.
   *
   * @var \Drupal\config_sync\ConfigSyncSnapshotterInterface
   */
  protected $configSyncSnapshotter;

  /**
   * Constructs a new ConfigSyncCommands object.
   *
   * @param \Drupal\config_sync\ConfigSyncListerInterface $configSyncLister
   *   The config synchronisation lister service.
   * @param \Drupal\config_sync\ConfigSyncInitializerInterface $configSyncInitializer
   *   The config synchronization initializer service.
   * @param \Drupal\Core\Config\StorageInterface $activeStorage
   *   The active configuration storage.
   * @param \Drupal\Core\Config\StorageInterface $mergedStorage
   *   The merged storage.
   * @param \Drupal\Core\Config\ConfigManagerInterface $configManager
   *   The configuration manager.
   * @param \Drush\Drupal\Commands\config\ConfigImportCommands $configImportCommands
   *   The service containing Drush commands for regular core config imports.
   * @param \Drupal\config_sync\ConfigSyncSnapshotterInterface $configSyncSnapshotter
   *   The config snapshotter service.
   */
  public function __construct(ConfigSyncListerInterface $configSyncLister, ConfigSyncInitializerInterface $configSyncInitializer, StorageInterface $activeStorage, StorageInterface $mergedStorage, ConfigManagerInterface $configManager, ConfigImportCommands $configImportCommands, ConfigSyncSnapshotterInterface $configSyncSnapshotter) {
    parent::__construct();
    $this->configSyncLister = $configSyncLister;
    $this->configSyncInitializer = $configSyncInitializer;
    $this->activeStorage = $activeStorage;
    $this->mergedStorage = $mergedStorage;
    $this->configManager = $configManager;
    $this->configImportCommands = $configImportCommands;
    $this->configSyncSnapshotter = $configSyncSnapshotter;
  }

  /**
   * Display a list of all extensions with available configuration updates.
   *
   * @command config-sync-list-updates
   * @usage drush config-sync-list-updates
   *   Display a list of all extensions with available configuration updates.
   * @aliases cs-list
   * @field-labels
   *   type: Operation type
   *   id: Config ID
   *   label: Label
   *   extension_type: Extension type
   *   extension: Extension
   * @default-fields extension,type,label
   * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
   */
  public function syncListUpdates($options = [
    'format' => 'table',
  ]) {
    $rows = [];
    foreach ($this->configSyncLister
      ->getExtensionChangelists() as $extension_type => $extensions) {
      foreach ($extensions as $extension_id => $operation_types) {
        foreach ($operation_types as $operation_type => $configurations) {
          foreach ($configurations as $config_id => $config_label) {
            $rows[$config_id] = [
              'type' => $operation_type,
              'id' => $config_id,
              'label' => $config_label,
              'extension_type' => $extension_type,
              'extension' => $extension_id,
            ];
          }
        }
      }
    }
    return new RowsOfFields($rows);
  }

  /**
   * Apply configuration updates.
   *
   * @command config-sync-update
   * @option discard-overrides The active configuration will be reverted to the new configuration state as defined by distributions and modules, discarding any changes that have been made in the active configuration. If this option is omitted, the overrides will be merged with the configuration updates.
   * @usage drush config-sync-update
   *   Apply updates to all extensions.
   * @aliases cs-update
   */
  public function syncUpdate($options = [
    'discard-overrides' => FALSE,
  ]) {
    $this->configSyncInitializer
      ->initialize(!$options['discard-overrides']);
    $storage_comparer = new StorageComparer($this->mergedStorage, $this->activeStorage, $this->configManager);
    if (!$storage_comparer
      ->createChangelist()
      ->hasChanges()) {
      $this
        ->logger()
        ->notice('There are no changes to import.');
      return;
    }

    // Output the changes that will be performed to the config.
    $change_list = [];
    foreach ($storage_comparer
      ->getAllCollectionNames() as $collection) {
      $change_list[$collection] = $storage_comparer
        ->getChangelist(NULL, $collection);
    }
    $table = ConfigCommands::configChangesTable($change_list, $this
      ->io());
    $table
      ->render();
    if ($this
      ->io()
      ->confirm(dt('Import the listed configuration changes?'))) {

      // Import the config using the default Drush command.
      // @see \Drush\Drupal\Commands\config\ConfigImportCommands::doImport()
      $results = drush_op([
        $this->configImportCommands,
        'doImport',
      ], $storage_comparer);

      // Perform a clean snapshot refresh after the import.
      $this->configSyncSnapshotter
        ->deleteSnapshot();
      $this->configSyncSnapshotter
        ->refreshSnapshot();
      return $results;
    }
    throw new UserAbortException();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigSyncCommands::$activeStorage protected property The active configuration storage.
ConfigSyncCommands::$configImportCommands protected property The service containing Drush commands for regular core config imports.
ConfigSyncCommands::$configManager protected property The configuration manager.
ConfigSyncCommands::$configSyncInitializer protected property The config synchronization initializer service.
ConfigSyncCommands::$configSyncLister protected property The config synchronisation lister service.
ConfigSyncCommands::$configSyncSnapshotter protected property The config snapshotter service.
ConfigSyncCommands::$mergedStorage protected property The merged storage.
ConfigSyncCommands::syncListUpdates public function Display a list of all extensions with available configuration updates.
ConfigSyncCommands::syncUpdate public function Apply configuration updates.
ConfigSyncCommands::__construct public function Constructs a new ConfigSyncCommands object.