Same name in this branch
  1. 10 core/modules/migrate_drupal/src/MigrationPluginManager.php \Drupal\migrate_drupal\MigrationPluginManager
  2. 10 core/modules/migrate/src/Plugin/MigrationPluginManager.php \Drupal\migrate\Plugin\MigrationPluginManager
Same name and namespace in other branches
  1. 8.9.x core/modules/migrate/src/Plugin/MigrationPluginManager.php \Drupal\migrate\Plugin\MigrationPluginManager
  2. 9 core/modules/migrate/src/Plugin/MigrationPluginManager.php \Drupal\migrate\Plugin\MigrationPluginManager

Plugin manager for migration plugins.

Hierarchy

  • class \Drupal\migrate\Plugin\MigrationPluginManager extends \Drupal\Core\Plugin\DefaultPluginManager implements \Drupal\migrate\Plugin\MigrationPluginManagerInterface, \Drupal\migrate\MigrateBuildDependencyInterface

Expanded class hierarchy of MigrationPluginManager

3 files declare their use of MigrationPluginManager
MigrateSqlIdMapTest.php in core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php
MigrationPluginManager.php in core/modules/migrate_drupal/src/MigrationPluginManager.php
MigrationPluginManagerTest.php in core/modules/migrate/tests/src/Unit/MigrationPluginManagerTest.php
1 string reference to 'MigrationPluginManager'
migrate.services.yml in core/modules/migrate/migrate.services.yml
core/modules/migrate/migrate.services.yml
1 service uses MigrationPluginManager
plugin.manager.migration in core/modules/migrate/migrate.services.yml
Drupal\migrate\Plugin\MigrationPluginManager

File

core/modules/migrate/src/Plugin/MigrationPluginManager.php, line 20

Namespace

Drupal\migrate\Plugin
View source
class MigrationPluginManager extends DefaultPluginManager implements MigrationPluginManagerInterface, MigrateBuildDependencyInterface {

  /**
   * Provides default values for migrations.
   *
   * @var array
   */
  protected $defaults = [
    'class' => '\\Drupal\\migrate\\Plugin\\Migration',
  ];

  /**
   * The interface the plugins should implement.
   *
   * @var string
   */
  protected $pluginInterface = 'Drupal\\migrate\\Plugin\\MigrationInterface';

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Construct a migration plugin manager.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
   *   The cache backend for the definitions.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   */
  public function __construct(ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, LanguageManagerInterface $language_manager) {
    $this->factory = new ContainerFactory($this, $this->pluginInterface);
    $this
      ->alterInfo('migration_plugins');
    $this
      ->setCacheBackend($cache_backend, 'migration_plugins');
    $this->moduleHandler = $module_handler;
  }

  /**
   * Gets the plugin discovery.
   *
   * This method overrides DefaultPluginManager::getDiscovery() in order to
   * search for migration configurations in the MODULENAME/migrations
   * directory.
   */
  protected function getDiscovery() {
    if (!isset($this->discovery)) {
      $directories = array_map(function ($directory) {
        return [
          $directory . '/migrations',
        ];
      }, $this->moduleHandler
        ->getModuleDirectories());
      $yaml_discovery = new YamlDirectoryDiscovery($directories, 'migrate');

      // This gets rid of migrations which try to use a non-existent source
      // plugin. The common case for this is if the source plugin has, or
      // specifies, a non-existent provider.
      $only_with_source_discovery = new NoSourcePluginDecorator($yaml_discovery);

      // This gets rid of migrations with explicit providers set if one of the
      // providers do not exist before we try to use a potentially non-existing
      // deriver. This is a rare case.
      $filtered_discovery = new ProviderFilterDecorator($only_with_source_discovery, [
        $this->moduleHandler,
        'moduleExists',
      ]);
      $this->discovery = new ContainerDerivativeDiscoveryDecorator($filtered_discovery);
    }
    return $this->discovery;
  }

  /**
   * {@inheritdoc}
   */
  public function createInstance($plugin_id, array $configuration = []) {
    $instances = $this
      ->createInstances([
      $plugin_id,
    ], [
      $plugin_id => $configuration,
    ]);
    return reset($instances);
  }

  /**
   * {@inheritdoc}
   */
  public function createInstances($migration_id, array $configuration = []) {
    if (empty($migration_id)) {
      $migration_id = array_keys($this
        ->getDefinitions());
    }
    $factory = $this
      ->getFactory();
    $migration_ids = (array) $migration_id;

    // We need to expand any derivative migrations. Derivative migrations are
    // calculated by migration derivers such as D6NodeDeriver. This allows
    // migrations to depend on the base id and then have a dependency on all
    // derivative migrations. For example, d6_comment depends on d6_node but
    // after we've expanded the dependencies it will depend on d6_node:page,
    // d6_node:story and so on, for other derivative migrations.
    $plugin_ids = $this
      ->expandPluginIds($migration_ids);
    $instances = [];
    foreach ($plugin_ids as $plugin_id) {
      $instances[$plugin_id] = $factory
        ->createInstance($plugin_id, $configuration[$plugin_id] ?? []);
    }

    // @todo Remove loop when the ability to call ::getMigrationDependencies()
    //   without expanding plugins is removed.
    foreach ($instances as $migration) {
      $migration
        ->set('migration_dependencies', $migration
        ->getMigrationDependencies(TRUE));
    }

    // Sort the migrations based on their dependencies.
    return $this
      ->buildDependencyMigration($instances, []);
  }

  /**
   * {@inheritdoc}
   */
  public function createInstancesByTag($tag) {
    $migrations = array_filter($this
      ->getDefinitions(), function ($migration) use ($tag) {
      return !empty($migration['migration_tags']) && in_array($tag, $migration['migration_tags']);
    });
    return $migrations ? $this
      ->createInstances(array_keys($migrations)) : [];
  }

  /**
   * {@inheritdoc}
   */
  public function expandPluginIds(array $migration_ids) {
    $plugin_ids = [];
    $all_ids = array_keys($this
      ->getDefinitions());
    foreach ($migration_ids as $id) {
      $plugin_ids = array_merge($plugin_ids, preg_grep('/^' . preg_quote($id, '/') . PluginBase::DERIVATIVE_SEPARATOR . '/', $all_ids));
      if ($this
        ->hasDefinition($id)) {
        $plugin_ids[] = $id;
      }
    }
    return $plugin_ids;
  }

  /**
   * {@inheritdoc}
   */
  public function buildDependencyMigration(array $migrations, array $dynamic_ids) {

    // Migration dependencies 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 = [];
    $required_dependency_graph = [];
    $have_optional = FALSE;
    foreach ($migrations as $migration) {

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

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

    // Sort weights, labels, and keys in the same order as each other.
    array_multisort($weights, SORT_DESC, SORT_NUMERIC, array_keys($migrations), SORT_ASC, SORT_NATURAL, $migrations);
    return $migrations;
  }

  /**
   * Add one or more dependencies to a graph.
   *
   * @param array $graph
   *   The graph so far, passed by reference.
   * @param int $id
   *   The migration ID.
   * @param string $dependency
   *   The dependency string.
   * @param array $dynamic_ids
   *   The dynamic ID mapping.
   */
  protected function addDependency(array &$graph, $id, $dependency, $dynamic_ids) {
    $dependencies = $dynamic_ids[$dependency] ?? [
      $dependency,
    ];
    if (!isset($graph[$id]['edges'])) {
      $graph[$id]['edges'] = [];
    }
    $graph[$id]['edges'] += array_combine($dependencies, $dependencies);
  }

  /**
   * {@inheritdoc}
   */
  public function createStubMigration(array $definition) {
    $id = $definition['id'] ?? uniqid();
    return Migration::create(\Drupal::getContainer(), [], $id, $definition);
  }

  /**
   * Finds plugin definitions.
   *
   * @return array
   *   List of definitions to store in cache.
   *
   * @todo This is a temporary solution to the fact that migration source
   *   plugins have more than one provider. This functionality will be moved to
   *   core in https://www.drupal.org/node/2786355.
   */
  protected function findDefinitions() {
    $definitions = $this
      ->getDiscovery()
      ->getDefinitions();
    foreach ($definitions as $plugin_id => &$definition) {
      $this
        ->processDefinition($definition, $plugin_id);
    }
    $this
      ->alterDefinitions($definitions);
    return ProviderFilterDecorator::filterDefinitions($definitions, function ($provider) {
      return $this
        ->providerExists($provider);
    });
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrationPluginManager::$defaults protected property Provides default values for migrations.
MigrationPluginManager::$moduleHandler protected property The module handler.
MigrationPluginManager::$pluginInterface protected property The interface the plugins should implement.
MigrationPluginManager::addDependency protected function Add one or more dependencies to a graph.
MigrationPluginManager::buildDependencyMigration public function
MigrationPluginManager::createInstance public function
MigrationPluginManager::createInstances public function
MigrationPluginManager::createInstancesByTag public function
MigrationPluginManager::createStubMigration public function
MigrationPluginManager::expandPluginIds public function
MigrationPluginManager::findDefinitions protected function Finds plugin definitions.
MigrationPluginManager::getDiscovery protected function Gets the plugin discovery.
MigrationPluginManager::__construct public function Construct a migration plugin manager. 1