You are here

protected function MigrateToolsCommands::migrationsList in Migrate Tools 8.5

Same name and namespace in other branches
  1. 8.4 src/Commands/MigrateToolsCommands.php \Drupal\migrate_tools\Commands\MigrateToolsCommands::migrationsList()

Retrieve a list of active migrations.

@default $options []

Parameters

string $migration_ids: Comma-separated list of migrations - if present, return only these migrations.

array $options: Command options.

Return value

\Drupal\migrate\Plugin\MigrationInterface[][] An array keyed by migration group, each value containing an array of migrations or an empty array if no migrations match the input criteria.

4 calls to MigrateToolsCommands::migrationsList()
MigrateToolsCommands::dependencyTree in src/Commands/MigrateToolsCommands.php
Shows a tree of migration dependencies.
MigrateToolsCommands::import in src/Commands/MigrateToolsCommands.php
Perform one or more migration processes.
MigrateToolsCommands::rollback in src/Commands/MigrateToolsCommands.php
Rollback one or more migrations.
MigrateToolsCommands::status in src/Commands/MigrateToolsCommands.php
List all migrations with current status.

File

src/Commands/MigrateToolsCommands.php, line 790

Class

MigrateToolsCommands
Migrate Tools drush commands.

Namespace

Drupal\migrate_tools\Commands

Code

protected function migrationsList($migration_ids = '', array $options = []) {

  // Filter keys must match the migration configuration property name.
  $filter['migration_group'] = explode(',', $options['group']);
  $filter['migration_tags'] = explode(',', $options['tag']);
  $manager = $this->migrationPluginManager;
  $matched_migrations = [];
  if (empty($migration_ids)) {

    // Get all migrations.
    $plugins = $manager
      ->createInstances([]);
    $matched_migrations = $plugins;
  }
  else {

    // Get the requested migrations.
    $migration_ids = explode(',', mb_strtolower($migration_ids));
    $definitions = $manager
      ->getDefinitions();
    foreach ($migration_ids as $given_migration_id) {
      if (isset($definitions[$given_migration_id])) {
        $matched_migrations[$given_migration_id] = $manager
          ->createInstance($given_migration_id);
      }
      else {
        $error_message = dt('Migration @id does not exist', [
          '@id' => $given_migration_id,
        ]);
        if ($options['continue-on-failure']) {
          $this
            ->loggProcessPluginBaseer()
            ->error($error_message);
        }
        else {
          throw new \Exception($error_message);
        }
      }
    }
  }

  // Do not return any migrations which fail to meet requirements.

  /** @var \Drupal\migrate\Plugin\Migration $migration */
  foreach ($matched_migrations as $id => $migration) {
    try {
      if ($migration
        ->getSourcePlugin() instanceof RequirementsInterface) {
        $migration
          ->getSourcePlugin()
          ->checkRequirements();
      }
    } catch (RequirementsException $e) {
      unset($matched_migrations[$id]);
    } catch (PluginNotFoundException $exception) {
      if ($options['continue-on-failure']) {
        $this
          ->logger()
          ->error($exception
          ->getMessage());
        unset($matched_migrations[$id]);
      }
      else {
        throw $exception;
      }
    }
  }

  // Filters the matched migrations if a group or a tag has been input.
  if (!empty($filter['migration_group']) || !empty($filter['migration_tags'])) {

    // Get migrations in any of the specified groups and with any of the
    // specified tags.
    foreach ($filter as $property => $values) {
      if (!empty($values)) {
        $filtered_migrations = [];
        foreach ($values as $search_value) {
          foreach ($matched_migrations as $id => $migration) {

            // Cast to array because migration_tags can be an array.
            $definition = $migration
              ->getPluginDefinition();
            $configured_values = (array) ($definition[$property] ?? NULL);
            $configured_id = in_array($search_value, $configured_values, TRUE) ? $search_value : 'default';
            if (empty($search_value) || $search_value === $configured_id) {
              if (empty($migration_ids) || in_array(mb_strtolower($id), $migration_ids, TRUE)) {
                $filtered_migrations[$id] = $migration;
              }
            }
          }
        }
        $matched_migrations = $filtered_migrations;
      }
    }
  }

  // Sort the matched migrations by group.
  if (!empty($matched_migrations)) {
    foreach ($matched_migrations as $id => $migration) {
      $configured_group_id = empty($migration->migration_group) ? 'default' : $migration->migration_group;
      $migrations[$configured_group_id][$id] = $migration;
    }
  }
  return $migrations ?? [];
}