You are here

function drush_features_diff in Features 8.3

Same name and namespace in other branches
  1. 8.4 drush/features.drush8.inc \drush_features_diff()
  2. 6 features.drush.inc \drush_features_diff()
  3. 7.2 features.drush.inc \drush_features_diff()
  4. 7 features.drush.inc \drush_features_diff()

Lists the differences in the package config vs the active store.

Parameters

string $package: The machine name of a package.

File

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

Code

function drush_features_diff() {
  if (!($args = func_get_args())) {
    drush_print_table(drush_features_list_packages());
    return;
  }

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

  /** @var \Drupal\features\FeaturesAssignerInterface $assigner */
  $assigner = \Drupal::service('features_assigner');
  $assigner
    ->assignConfigPackages();
  $module = $args[0];
  $filter_ctypes = drush_get_option("ctypes");
  if ($filter_ctypes) {
    $filter_ctypes = explode(',', $filter_ctypes);
  }
  $feature = $manager
    ->loadPackage($module, TRUE);
  if (empty($feature)) {
    drush_log(dt('No such feature is available: @module', [
      '@module' => $module,
    ]), 'error');
    return;
  }
  $lines = drush_get_option('lines');
  $lines = isset($lines) ? $lines : 2;
  $formatter = new DiffFormatter();
  $formatter->leading_context_lines = $lines;
  $formatter->trailing_context_lines = $lines;
  $formatter->show_header = FALSE;
  if (drush_get_context('DRUSH_NOCOLOR')) {
    $red = $green = "%s";
  }
  else {
    $red = "\33[31;40m\33[1m%s\33[0m";
    $green = "\33[0;32;40m\33[1m%s\33[0m";
  }
  $overrides = $manager
    ->detectOverrides($feature);
  $missing = $manager
    ->reorderMissing($manager
    ->detectMissing($feature));
  $overrides = array_merge($overrides, $missing);
  if (empty($overrides)) {
    drush_print(dt('Active config matches stored config for @module.', [
      '@module' => $module,
    ]));
  }
  else {

    /** @var \Drupal\config_update\ConfigDiffInterface $config_diff */
    $config_diff = \Drupal::service('config_update.config_diff');

    /** @var \Drupal\Core\Config\StorageInterface $active_storage */
    $active_storage = \Drupal::service('config.storage');

    // Print key for colors.
    drush_print(dt('Legend: '));
    drush_print(sprintf($red, dt('Code:    drush features-import will replace the active config with the displayed code.')));
    drush_print(sprintf($green, dt('Active:  drush features-export will update the exported feature with the displayed active config')));
    foreach ($overrides as $name) {
      $message = '';
      if (in_array($name, $missing)) {
        $message = sprintf($red, t('(missing from active)'));
        $extension = [];
      }
      else {
        $active = $manager
          ->getActiveStorage()
          ->read($name);
        $extension = $manager
          ->getExtensionStorages()
          ->read($name);
        if (empty($extension)) {
          $extension = [];
          $message = sprintf($green, t('(not exported)'));
        }
        $diff = $config_diff
          ->diff($extension, $active);
        $rows = explode("\n", $formatter
          ->format($diff));
      }
      drush_print();
      drush_print(dt("Config @name @message", [
        '@name' => $name,
        '@message' => $message,
      ]));
      if (!empty($extension)) {
        foreach ($rows as $row) {
          if (strpos($row, '>') === 0) {
            drush_print(sprintf($green, $row));
          }
          elseif (strpos($row, '<') === 0) {
            drush_print(sprintf($red, $row));
          }
          else {
            drush_print($row);
          }
        }
      }
    }
  }
}