Commit 2c35bee1 authored by catch's avatar catch

Issue #2666650 by alexpott, benjy: Inject migration and source plugin managers...

Issue #2666650 by alexpott, benjy: Inject migration and source plugin managers into the migration plugins
parent 8a8fc5b5
......@@ -7,11 +7,13 @@
namespace Drupal\migrate\Plugin;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\migrate\Exception\RequirementsException;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\Component\Utility\NestedArray;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines the Migration plugin.
......@@ -20,7 +22,7 @@
* container for the information about a single migration such as the source,
* process and destination plugins.
*/
class Migration extends PluginBase implements MigrationInterface, RequirementsInterface {
class Migration extends PluginBase implements MigrationInterface, RequirementsInterface, ContainerFactoryPluginInterface {
/**
* The migration ID (machine name).
......@@ -217,6 +219,33 @@ class Migration extends PluginBase implements MigrationInterface, RequirementsIn
*/
protected $migrationPluginManager;
/**
* The source plugin manager.
*
* @var \Drupal\migrate\Plugin\MigratePluginManager
*/
protected $sourcePluginManager;
/**
* Thep process plugin manager.
*
* @var \Drupal\migrate\Plugin\MigratePluginManager
*/
protected $processPluginManager;
/**
* The destination plugin manager.
*
* @var \Drupal\migrate\Plugin\MigrateDestinationPluginManager
*/
protected $destinationPluginManager;
/**
* The ID map plugin manager.
*
* @var \Drupal\migrate\Plugin\MigratePluginManager
*/
protected $idMapPluginManager;
/**
* Labels corresponding to each defined status.
......@@ -232,15 +261,54 @@ class Migration extends PluginBase implements MigrationInterface, RequirementsIn
];
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
* Constructs a Migration.
*
* @param array $configuration
* Plugin configuration.
* @param string $plugin_id
* The plugin ID.
* @param mixed $plugin_definition
* The plugin definition.
* @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $migration_plugin_manager
* The migration plugin manager.
* @param \Drupal\migrate\Plugin\MigratePluginManager $source_plugin_manager
* The source migration plugin manager.
* @param \Drupal\migrate\Plugin\MigratePluginManager $process_plugin_manager
* The process migration plugin manager.
* @param \Drupal\migrate\Plugin\MigrateDestinationPluginManager $destination_plugin_manager
* The destination migration plugin manager.
* @param \Drupal\migrate\Plugin\MigratePluginManager $idmap_plugin_manager
* The ID map migration plugin manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationPluginManagerInterface $migration_plugin_manager, MigratePluginManager $source_plugin_manager, MigratePluginManager $process_plugin_manager, MigrateDestinationPluginManager $destination_plugin_manager, MigratePluginManager $idmap_plugin_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->migrationPluginManager = $migration_plugin_manager;
$this->sourcePluginManager = $source_plugin_manager;
$this->processPluginManager = $process_plugin_manager;
$this->destinationPluginManager = $destination_plugin_manager;
$this->idMapPluginManager = $idmap_plugin_manager;
foreach ($plugin_definition as $key => $value) {
$this->$key = $value;
}
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('plugin.manager.migration'),
$container->get('plugin.manager.migrate.source'),
$container->get('plugin.manager.migrate.process'),
$container->get('plugin.manager.migrate.destination'),
$container->get('plugin.manager.migrate.id_map')
);
}
/**
* {@inheritdoc}
*/
......@@ -283,7 +351,7 @@ public function getIdMapPlugin() {
*/
public function getSourcePlugin() {
if (!isset($this->sourcePlugin)) {
$this->sourcePlugin = \Drupal::service('plugin.manager.migrate.source')->createInstance($this->source['plugin'], $this->source, $this);
$this->sourcePlugin = $this->sourcePluginManager->createInstance($this->source['plugin'], $this->source, $this);
}
return $this->sourcePlugin;
}
......@@ -302,11 +370,11 @@ public function getProcessPlugins(array $process = NULL) {
$this->processPlugins[$index][$property] = array();
foreach ($configurations as $configuration) {
if (isset($configuration['source'])) {
$this->processPlugins[$index][$property][] = \Drupal::service('plugin.manager.migrate.process')->createInstance('get', $configuration, $this);
$this->processPlugins[$index][$property][] = $this->processPluginManager->createInstance('get', $configuration, $this);
}
// Get is already handled.
if ($configuration['plugin'] != 'get') {
$this->processPlugins[$index][$property][] = \Drupal::service('plugin.manager.migrate.process')->createInstance($configuration['plugin'], $configuration, $this);
$this->processPlugins[$index][$property][] = $this->processPluginManager->createInstance($configuration['plugin'], $configuration, $this);
}
if (!$this->processPlugins[$index][$property]) {
throw new MigrateException("Invalid process configuration for $property");
......@@ -351,7 +419,7 @@ public function getDestinationPlugin($stub_being_requested = FALSE) {
throw new MigrateSkipRowException;
}
if (!isset($this->destinationPlugin)) {
$this->destinationPlugin = \Drupal::service('plugin.manager.migrate.destination')->createInstance($this->destination['plugin'], $this->destination, $this);
$this->destinationPlugin = $this->destinationPluginManager->createInstance($this->destination['plugin'], $this->destination, $this);
}
return $this->destinationPlugin;
}
......@@ -363,7 +431,7 @@ public function getIdMap() {
if (!isset($this->idMapPlugin)) {
$configuration = $this->idMap;
$plugin = isset($configuration['plugin']) ? $configuration['plugin'] : 'sql';
$this->idMapPlugin = \Drupal::service('plugin.manager.migrate.id_map')->createInstance($plugin, $configuration, $this);
$this->idMapPlugin = $this->idMapPluginManager->createInstance($plugin, $configuration, $this);
}
return $this->idMapPlugin;
}
......@@ -434,9 +502,6 @@ public function checkRequirements() {
* The plugin manager.
*/
protected function getMigrationPluginManager() {
if (!isset($this->migrationPluginManager)) {
$this->migrationPluginManager = \Drupal::service('plugin.manager.migration');
}
return $this->migrationPluginManager;
}
......
......@@ -31,7 +31,7 @@ public static function getSourcePlugin($source_plugin_id) {
'plugin' => 'null',
],
];
return (new Migration([], uniqid(), $definition))->getSourcePlugin();
return \Drupal::service('plugin.manager.migration')->createStubMigration($definition)->getSourcePlugin();
}
}
......@@ -223,4 +223,12 @@ protected function addDependency(array &$graph, $id, $dependency, $dynamic_ids)
$graph[$id]['edges'] += array_combine($dependencies, $dependencies);
}
/**
* {@inheritdoc}
*/
public function createStubMigration(array $definition) {
$id = isset($definition['id']) ? $definition['id'] : uniqid();
return Migration::create(\Drupal::getContainer(), [], $id, $definition);
}
}
......@@ -33,4 +33,16 @@ interface MigrationPluginManagerInterface extends PluginManagerInterface {
*/
public function createInstances($id, array $configuration = array());
/**
* Creates a stub migration plugin from a definition array.
*
* @param array $definition
* The migration definition. If an 'id' key is set then this will be used as
* the migration ID, if not a random ID will be assigned.
*
* @return \Drupal\migrate\Plugin\Migration
* The stub migration.
*/
public function createStubMigration(array $definition);
}
......@@ -7,7 +7,6 @@
namespace Drupal\Tests\migrate\Kernel;
use Drupal\migrate\Plugin\Migration;
use Drupal\KernelTests\KernelTestBase;
/**
......@@ -44,7 +43,7 @@ public function testEmbeddedData() {
'destination' => ['plugin' => 'null'],
];
$migration = new Migration([], uniqid(), $definition);
$migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
$source = $migration->getSourcePlugin();
// Validate the plugin returns the source data that was provided.
......
......@@ -15,7 +15,6 @@
use Drupal\migrate\MigrateMessage;
use Drupal\migrate\Event\MigrateEvents;
use Drupal\migrate\MigrateExecutable;
use Drupal\migrate\Plugin\Migration;
use Drupal\KernelTests\KernelTestBase;
/**
......@@ -80,7 +79,7 @@ public function testMigrateEvents() {
'destination' => ['plugin' => 'dummy'],
];
$migration = new Migration([], uniqid(), $definition);
$migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
$executable = new MigrateExecutable($migration, new MigrateMessage());
// As the import runs, events will be dispatched, recording the received
......
......@@ -7,7 +7,6 @@
namespace Drupal\Tests\migrate\Kernel;
use Drupal\migrate\Plugin\Migration;
use Drupal\migrate\Event\MigratePostRowSaveEvent;
use Drupal\migrate\MigrateMessage;
use Drupal\migrate\Plugin\MigrationInterface;
......@@ -60,7 +59,7 @@ public function testMigrateEvents() {
'destination' => ['plugin' => 'dummy'],
];
$migration = new Migration([], uniqid(), $definition);
$migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
$executable = new MigrateExecutable($migration, new MigrateMessage());
// When the import runs, the first row imported will trigger an
......
......@@ -8,7 +8,6 @@
namespace Drupal\Tests\migrate\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\migrate\Plugin\Migration;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Event\MigrateEvents;
use Drupal\migrate\Event\MigrateIdMapMessageEvent;
......@@ -76,7 +75,7 @@ protected function setUp() {
],
];
$this->migration = new Migration([], uniqid(), $definition);
$this->migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
}
/**
......
......@@ -7,7 +7,6 @@
namespace Drupal\Tests\migrate\Kernel;
use Drupal\migrate\Plugin\Migration;
use Drupal\migrate\MigrateExecutable;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Row;
......@@ -66,7 +65,7 @@ public function testRollback() {
'destination' => ['plugin' => 'entity:taxonomy_vocabulary'],
];
$vocabulary_migration = new Migration([], uniqid(), $definition);
$vocabulary_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
$vocabulary_id_map = $vocabulary_migration->getIdMap();
$this->assertTrue($vocabulary_migration->getDestinationPlugin()->supportsRollback());
......@@ -107,7 +106,7 @@ public function testRollback() {
'migration_dependencies' => ['required' => ['vocabularies']],
];
$term_migration = new Migration([], uniqid(), $definition);
$term_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
$term_id_map = $term_migration->getIdMap();
$this->assertTrue($term_migration->getDestinationPlugin()->supportsRollback());
......@@ -180,7 +179,7 @@ public function testRollback() {
'migration_dependencies' => ['required' => ['vocabularies']],
];
$settings_migration = new Migration([], uniqid(), $definition);
$settings_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
$this->assertFalse($settings_migration->getDestinationPlugin()->supportsRollback());
}
......
......@@ -12,7 +12,6 @@
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\MigrateExecutable;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Plugin\Migration;
/**
* Tests row skips triggered during hook_migrate_prepare_row().
......@@ -54,7 +53,7 @@ public function testPrepareRowSkip() {
'load' => ['plugin' => 'null'],
];
$migration = new Migration([], uniqid(), $definition);
$migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
$executable = new MigrateExecutable($migration, new MigrateMessage());
$result = $executable->import();
......
......@@ -7,7 +7,6 @@
namespace Drupal\Tests\migrate\Kernel;
use Drupal\migrate\Plugin\Migration;
use Drupal\migrate\Plugin\MigrationInterface;
/**
......@@ -32,7 +31,7 @@ public function testStatus() {
],
'process' => ['foo' => 'bar'],
];
$migration = new Migration([], uniqid(), $definition);
$migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
// Default status is idle.
$status = $migration->getStatus();
......
......@@ -8,7 +8,6 @@
namespace Drupal\Tests\migrate\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\migrate\Plugin\Migration;
/**
* Tests the migration plugin.
......@@ -32,7 +31,7 @@ class MigrationTest extends KernelTestBase {
* @covers ::set
*/
public function testSetInvalidation() {
$migration = new Migration([], uniqid(), [
$migration = \Drupal::service('plugin.manager.migration')->createStubMigration([
'source' => ['plugin' => 'empty'],
'destination' => ['plugin' => 'entity:entity_view_mode'],
]);
......
......@@ -2,21 +2,25 @@
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\Entity\MigrationTest.
* Contains \Drupal\Tests\migrate\Kernel\Plugin\MigrationTest.
*/
namespace Drupal\Tests\migrate\Unit\Entity;
namespace Drupal\Tests\migrate\Kernel\Plugin;
use Drupal\migrate\Plugin\Migration;
use Drupal\Tests\UnitTestCase;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests the migrate entity.
* Tests the migration plugin.
*
* @coversDefaultClass \Drupal\migrate\Plugin\Migration
* @group migrate
*/
class MigrationTest extends UnitTestCase {
class MigrationTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['migrate'];
/**
* Tests Migration::getProcessPlugins()
......@@ -24,7 +28,7 @@ class MigrationTest extends UnitTestCase {
* @covers ::getProcessPlugins
*/
public function testGetProcessPlugins() {
$migration = new Migration([], uniqid(), []);
$migration = \Drupal::service('plugin.manager.migration')->createStubMigration([]);
$this->assertEquals([], $migration->getProcessPlugins([]));
}
......
......@@ -10,7 +10,6 @@
use Drupal\migrate\Exception\RequirementsException;
use Drupal\migrate\MigrateExecutable;
use Drupal\migrate\MigrateMessage;
use Drupal\migrate\Plugin\Migration;
use Drupal\migrate\Plugin\RequirementsInterface;
/**
......@@ -43,7 +42,7 @@ function migrate_drupal_migration_plugins_alter(&$definitions) {
'plugin' => 'null',
],
];
$vocabulary_migration = new Migration([], uniqid(), $vocabulary_migration_definition);
$vocabulary_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($vocabulary_migration_definition);
try {
$source_plugin = $vocabulary_migration->getSourcePlugin();
......
......@@ -9,6 +9,7 @@
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\Exception\RequirementsException;
use Drupal\migrate\Plugin\MigrateDestinationPluginManager;
use Drupal\migrate\Plugin\MigratePluginManager;
use Drupal\migrate\Plugin\Migration;
use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
......@@ -41,13 +42,6 @@ class CckMigration extends Migration implements ContainerFactoryPluginInterface
*/
protected $cckPluginCache;
/**
* The migration plugin manager.
*
* @var \Drupal\Component\Plugin\PluginManagerInterface
*/
protected $migrationPluginManager;
/**
* Constructs a CckMigration.
*
......@@ -61,11 +55,18 @@ class CckMigration extends Migration implements ContainerFactoryPluginInterface
* The cckfield plugin manager.
* @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $migration_plugin_manager
* The migration plugin manager.
* @param \Drupal\migrate\Plugin\MigratePluginManager $source_plugin_manager
* The source migration plugin manager.
* @param \Drupal\migrate\Plugin\MigratePluginManager $process_plugin_manager
* The process migration plugin manager.
* @param \Drupal\migrate\Plugin\MigrateDestinationPluginManager $destination_plugin_manager
* The destination migration plugin manager.
* @param \Drupal\migrate\Plugin\MigratePluginManager $idmap_plugin_manager
* The ID map migration plugin manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigratePluginManager $cck_manager, MigrationPluginManagerInterface $migration_plugin_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigratePluginManager $cck_manager, MigrationPluginManagerInterface $migration_plugin_manager, MigratePluginManager $source_plugin_manager, MigratePluginManager $process_plugin_manager, MigrateDestinationPluginManager $destination_plugin_manager, MigratePluginManager $idmap_plugin_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration_plugin_manager, $source_plugin_manager, $process_plugin_manager, $destination_plugin_manager, $idmap_plugin_manager);
$this->cckPluginManager = $cck_manager;
$this->migrationPluginManager = $migration_plugin_manager;
}
/**
......@@ -77,7 +78,11 @@ public static function create(ContainerInterface $container, array $configuratio
$plugin_id,
$plugin_definition,
$container->get('plugin.manager.migrate.cckfield'),
$container->get('plugin.manager.migration')
$container->get('plugin.manager.migration'),
$container->get('plugin.manager.migrate.source'),
$container->get('plugin.manager.migrate.process'),
$container->get('plugin.manager.migrate.destination'),
$container->get('plugin.manager.migrate.id_map')
);
}
......
......@@ -6,7 +6,7 @@
*/
namespace Drupal\migrate_drupal\Tests;
use Drupal\migrate\Plugin\Migration;
use Drupal\migrate\Row;
/**
......@@ -51,7 +51,7 @@ protected function createStub($entity_type_id) {
'process' => [],
'destination' => ['plugin' => 'entity:' . $entity_type_id],
];
$migration = new Migration([], uniqid(), $definition);
$migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
$destination_plugin = $migration->getDestinationPlugin(TRUE);
$stub_row = new Row([], [], TRUE);
$destination_ids = $destination_plugin->import($stub_row);
......
......@@ -12,7 +12,6 @@
use Drupal\Core\Database\DatabaseExceptionWrapper;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\migrate\Exception\RequirementsException;
use Drupal\migrate\Plugin\Migration;
use Drupal\migrate\Plugin\MigrationDeriverTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -111,7 +110,7 @@ public function getDerivativeDefinitions($base_plugin_definition) {
$values['migration_dependencies']['required'][] = 'd6_node:' . $node_type;
}
$migration = new Migration([], uniqid(), $values);
$migration = \Drupal::service('plugin.manager.migration')->createStubMigration($values);
if (isset($fields[$node_type])) {
foreach ($fields[$node_type] as $field_name => $info) {
$field_type = $info['type'];
......
......@@ -12,7 +12,6 @@
use Drupal\Core\Database\DatabaseExceptionWrapper;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\migrate\Exception\RequirementsException;
use Drupal\migrate\Plugin\Migration;
use Drupal\migrate\Plugin\MigrationDeriverTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -99,7 +98,7 @@ public function getDerivativeDefinitions($base_plugin_definition) {
]);
$values['source']['node_type'] = $node_type;
$migration = new Migration([], uniqid(), $values);
$migration = \Drupal::service('plugin.manager.migration')->createStubMigration($values);
if (isset($fields[$node_type])) {
foreach ($fields[$node_type] as $field_name => $info) {
$field_type = $info['type'];
......
......@@ -7,7 +7,6 @@
namespace Drupal\Tests\taxonomy\Kernel\Migrate;
use Drupal\migrate\Plugin\Migration;
use Drupal\migrate\MigrateExecutable;
use Drupal\Tests\migrate_drupal\Kernel\MigrateDrupalTestBase;
use Drupal\migrate_drupal\Tests\StubTestTrait;
......@@ -69,7 +68,7 @@ public function testStubWithWeightMapping() {
],
'destination' => ['plugin' => 'entity:taxonomy_vocabulary'],
];
$vocabulary_migration = new Migration([], uniqid(), $definition);
$vocabulary_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
$vocabulary_executable = new MigrateExecutable($vocabulary_migration, $this);
$vocabulary_executable->import();
......
......@@ -34,7 +34,7 @@ public function getProcess() {
] + $this->source;
$definition['destination']['plugin'] = 'null';
try {
$profile_field_migration = new Migration([], uniqid(), $definition);
$profile_field_migration = $this->migrationPluginManager->createStubMigration($definition);
$source_plugin = $profile_field_migration->getSourcePlugin();
$source_plugin->checkRequirements();
foreach ($source_plugin as $row) {
......
......@@ -35,7 +35,7 @@ public function getProcess() {
$definition['destination']['plugin'] = 'null';
if (\Drupal::moduleHandler()->moduleExists('field')) {
$definition['source']['plugin'] = 'd7_field_instance';
$field_migration = new Migration([], uniqid(), $definition);
$field_migration = $this->migrationPluginManager->createStubMigration($definition);
foreach ($field_migration->getSourcePlugin() as $row) {
$field_name = $row->getSourceProperty('field_name');
$this->process[$field_name] = $field_name;
......@@ -43,7 +43,7 @@ public function getProcess() {
}
try {
$definition['source']['plugin'] = 'profile_field';
$profile_migration = new Migration([], uniqid(), $definition);
$profile_migration = $this->migrationPluginManager->createStubMigration($definition);
// Ensure that Profile is enabled in the source DB.
$profile_migration->checkRequirements();
foreach ($profile_migration->getSourcePlugin() as $row) {
......
......@@ -7,7 +7,6 @@
namespace Drupal\Tests\user\Kernel\Migrate;
use Drupal\migrate\Plugin\Migration;
use Drupal\Tests\migrate\Kernel\MigrateTestBase;
use Drupal\user\Entity\User;
......@@ -94,7 +93,7 @@ public function testAdminPasswordPreserved() {
],
'destination' => ['plugin' => 'entity:user'],
];
$migration = new Migration([], $definition['id'], $definition);
$migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
$this->executeMigration($migration);
// Verify that admin username and email were changed, but password was not.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment