Commit 2cace84b authored by Bálint Csuthy's avatar Bálint Csuthy Committed by Bálint Csuthy
Browse files

Issue #3042621 by jhodgdon, Pasqualle, bserem: Refactor to not support Drush 8 any more

parent a0d4d7a7
Loading
Loading
Loading
Loading
+0 −272
Changes for config_update_ui/config_update_ui.drush.inc: 0 added lines, 272 removed lines.
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Drush 8 commands for the Configuration Update Reports module.
 */

use Drupal\config_update_ui\Logger\ConfigUpdateUiDrush8Logger;

/**
 * Implements hook_drush_command().
 */
function config_update_ui_drush_command() {

  $items = [];

  $items['config-list-types'] = [
    'description' => 'List config types',
    'aliases' => ['clt'],
    'core' => ['8+'],
    'outputformat' => [
      'default' => 'list',
    ],
  ];

  $items['config-added-report'] = [
    'description' => 'Display a list of config items that did not come from your installed modules, themes, or install profile',
    'arguments' => [
      'name' => 'The type of config to report on. See config-list-types to list them. You can also use system.all for all types, or system.simple for simple config.',
    ],
    'required-arguments' => 1,
    'examples' => [
      'drush config-added-report action' => 'Displays the added config report for action config.',
    ],
    'aliases' => ['cra'],
    'core' => ['8+'],
    'outputformat' => [
      'default' => 'list',
    ],
  ];

  $items['config-missing-report'] = [
    'description' => 'Display a list of config items from your installed modules, themes, or install profile that are not currently in your active config',
    'arguments' => [
      'type' => 'Run the report for: module, theme, profile, or "type" for config entity type.',
      'name' => 'The machine name of the module, theme, etc. to report on. See config-list-types to list types for config entities; you can also use system.all for all types, or system.simple for simple config.',
    ],
    'required-arguments' => 2,
    'examples' => [
      'drush config-missing-report type action' => 'Displays the missing config report for action config.',
    ],
    'aliases' => ['crm'],
    'core' => ['8+'],
    'outputformat' => [
      'default' => 'list',
    ],
  ];

  $items['config-inactive-report'] = [
    'description' => 'Display a list of optional config items from your installed modules, themes, or install profile that are not currently in your active config',
    'arguments' => [
      'type' => 'Run the report for: module, theme, profile, or "type" for config entity type.',
      'name' => 'The machine name of the module, theme, etc. to report on. See config-list-types to list types for config entities; you can also use system.all for all types, or system.simple for simple config.',
    ],
    'required-arguments' => 2,
    'examples' => [
      'drush config-inactive-report type action' => 'Displays the inactive config report for action config.',
    ],
    'aliases' => ['cri'],
    'core' => ['8+'],
    'outputformat' => [
      'default' => 'list',
    ],
  ];

  $items['config-different-report'] = [
    'description' => 'Display a list of config items that differ from the versions provided by your installed modules, themes, or install profile. See config-diff to show what the differences are.',
    'arguments' => [
      'type' => 'Run the report for: module, theme, profile, or "type" for config entity type.',
      'name' => 'The machine name of the module, theme, etc. to report on. See config-list-types to list types for config entities; you can also use system.all for all types, or system.simple for simple config.',
    ],
    'required-arguments' => 2,
    'examples' => [
      'drush config-different-report type action' => 'Displays the differing config report for action config.',
    ],
    'aliases' => ['crd'],
    'core' => ['8+'],
    'outputformat' => [
      'default' => 'list',
    ],
  ];

  $items['config-diff'] = [
    'description' => 'Display line-by-line differences for one config item between your active config and the version currently being provided by an installed module, theme, or install profile',
    'arguments' => [
      'name' => 'The config item to diff. See config-different-report to list config items that are different.',
    ],
    'required-arguments' => 1,
    'examples' => [
      'drush config-diff block.block.olivero_search' => 'Displays the config differences for the search block in the Olivero theme.',
    ],
    'aliases' => ['cfd'],
    'core' => ['8+'],
  ];

  $items['config-revert'] = [
    'description' => 'Revert one config item in active storage to the version provided by an installed module, theme, or install profile.',
    'arguments' => [
      'name' => 'The config item to revert. See config-different-report to list config items that are different.',
    ],
    'required-arguments' => 1,
    'examples' => [
      'drush config-revert block.block.olivero_search' => 'Revert the config for the search block in the Olivero theme to the version provided by the install profile.',
    ],
    'aliases' => ['cfr'],
    'core' => ['8+'],
  ];

  $items['config-import-missing'] = [
    'description' => 'Import a missing or inactive config item provided by an installed module, theme, or install profile. Be sure that requirements are met.',
    'arguments' => [
      'name' => 'The name of the config item to import (usually the ID you would see in the user interface). See config-missing-report to list config items that are missing, and config-inactive-report to list config items that are inactive.',
    ],
    'required-arguments' => 1,
    'examples' => [
      'drush config-import-missing block.block.olivero_search' => 'Import the config for the search block in the Olivero theme from the version provided by the install profile.',
    ],
    'aliases' => ['cfi'],
    'core' => ['8+'],
  ];

  $items['config-revert-multiple'] = [
    'description' => 'Revert a set of config items to the versions provided by installed modules, themes, or install profiles. A set is all differing items from one extension, or one type of configuration.',
    'arguments' => [
      'type' => 'Type of set to revert: "module" for all items from a module, "theme" for all items from a theme, "profile" for all items from the install profile, or "type" for all items of one config entity type. See config-different-report to list config items that are different.',
      'name' => 'The machine name of the module, theme, etc. to revert items of. All items in the corresponding config-different-report will be reverted.',
    ],
    'required-arguments' => 2,
    'examples' => [
      'drush config-revert-multiple type action' => 'Revert all differing config items of type action.',
    ],
    'aliases' => ['cfrm'],
    'core' => ['8+'],
  ];

  return $items;
}

/**
 * Lists available config types.
 *
 * @return array
 *   An array of configuration types.
 */
function drush_config_update_ui_config_list_types() {
  return drush_config_update_ui_cli_service()->listTypes();
}

/**
 * Runs the config added report.
 *
 * @param string $name
 *   Type of config to report on.
 *
 * @return array
 *   An array of added configuration items.
 */
function drush_config_update_ui_config_added_report($name) {
  return drush_config_update_ui_cli_service()->addedReport($name);
}

/**
 * Runs the config missing report.
 *
 * @param string $type
 *   Type of report to run: 'type', 'module', 'theme', or 'profile'.
 * @param string $name
 *   Machine name of item to report on.
 *
 * @return array
 *   An array of missing configuration items.
 */
function drush_config_update_ui_config_missing_report($type, $name) {
  return drush_config_update_ui_cli_service()->missingReport($type, $name);
}

/**
 * Runs the config inactive report.
 *
 * @param string $type
 *   Type of report to run: 'type', 'module', 'theme', or 'profile'.
 * @param string $name
 *   Machine name of item to report on.
 *
 * @return array
 *   An array of inactive configuration items.
 */
function drush_config_update_ui_config_inactive_report($type, $name) {
  return drush_config_update_ui_cli_service()->inactiveReport($type, $name);
}

/**
 * Runs the config different report.
 *
 * @param string $type
 *   Type of report to run: 'type', 'module', 'theme', or 'profile'.
 * @param string $name
 *   Machine name of item to report on.
 *
 * @return array
 *   An array of differing configuration items.
 */
function drush_config_update_ui_config_different_report($type, $name) {
  return drush_config_update_ui_cli_service()->differentReport($type, $name);
}

/**
 * Runs the drush config-diff command.
 *
 * @param string $name
 *   Config item to diff.
 *
 * @return string
 *   The formatted diff output.
 */
function drush_config_update_ui_config_diff($name) {
  return drush_config_update_ui_cli_service()->diff($name);
}

/**
 * Runs the drush config-revert command.
 *
 * @param string $name
 *   Config item to revert.
 */
function drush_config_update_ui_config_revert($name) {
  return drush_config_update_ui_cli_service()->revert($name);
}

/**
 * Runs the drush config-import-missing command.
 *
 * @param string $name
 *   Name of config item to import, without prefix.
 */
function drush_config_update_ui_config_import_missing($name) {
  return drush_config_update_ui_cli_service()->importMissing($name);
}

/**
 * Runs the drush config-revert-multiple command.
 *
 * @param string $type
 *   Type of config to revert: 'type', 'module', 'theme', or 'profile'.
 * @param string $name
 *   Machine name of type to revert.
 */
function drush_config_update_ui_config_revert_multiple($type, $name) {
  return drush_config_update_ui_cli_service()->revertMultiple($type, $name);
}

/**
 * Instantiates and returns the Drush Commands CLI service for Drush 8.
 */
function drush_config_update_ui_cli_service() {
  /** @var \Drupal\config_update_ui\ConfigUpdateUiCliService $config_update_ui_service */
  $config_update_ui_service = \Drupal::service('config_update_ui.cli');
  $logger = new ConfigUpdateUiDrush8Logger(\Drupal::service('logger.log_message_parser'));
  $config_update_ui_service->setLogger($logger);

  return $config_update_ui_service;
}
+0 −57
Changes for config_update_ui/config_update_ui.drush_testing.inc: 0 added lines, 57 removed lines.
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Defines stubs for Drush functions and constants so tests can run.
 *
 * @see \Drupal\config_update_ui\Tests\ConfigUpdateTest
 */

if (!function_exists('drush_log')) {

  /**
   * Stub for drush_log() function for testing.
   */
  function drush_log() {
  }

}

if (!function_exists('drush_print')) {

  /**
   * Stub for drush_print() function for testing.
   */
  function drush_print() {
  }

}

if (!function_exists('dt')) {

  /**
   * Stub for dt() function for testing.
   */
  function dt($text) {
    return $text;
  }

}

if (!class_exists('Drush\Log\LogLevel')) {

  /**
   * Mock class for Drush\Log\LogLevel::SUCCESS and ERROR constants.
   *
   * Used in Drush 8's logger for the Config Update CLI service.
   */
  class DrushLogLevel {
    const SUCCESS = 'success';
    const ERROR = 'error';

  }
  class_alias('DrushLogLevel', 'Drush\Log\LogLevel');

}

defined('STDERR') or define('STDERR', 25);
+0 −4
Changes for config_update_ui/config_update_ui.services.yml: 0 added lines, 4 removed lines.
Original line number Diff line number Diff line
services:
  config_update_ui.cli:
    class: Drupal\config_update_ui\ConfigUpdateUiCliService
    arguments: ['@entity_type.manager', '@config_update.config_diff', '@config_update.config_list', '@config_update.config_update', '@config.factory']
+1 −1
Changes for config_update_ui/drush.services.yml: 1 added line, 1 removed line.
Original line number Diff line number Diff line
services:
  config_update_ui.commands:
    class: \Drupal\config_update_ui\Commands\ConfigUpdateUiCommands
    arguments: ['@config_update_ui.cli']
    arguments: ['@entity_type.manager', '@config_update.config_diff', '@config_update.config_list', '@config_update.config_update']
    tags:
      - { name: drush.command }
+181 −19
Changes for config_update_ui/src/Commands/ConfigUpdateUiCommands.php: 181 added lines, 19 removed lines.
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@ namespace Drupal\config_update_ui\Commands;

use Drush\Drush;
use Drush\Commands\DrushCommands;
use Drupal\config_update_ui\ConfigUpdateUiCliService;

/**
 * A set of Drush commands for Config Update Manager.
@@ -12,23 +11,58 @@ use Drupal\config_update_ui\ConfigUpdateUiCliService;
class ConfigUpdateUiCommands extends DrushCommands {

  /**
   * The interoperability CLI service for Configuration Update Manager.
   * The entity manager.
   *
   * Allows for sharing logic for CLI commands between Drush 8 and 9.
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityManager;

  /**
   * The config differ.
   *
   * @var \Drupal\config_update\ConfigDiffer
   */
  protected $configDiff;

  /**
   * The config lister.
   *
   * @var \Drupal\config_update\ConfigListerWithProviders
   */
  protected $configList;

  /**
   * The config reverter.
   *
   * @var \Drupal\config_update\ConfigReverter
   */
  protected $configUpdate;

  /**
   * The logger class.
   *
   * @var \Drupal\config_update_ui\ConfigUpdateUiCliService
   * @var \Drush\Log\Logger
   */
  protected $cliService;
  protected $logger;

  /**
   * Constructs a ConfigUpdateUiCommands object.
   *
   * @param \Drupal\config_update_ui\ConfigUpdateUiCliService $cliService
   *   The CLI service which allows interoperability between Drush versions.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityManager
   *   The entity type manager.
   * @param \Drupal\config_update\ConfigDiffer $configDiff
   *   The config differ.
   * @param \Drupal\config_update\ConfigListerWithProviders $configList
   *   The config lister.
   * @param \Drupal\config_update\ConfigReverter $configUpdate
   *   The config reverter.
   */
  public function __construct(ConfigUpdateUiCliService $cliService) {
    $this->cliService = $cliService;
    $this->cliService->setLogger(Drush::logger());
  public function __construct(EntityTypeManagerInterface $entityManager, ConfigDiffer $configDiff, ConfigListerWithProviders $configList, ConfigReverter $configUpdate) {
    $this->entityManager = $entityManager;
    $this->configDiff = $configDiff;
    $this->configList = $configList;
    $this->configUpdate = $configUpdate;
    $this->logger = Drush::logger();
  }

  /**
@@ -41,7 +75,11 @@ class ConfigUpdateUiCommands extends DrushCommands {
   * @aliases clt,config-list-types
   */
  public function listTypes() {
    return $this->cliService->listTypes();
    $rows = [];
    $definitions = $this->configList->listTypes();
    $output = array_keys($definitions);

    return $output;
  }

  /**
@@ -65,7 +103,14 @@ class ConfigUpdateUiCommands extends DrushCommands {
   * @aliases cra,config-added-report
   */
  public function addedReport($name) {
    return $this->cliService->addedReport($name);
    list($activeList, $installList, $optionalList) = $this->configList->listConfig('type', $name);
    $addedItems = array_diff($activeList, $installList, $optionalList);
    if (!count($addedItems)) {
      $this->logger->success(dt('No added config.'));
    }
    sort($addedItems);

    return $addedItems;
  }

  /**
@@ -92,7 +137,14 @@ class ConfigUpdateUiCommands extends DrushCommands {
   * @aliases crm,config-missing-report
   */
  public function missingReport($type, $name) {
    return $this->cliService->missingReport($type, $name);
    list($activeList, $installList, $optionalList) = $this->configList->listConfig($type, $name);
    $missingItems = array_diff($installList, $activeList);
    if (!count($missingItems)) {
      $this->logger->success(dt('No missing config.'));
    }
    sort($missingItems);

    return $missingItems;
  }

  /**
@@ -119,7 +171,14 @@ class ConfigUpdateUiCommands extends DrushCommands {
   * @aliases cri,config-inactive-report
   */
  public function inactiveReport($type, $name) {
    return $this->cliService->inactiveReport($type, $name);
    list($activeList, $installList, $optionalList) = $this->configList->listConfig($type, $name);
    $inactiveItems = array_diff($optionalList, $activeList);
    if (!count($inactiveItems)) {
      $this->logger->success(dt('No inactive config.'));
    }
    sort($inactiveItems);

    return $inactiveItems;
  }

  /**
@@ -147,7 +206,12 @@ class ConfigUpdateUiCommands extends DrushCommands {
   * @aliases crd,config-different-report
   */
  public function differentReport($type, $name) {
    return $this->cliService->differentReport($type, $name);
    $differentItems = $this->getDifferentItems($type, $name);
    if (!count($differentItems)) {
      $this->logger->success(dt('No different config'));
    }

    return $differentItems;
  }

  /**
@@ -171,7 +235,19 @@ class ConfigUpdateUiCommands extends DrushCommands {
   * @aliases cfd,config-diff
   */
  public function diff($name) {
    return $this->cliService->diff($name);
    $extension = $this->configUpdate->getFromExtension('', $name);
    $active = $this->configUpdate->getFromActive('', $name);
    if ($extension && $active) {
      $diff = $this->configDiff->diff($extension, $active);
      // Drupal\Component\Diff\DiffFormatter does not expose a service so we
      // instantiate it manually here.
      $diffFormatter = new DiffFormatter();
      $output = $diffFormatter->format($diff);
      return $output;
    }
    else {
      $this->logger->error(dt('Config is missing, cannot diff.'));
    }
  }

  /**
@@ -192,7 +268,19 @@ class ConfigUpdateUiCommands extends DrushCommands {
   * @aliases cfr,config-revert
   */
  public function revert($name) {
    $this->cliService->revert($name);
    $type = $this->configList->getTypeNameByConfigName($name);
    // The lister gives NULL if simple configuration, but the reverter expects
    // 'system.simple' so we convert it.
    if ($type === NULL) {
      $type = 'system.simple';
    }
    $shortname = $this->getConfigShortname($type, $name);
    if ($this->configUpdate->revert($type, $shortname)) {
      $this->logger->success(dt('The configuration item @name was reverted to its source.', ['@name' => $name]));
    }
    else {
      $this->logger->error(dt('There was an error and the configuration item @name was not reverted.', ['@name' => $name]));
    }
  }

  /**
@@ -215,7 +303,19 @@ class ConfigUpdateUiCommands extends DrushCommands {
   * @aliases cfi,config-import-missing
   */
  public function importMissing($name) {
    $this->cliService->importMissing($name);
    $type = $this->configList->getTypeNameByConfigName($name);
    // The lister gives NULL if simple configuration, but the reverter expects
    // 'system.simple' so we convert it.
    if ($type === NULL) {
      $type = 'system.simple';
    }
    $shortname = $this->getConfigShortname($type, $name);
    if ($this->configUpdate->import($type, $shortname)) {
      $this->logger->success(dt('The configuration item @name was imported from its source.', ['@name' => $name]));
    }
    else {
      $this->logger->error(dt('There was an error and the configuration item @name was not imported.', ['@name' => $name]));
    }
  }

  /**
@@ -241,7 +341,69 @@ class ConfigUpdateUiCommands extends DrushCommands {
   * @aliases cfrm,config-revert-multiple
   */
  public function revertMultiple($type, $name) {
    return $this->cliService->revertMultiple($type, $name);
    $different = $this->getDifferentItems($type, $name);
    foreach ($different as $name) {
      $this->revert($name);
    }
  }

  /**
   * Lists differing config items.
   *
   * Lists config items that differ from the versions provided by your
   * installed modules, themes, or install profile. See config-diff to show
   * what the differences are.
   *
   * @param string $type
   *   Run the report for: module, theme, profile, or "type" for config entity
   *   type.
   * @param string $name
   *   The machine name of the module, theme, etc. to report on. See
   *   config-list-types to list types for config entities; you can also use
   *   system.all for all types, or system.simple for simple config.
   *
   * @return array
   *   An array of differing configuration items.
   */
  protected function getDifferentItems($type, $name) {
    list($activeList, $installList, $optionalList) = $this->configList->listConfig($type, $name);
    $addedItems = array_diff($activeList, $installList, $optionalList);
    $activeAndAddedItems = array_diff($activeList, $addedItems);
    $differentItems = [];
    foreach ($activeAndAddedItems as $name) {
      $active = $this->configUpdate->getFromActive('', $name);
      $extension = $this->configUpdate->getFromExtension('', $name);
      if (!$this->configDiff->same($active, $extension)) {
        $differentItems[] = $name;
      }
    }
    sort($differentItems);

    return $differentItems;
  }

  /**
   * Gets the config item shortname given the type and name.
   *
   * @param string $type
   *   The type of the config item.
   * @param string $name
   *   The name of the config item.
   *
   * @return string
   *   The shortname for the configuration item.
   */
  protected function getConfigShortname($type, $name) {
    $shortname = $name;
    if ($type != 'system.simple') {
      $definition = $this->entityManager->getDefinition($type);
      $prefix = $definition->getConfigPrefix() . '.';
      if (strpos($name, $prefix) === 0) {
        $shortname = substr($name, strlen($prefix));
      }
    }

    return $shortname;
  }
 
}
Loading