You are here

function drush_features_import in Features 8.4

Same name and namespace in other branches
  1. 8.3 drush/features.drush8.inc \drush_features_import()

Imports module config into the active store.

Same as the old "revert" functionality.

1 string reference to 'drush_features_import'
drush_features_import_all in drush/features.drush8.inc
Drush command callback for features-import-all.

File

drush/features.drush8.inc, line 573
Features module drush integration.

Code

function drush_features_import() {
  if ($args = func_get_args()) {
    _drush_features_options();

    // Determine if revert should be forced.
    $force = drush_get_option('force');

    // Determine if -y was supplied. If so, we can filter out needless output
    // from this command.
    $skip_confirmation = drush_get_context('DRUSH_AFFIRMATIVE');

    /** @var \Drupal\features\FeaturesManagerInterface $manager */
    $manager = \Drupal::service('features.manager');

    // Parse list of arguments.
    $modules = [];
    foreach ($args as $arg) {
      $arg = explode(':', $arg);
      $module = array_shift($arg);
      $component = array_shift($arg);
      if (isset($module)) {
        if (empty($component)) {

          // If we received just a feature name, this means that we need all of
          // its components.
          $modules[$module] = TRUE;
        }
        elseif ($modules[$module] !== TRUE) {
          if (!isset($modules[$module])) {
            $modules[$module] = [];
          }
          $modules[$module][] = $component;
        }
      }
    }

    // Process modules.
    foreach ($modules as $module => $components_needed) {
      $dt_args['@module'] = $module;

      /** @var \Drupal\features\Package $feature */
      $feature = $manager
        ->loadPackage($module, TRUE);
      if (empty($feature)) {
        drush_log(dt('No such feature is available: @module', $dt_args), 'error');
        return;
      }
      if ($feature
        ->getStatus() != FeaturesManagerInterface::STATUS_INSTALLED) {
        drush_log(dt('No such feature is installed: @module', $dt_args), 'error');
        return;
      }

      // Forcefully revert all components of a feature.
      if ($force) {
        $components = $feature
          ->getConfigOrig();
      }
      else {
        $components = $manager
          ->detectOverrides($feature);
        $missing = $manager
          ->reorderMissing($manager
          ->detectMissing($feature));

        // Be sure to import missing components first.
        $components = array_merge($missing, $components);
      }
      if (!empty($components_needed) && is_array($components_needed)) {
        $components = array_intersect($components, $components_needed);
      }
      if (empty($components)) {
        drush_log(dt('Current state already matches active config, aborting.'), 'ok');
      }
      else {

        // Determine which config the user wants to import/revert.
        $config_to_create = [];
        foreach ($components as $component) {
          $dt_args['@component'] = $component;
          $confirmation_message = 'Do you really want to import @module : @component?';
          if ($skip_confirmation || drush_confirm(dt($confirmation_message, $dt_args))) {
            $config_to_create[$component] = '';
          }
        }

        // Perform the import/revert.
        $config_imported = $manager
          ->createConfiguration($config_to_create);

        // List the results.
        foreach ($components as $component) {
          $dt_args['@component'] = $component;
          if (isset($config_imported['new'][$component])) {
            drush_log(dt('Imported @module : @component.', $dt_args), 'ok');
          }
          elseif (isset($config_imported['updated'][$component])) {
            drush_log(dt('Reverted @module : @component.', $dt_args), 'ok');
          }
          elseif (!isset($config_to_create[$component])) {
            drush_log(dt('Skipping @module : @component.', $dt_args), 'ok');
          }
          else {
            drush_log(dt('Error importing @module : @component.', $dt_args), 'error');
          }
        }
      }
    }
  }
  else {
    drush_print_table(drush_features_list_packages());
    return;
  }
}