You are here

public function MigrationStorage::buildDependencyMigration in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/migrate/src/MigrationStorage.php \Drupal\migrate\MigrationStorage::buildDependencyMigration()

Builds a dependency tree for the migrations and set their order.

Parameters

\Drupal\migrate\Entity\MigrationInterface[] $migrations: Array of loaded migrations with their declared dependencies.

array $dynamic_ids: Keys are dynamic ids (for example node:*) values are a list of loaded migration ids (for example node:page, node:article).

Return value

array An array of migrations.

Overrides MigrateBuildDependencyInterface::buildDependencyMigration

1 call to MigrationStorage::buildDependencyMigration()
MigrationStorage::loadMultiple in core/modules/migrate/src/MigrationStorage.php
Loads one or more entities.

File

core/modules/migrate/src/MigrationStorage.php, line 120
Contains \Drupal\migrate\MigrationStorage.

Class

MigrationStorage
Storage for migration entities.

Namespace

Drupal\migrate

Code

public function buildDependencyMigration(array $migrations, array $dynamic_ids) {

  // Migration dependencies defined in the migration storage can be
  // optional or required. If an optional dependency does not run, the current
  // migration is still OK to go. Both optional and required dependencies
  // (if run at all) must run before the current migration.
  $dependency_graph = array();
  $requirement_graph = array();
  $different = FALSE;
  foreach ($migrations as $migration) {

    /** @var \Drupal\migrate\Entity\MigrationInterface $migration */
    $id = $migration
      ->id();
    $requirements[$id] = array();
    $dependency_graph[$id]['edges'] = array();
    $migration_dependencies = $migration
      ->getMigrationDependencies();
    if (isset($migration_dependencies['required'])) {
      foreach ($migration_dependencies['required'] as $dependency) {
        if (!isset($dynamic_ids[$dependency])) {
          $this
            ->addDependency($requirement_graph, $id, $dependency, $dynamic_ids);
        }
        $this
          ->addDependency($dependency_graph, $id, $dependency, $dynamic_ids);
      }
    }
    if (isset($migration_dependencies['optional'])) {
      foreach ($migration_dependencies['optional'] as $dependency) {
        $different = TRUE;
        $this
          ->addDependency($dependency_graph, $id, $dependency, $dynamic_ids);
      }
    }
  }
  $graph_object = new Graph($dependency_graph);
  $dependency_graph = $graph_object
    ->searchAndSort();
  if ($different) {
    $graph_object = new Graph($requirement_graph);
    $requirement_graph = $graph_object
      ->searchAndSort();
  }
  else {
    $requirement_graph = $dependency_graph;
  }
  $weights = array();
  foreach ($migrations as $migration_id => $migration) {

    // Populate a weights array to use with array_multisort later.
    $weights[] = $dependency_graph[$migration_id]['weight'];
    if (!empty($requirement_graph[$migration_id]['paths'])) {
      $migration
        ->set('requirements', $requirement_graph[$migration_id]['paths']);
    }
  }
  array_multisort($weights, SORT_DESC, SORT_NUMERIC, $migrations);
  return $migrations;
}