function drush_config_import

8.0.x config.drush.inc drush_config_import($source = NULL)
7.x config.drush.inc drush_config_import($source = NULL)
master config.drush.inc drush_config_import($source = NULL)

Command callback. Import from specified config directory (defaults to sync).

File

commands/core/config.drush.inc, line 571
Provides Configuration Management commands.

Code

function drush_config_import($source = NULL) {
  global $config_directories;

  if ($target = drush_get_option('source')) {
    $source_dir = $target;
  }
  else {
    $choices = drush_map_assoc(array_keys($config_directories));
    unset($choices[CONFIG_ACTIVE_DIRECTORY]);
    if (!isset($source) && count($choices) >= 2) {
      $source = drush_choice($choices, 'Choose a source.');
      if (empty($source)) {
        return drush_user_abort();
      }
    }
    elseif (!isset($source)) {
      $source = CONFIG_SYNC_DIRECTORY;
    }
    $source_dir = config_get_config_directory($source);
  }

  // Retrieve a list of differences between the active and source configuration (if any).
  $source_storage = new FileStorage($source_dir);
  /** @var \Drupal\Core\Config\StorageInterface $active_storage */
  $active_storage = Drupal::service('config.storage');
  // If our configuration storage is being filtered, then attach all filters
  // to the source storage object.  We will use the filtered values uniformly
  // for comparison, full imports, and partial imports.
  $storage_filters = drush_config_get_storage_filters();
  if (!empty($storage_filters)) {
    $source_storage = new StorageWrapper($source_storage, $storage_filters);
  }
  if (drush_get_option('partial', FALSE)) {
    // With partial imports, the comparison must only be made against configs
    // that exist in the source directory.
    $temp_active_storage = new FileStorage(drush_tempdir());
    foreach ($source_storage->listAll() as $name) {
      // Copy active storage to our temporary active store.
      if ($existing = $active_storage->read($name)) {
        $temp_active_storage->write($name, $existing);
      }
    }
    $active_storage = $temp_active_storage;
  }
  $config_comparer = new StorageComparer($source_storage, $active_storage, Drupal::service('config.manager'));
  if (!$config_comparer->createChangelist()->hasChanges()) {
    return drush_log(dt('There are no changes to import.'), LogLevel::OK);
  }

  if (drush_get_option('preview', 'list') == 'list') {
    $change_list = array();
    foreach ($config_comparer->getAllCollectionNames() as $collection) {
      $change_list[$collection] = $config_comparer->getChangelist(NULL, $collection);
    }
    _drush_print_config_changes_table($change_list);
  }
  else {
    $destination_dir = drush_tempdir();
    drush_invoke_process('@self', 'config-export', array(), array('destination' => $destination_dir));
    // @todo Can DiffFormatter produce a CLI pretty diff?
    drush_shell_exec('diff -x %s -u %s %s', '*.git', $destination_dir, $source_dir);
    $output = drush_shell_exec_output();
    drush_print(implode("\n", $output));
  }

  if (drush_confirm(dt('Import the listed configuration changes?'))) {
    if (drush_get_option('partial')) {
      // Partial imports require different processing.
      return drush_op('_drush_config_import_partial', $source_storage);
    }
    return drush_op('_drush_config_import', $config_comparer);
  }
}