You are here

public function MigrateToolsCommands::dependencyTree in Migrate Tools 8.5

Shows a tree of migration dependencies.

@command migrate:tree

Parameters

string $migration_names: Restrict to a comma-separated list of migrations (Optional).

array $options: Additional options for the command.

File

src/Commands/MigrateToolsCommands.php, line 91

Class

MigrateToolsCommands
Migrate Tools drush commands.

Namespace

Drupal\migrate_tools\Commands

Code

public function dependencyTree($migration_names = '', array $options = [
  'all' => FALSE,
  'group' => self::REQ,
  'tag' => self::REQ,
]) {
  $manager = $this->migrationPluginManager;
  $migrations = $this
    ->migrationsList($migration_names, $options);

  // Turn this into a flat array.
  $migrations_to_process = [];
  foreach ($migrations as $group => $group_migrations) {
    foreach ($group_migrations as $migration) {
      $migrations_to_process[$migration
        ->id()] = $migration;
    }
  }

  // Create a dependency graph. The migrations in the given list may have
  // dependencies not in the list, so we need to add those to the list as we
  // go.
  $dependency_graph = [];
  while (!empty($migrations_to_process)) {

    // Get the next migration in the list.
    $migration = reset($migrations_to_process);
    unset($migrations_to_process[$migration
      ->id()]);

    // Add its dependencies to the graph and to the list.
    $migration_dependencies = $migration
      ->getMigrationDependencies();
    $dependency_graph[$migration
      ->id()]['edges'] = [];
    if (isset($migration_dependencies['required'])) {
      foreach ($migration_dependencies['required'] as $dependency) {
        $dependency_graph[$migration
          ->id()]['edges'][$dependency] = $dependency;
        $migrations_to_process[$dependency] = $manager
          ->createInstance($dependency);
      }
    }
  }
  $dependency_graph = (new Graph($dependency_graph))
    ->searchAndSort();

  // Get the list of top-level migrations, that is, those that nothing depends
  // on.
  $top_level_migrations = [];
  foreach ($dependency_graph as $migration_name => $vertex) {
    if (empty($vertex['reverse_paths'])) {
      $top_level_migrations[] = $migration_name;
    }
  }
  foreach ($top_level_migrations as $migration_name) {
    $this
      ->output()
      ->writeln($migration_name);
    $this
      ->printDependencies(0, '', $dependency_graph, $migration_name);
  }
}