You are here

function drush_replication_uninstall in Replication 8

Implements drush_hook_COMMAND().

File

./replication.drush.inc, line 122
Drush integration for the replication module.

Code

function drush_replication_uninstall() {
  $extension = 'replication';
  $uninstall = TRUE;
  $extension_info = drush_get_extensions();
  $required = drush_drupal_required_modules($extension_info);
  if (in_array($extension, $required)) {
    $info = $extension_info[$extension]->info;
    $explanation = !empty($info['explanation']) ? ' ' . dt('Reason: !explanation.', [
      '!explanation' => strip_tags($info['explanation']),
    ]) : '';
    drush_log(dt('!extension is a required extension and can\'t be uninstalled.', [
      '!extension' => $extension,
    ]) . $explanation, LogLevel::INFO);
    $uninstall = FALSE;
  }
  elseif (!$extension_info[$extension]->status) {
    drush_log(dt('!extension is already uninstalled.', [
      '!extension' => $extension,
    ]), LogLevel::INFO);
    $uninstall = FALSE;
  }
  elseif (drush_extension_get_type($extension_info[$extension]) == 'module') {
    $dependents = [];
    foreach (drush_module_dependents([
      $extension,
    ], $extension_info) as $dependent) {
      if (!in_array($dependent, $required) && $extension_info[$dependent]->status) {
        $dependents[] = $dependent;
      }
    }
    if (count($dependents)) {
      drush_log(dt('To uninstall !extension, the following extensions must be uninstalled first: !required', [
        '!extension' => $extension,
        '!required' => implode(', ', $dependents),
      ]), LogLevel::ERROR);
      $uninstall = FALSE;
    }
  }
  if ($uninstall) {
    drush_print(dt('Replication will be uninstalled.'));
    if (!drush_confirm(dt('Do you really want to continue?'))) {
      return drush_user_abort();
    }
    try {

      // Delete all replication_log entities.
      $storage = \Drupal::entityTypeManager()
        ->getStorage('replication_log')
        ->getOriginalStorage();
      $entities = $storage
        ->loadMultiple();
      $storage
        ->delete($entities);
      drush_module_uninstall([
        $extension,
      ]);
    } catch (Exception $e) {
      drush_log($e
        ->getMessage(), LogLevel::ERROR);
    }

    // Inform the user of final status.
    drush_log(dt('!extension was successfully uninstalled.', [
      '!extension' => $extension,
    ]), LogLevel::INFO);
  }
}