You are here

public function MigrateManifest::import in Migrate Manifest 3.x

Same name and namespace in other branches
  1. 8.2 src/MigrateManifest.php \Drupal\migrate_manifest\MigrateManifest::import()
  2. 8 src/MigrateManifest.php \Drupal\migrate_manifest\MigrateManifest::import()

Drush execution method. Runs imports on the supplied manifest.

Parameters

$manifest_file: The location of the manifest file.

Return value

array A list of run migrations.

File

src/MigrateManifest.php, line 59

Class

MigrateManifest

Namespace

Drupal\migrate_manifest

Code

public function import($manifest_file) {
  $nonexistent_migrations = [];
  if (!file_exists($manifest_file)) {
    throw new FileNotFoundException($manifest_file);
  }
  $migration_manifest = Yaml::parse(file_get_contents($manifest_file));
  $migration_info = [];

  // Standardize list of migrations.
  foreach ($migration_manifest as $manifest_row) {
    if (is_array($manifest_row)) {

      // The migration is stored as the key in the info array.
      // The info will be stored underneath that key as another array.
      // Any other info is just dropped. It can't be mapped and doesn't match
      // our expected input.
      $migration_info[key($manifest_row)] = current($manifest_row);
    }
    else {

      // If it wasn't an array then the info is just the migration_id.
      $migration_info[$manifest_row] = [];
    }
  }
  $run_migrations = [];
  foreach ($migration_info as $migration_id => $config) {
    do {
      $complete = true;

      // createInstance and createInstances doesn't follow the plugin interface
      // so this won't throw an exception. Instead we have to check the return
      // value to confirm that a migration was returned.
      // https://www.drupal.org/node/2744323

      /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
      $migration_instances = $this->manager
        ->createInstances([
        $migration_id,
      ], [
        $migration_id => $config,
      ]);
      if (empty($migration_instances)) {
        $nonexistent_migrations[] = $migration_id;
        continue;
      }
      foreach ($migration_instances as $migration_instance) {
        if ($this->force == 2) {
          $migration_instance
            ->set('requirements', []);
        }
        $migrations_to_run = $this
          ->injectDependencies($migration_instance, $migration_instances);
        foreach ($migrations_to_run as $migration) {
          if (isset($run_migrations[$migration
            ->id()])) {
            continue;
          }
          if ($this->force) {
            $migration
              ->set('requirements', []);
          }
          if ($this->update) {
            $migration
              ->getIdMap()
              ->prepareUpdate();
          }

          // Store all the migrations for later.
          $run_migrations[$migration
            ->id()] = $this
            ->executeMigration($migration);
          if ($run_migrations[$migration
            ->id()] == MigrationInterface::RESULT_INCOMPLETE) {
            unset($run_migrations[$migration
              ->id()]);
            $complete = FALSE;
            break 2;
          }
        }
      }

      // Since we might be cycling because of memory leaks in the migration
      // instance, clear the references and force a gc to recover the memory.
      unset($migration_instances, $migrations_to_run);
      gc_collect_cycles();
    } while (!$complete);
  }

  // Warn the user if any migrations were not found.
  if (count($nonexistent_migrations) > 0) {
    $this->logger
      ->warning(dt('The following migrations were not found: @migrations', [
      '@migrations' => implode(',', $nonexistent_migrations),
    ]));
  }
  return $run_migrations;
}