You are here

public function DrupalmoduleupgraderCommands::upgrade in Drupal 7 to 8/9 Module Upgrader 8

Upgrades a Drupal 7 module to Drupal 8 or Drupal 9.

@option backup If set, creates a backup copy of the module before conversion. @option only A comma-separated list of converters to run, excluding all others. @option skip A comma-separated list of converters to skip. @option path Optional path to the target module. Will be determined automatically if omitted. @usage drush dmu-upgrade pants Upgrade whatever can be automatically upgraded in the pants module.

@command dmu:upgrade @aliases dmu-upgrade

Parameters

string $module: The machine name of a Drupal 7 module.

array $options: An associative array of options whose values come from cli, aliases, config, etc.

File

src/Commands/DrupalmoduleupgraderCommands.php, line 165

Class

DrupalmoduleupgraderCommands

Namespace

Drupal\drupalmoduleupgrader\Commands

Code

public function upgrade(string $module, array $options = [
  'backup' => NULL,
  'only' => NULL,
  'skip' => NULL,
  'path' => NULL,
]) {
  $target = $this
    ->dmuBuildTarget($module, $options['path']);
  $module_path = $this
    ->dmuGetDirectory($module);
  if (is_null($module_path)) {
    return;
  }
  if (file_exists($module_path . '/' . $module . '.info.yml')) {
    $this
      ->output()
      ->writeln("You have already run dmu upgrade command for this module.");
  }
  if (isset($options['backup']) && $options['backup']) {
    $fs = new Filesystem();
    $backup_at = $target
      ->getBasePath() . '.bak';
    $fs
      ->mirror($target
      ->getBasePath(), $backup_at);
    $this
      ->logger()
      ->info('Created backup at @path', [
      '@path' => $backup_at,
    ]);
  }
  foreach ($this
    ->dmuPluginList('converter', $options) as $id) {

    /** @var \Drupal\drupalmoduleupgrader\ConverterInterface $converter */
    $converter = $this->converterPluginManager
      ->createInstance($id);
    if ($converter
      ->isExecutable($target)) {
      $this
        ->logger()
        ->info('Executing plugin: @plugin_id', [
        '@plugin_id' => $id,
      ]);
      try {
        $converter
          ->convert($target);
      } catch (\Exception $e) {
        $this
          ->logger()
          ->error($e
          ->getMessage());

        // Being a notice, the stack trace will only appear in verbose mode.
        $this
          ->logger()
          ->notice($e
          ->getTraceAsString());
      }
    }
  }
}