You are here

public function MmmfFixCommand::fixCommand in Module Missing Message Fixer 8

Same name and namespace in other branches
  1. 2.0.x src/Commands/MmmfFixCommand.php \Drupal\module_missing_message_fixer\Commands\MmmfFixCommand::fixCommand()

Fixes the missing modules.

@command module-missing-message-fixer:fix

@option all Fixes all module missing messages.

@usage drush module-missing-message-fixer:fix stuff Fixes the stuff module. @usage drush module-missing-message-fixer:fix stuff --all Fixes all the modules.

@aliases mmmff

Parameters

string $name: The name of the module to fix messages for.

array $options: The all flag option.

File

src/Commands/MmmfFixCommand.php, line 73

Class

MmmfFixCommand
Class MmmfFixCommand.

Namespace

Drupal\module_missing_message_fixer\Commands

Code

public function fixCommand($name = NULL, array $options = [
  'all' => NULL,
]) {
  $modules = [];
  $rows = $this->fixer
    ->checkModules(TRUE);
  if ($options['all'] !== NULL) {
    if (!empty($rows)) {
      foreach ($rows as $row) {
        $modules[] = $row['name'];

        // Clean up old migrate configuration.
        $like = $this->connection
          ->escapeLike($row['name'] . '.');
        $config_names = $this->connection
          ->select('config', 'c')
          ->fields('c', [
          'name',
        ])
          ->condition('name', $like . '%', 'LIKE')
          ->execute()
          ->fetchAll();

        // Delete each config using configFactory.
        foreach ($config_names as $config_name) {
          $this->configFactory
            ->getEditable($config_name->name)
            ->delete();
        }

        // Reminds users to export config.
        if (!empty($config_name)) {
          $this
            ->output()
            ->writeln(dt("Don't forget to export your config"));
        }
      }
    }
  }
  elseif ($name !== NULL) {

    // If this exists in the table.
    if (strpos(json_encode($rows), $name)) {
      $modules[] = $name;
    }
    else {
      $this
        ->output()
        ->writeln(dt('Module ' . $name . ' was not found.'), 'error');
    }
  }
  else {
    $this
      ->output()
      ->writeln(dt('Missing input, provide module name or run with --all'), 'error');
  }

  // Delete if there is no modules.
  if (count($modules) > 0) {
    $query = $this->connection
      ->delete('key_value');
    $query
      ->condition('collection', 'system.schema');
    $query
      ->condition('name', $modules, 'IN');
    $query
      ->execute();
    if ($options['all'] !== NULL) {
      $this
        ->output()
        ->writeln(dt('All missing references have been removed.'), 'success');
    }
    elseif ($name !== NULL) {
      if (in_array($name, $modules, TRUE)) {
        $this
          ->output()
          ->writeln(dt('Reference to ' . $name . ' (if found) has been removed.'), 'success');
      }
    }
  }
}