diff --git a/core/modules/migrate/src/Plugin/Migration.php b/core/modules/migrate/src/Plugin/Migration.php index 2bf6b6f0f1..20b8ae877b 100644 --- a/core/modules/migrate/src/Plugin/Migration.php +++ b/core/modules/migrate/src/Plugin/Migration.php @@ -2,14 +2,12 @@ namespace Drupal\migrate\Plugin; -use Drupal\Component\Plugin\ConfigurablePluginInterface; 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 Drupal\migrate\Plugin\migrate\ConfigurablePluginTrait; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -19,9 +17,21 @@ * container for the information about a single migration such as the source, * process and destination plugins. */ -class Migration extends PluginBase implements MigrationInterface, RequirementsInterface, ContainerFactoryPluginInterface, ConfigurablePluginInterface { +class Migration extends PluginBase implements MigrationInterface, RequirementsInterface, ContainerFactoryPluginInterface { - use ConfigurablePluginTrait; + /** + * The migration ID (machine name). + * + * @var string + */ + protected $id; + + /** + * The human-readable label for the migration. + * + * @var string + */ + protected $label; /** * The plugin ID for the row. @@ -30,6 +40,15 @@ class Migration extends PluginBase implements MigrationInterface, RequirementsIn */ protected $row; + /** + * The source configuration, with at least a 'plugin' key. + * + * Used to initialize the $sourcePlugin. + * + * @var array + */ + protected $source; + /** * The source plugin. * @@ -37,6 +56,16 @@ class Migration extends PluginBase implements MigrationInterface, RequirementsIn */ protected $sourcePlugin; + /** + * The configuration describing the process plugins. + * + * This is a strictly internal property and should not returned to calling + * code, use getProcess() instead. + * + * @var array + */ + protected $process = []; + /** * The cached process plugins. * @@ -44,6 +73,15 @@ class Migration extends PluginBase implements MigrationInterface, RequirementsIn */ protected $processPlugins = []; + /** + * The destination configuration, with at least a 'plugin' key. + * + * Used to initialize $destinationPlugin. + * + * @var array + */ + protected $destination; + /** * The destination plugin. * @@ -51,6 +89,15 @@ class Migration extends PluginBase implements MigrationInterface, RequirementsIn */ protected $destinationPlugin; + /** + * The identifier map data. + * + * Used to initialize $idMapPlugin. + * + * @var string + */ + protected $idMap = []; + /** * The identifier map. * @@ -93,6 +140,67 @@ class Migration extends PluginBase implements MigrationInterface, RequirementsIn */ protected $trackLastImported = FALSE; + /** + * These migrations must be already executed before this migration can run. + * + * @var array + */ + protected $requirements = []; + + /** + * An optional list of tags, used by the plugin manager for filtering. + * + * @var array + */ + protected $migration_tags = []; + + /** + * Whether the migration is auditable. + * + * If set to TRUE, the migration's IDs will be audited. This means that, if + * the highest destination ID is greater than the highest source ID, a warning + * will be displayed that entities might be overwritten. + * + * @var bool + */ + protected $audit = FALSE; + + /** + * These migrations, if run, must be executed before this migration. + * + * These are different from the configuration dependencies. Migration + * dependencies are only used to store relationships between migrations. + * + * The migration_dependencies value is structured like this: + * @code + * array( + * 'required' => array( + * // An array of migration IDs that must be run before this migration. + * ), + * 'optional' => array( + * // An array of migration IDs that, if they exist, must be run before + * // this migration. + * ), + * ); + * @endcode + * + * @var array + */ + protected $migration_dependencies = []; + + /** + * The migration's configuration dependencies. + * + * These store any dependencies on modules or other configuration (including + * other migrations) that must be available before the migration can be + * created. + * + * @see \Drupal\Core\Config\Entity\ConfigDependencyManager + * + * @var array + */ + protected $dependencies = []; + /** * The migration plugin manager for loading other migration plugins. * @@ -163,14 +271,15 @@ class Migration extends PluginBase implements MigrationInterface, RequirementsIn */ public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationPluginManagerInterface $migration_plugin_manager, MigratePluginManagerInterface $source_plugin_manager, MigratePluginManagerInterface $process_plugin_manager, MigrateDestinationPluginManager $destination_plugin_manager, MigratePluginManagerInterface $idmap_plugin_manager) { parent::__construct($configuration, $plugin_id, $plugin_definition); - $this->setConfiguration($configuration); - $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 (NestedArray::mergeDeep($plugin_definition, $configuration) as $key => $value) { + $this->$key = $value; + } } /** @@ -200,7 +309,7 @@ public function id() { * {@inheritdoc} */ public function label() { - return $this->configuration['label']; + return $this->label; } /** @@ -218,13 +327,7 @@ public function label() { * @see https://www.drupal.org/node/2873795 */ public function get($property) { - if (isset($this->$property)) { - return $this->$property; - } - if (isset($this->configuration[$property])) { - return $this->configuration[$property]; - } - return NULL; + return isset($this->$property) ? $this->$property : NULL; } /** @@ -242,7 +345,7 @@ public function getIdMapPlugin() { */ public function getSourcePlugin() { if (!isset($this->sourcePlugin)) { - $this->sourcePlugin = $this->sourcePluginManager->createInstance($this->configuration['source']['plugin'], $this->configuration['source'], $this); + $this->sourcePlugin = $this->sourcePluginManager->createInstance($this->source['plugin'], $this->source, $this); } return $this->sourcePlugin; } @@ -310,7 +413,7 @@ public function getDestinationPlugin($stub_being_requested = FALSE) { throw new MigrateSkipRowException('Stub requested but not made because no_stub configuration is set.'); } if (!isset($this->destinationPlugin)) { - $this->destinationPlugin = $this->destinationPluginManager->createInstance($this->configuration['destination']['plugin'], $this->configuration['destination'], $this); + $this->destinationPlugin = $this->destinationPluginManager->createInstance($this->destination['plugin'], $this->destination, $this); } return $this->destinationPlugin; } @@ -320,8 +423,9 @@ public function getDestinationPlugin($stub_being_requested = FALSE) { */ public function getIdMap() { if (!isset($this->idMapPlugin)) { - $plugin = $this->configuration['idMap']['plugin']; - $this->idMapPlugin = $this->idMapPluginManager->createInstance($plugin, $this->configuration['idMap'], $this); + $configuration = $this->idMap; + $plugin = isset($configuration['plugin']) ? $configuration['plugin'] : 'sql'; + $this->idMapPlugin = $this->idMapPluginManager->createInstance($plugin, $configuration, $this); } return $this->idMapPlugin; } @@ -339,14 +443,14 @@ public function checkRequirements() { $this->getDestinationPlugin()->checkRequirements(); } - if (empty($this->configuration['requirements'])) { + if (empty($this->requirements)) { // There are no requirements to check. return; } /** @var \Drupal\migrate\Plugin\MigrationInterface[] $required_migrations */ - $required_migrations = $this->getMigrationPluginManager()->createInstances($this->configuration['requirements']); + $required_migrations = $this->getMigrationPluginManager()->createInstances($this->requirements); - $missing_migrations = array_diff($this->configuration['requirements'], array_keys($required_migrations)); + $missing_migrations = array_diff($this->requirements, array_keys($required_migrations)); // Check if the dependencies are in good shape. foreach ($required_migrations as $migration_id => $required_migration) { if (!$required_migration->allRowsProcessed()) { @@ -361,7 +465,7 @@ public function checkRequirements() { /** * Gets the migration plugin manager. * - * @return \Drupal\migrate\Plugin\MigrationPluginManagerInterface + * @return \Drupal\migrate\Plugin\MigratePluginManager * The plugin manager. */ protected function getMigrationPluginManager() { @@ -447,7 +551,6 @@ public function set($property_name, $value) { unset($this->destinationPlugin); } $this->{$property_name} = $value; - $this->configuration[$property_name] = $value; return $this; } @@ -455,14 +558,14 @@ public function set($property_name, $value) { * {@inheritdoc} */ public function getProcess() { - return $this->getProcessNormalized($this->configuration['process']); + return $this->getProcessNormalized($this->process); } /** * {@inheritdoc} */ public function setProcess(array $process) { - $this->configuration['process'] = $process; + $this->process = $process; return $this; } @@ -470,7 +573,7 @@ public function setProcess(array $process) { * {@inheritdoc} */ public function setProcessOfProperty($property, $process_of_property) { - $this->configuration['process'][$property] = $process_of_property; + $this->process[$property] = $process_of_property; return $this; } @@ -510,8 +613,9 @@ public function setTrackLastImported($track_last_imported) { * {@inheritdoc} */ public function getMigrationDependencies() { - $this->configuration['migration_dependencies']['optional'] = array_unique(array_merge($this->configuration['migration_dependencies']['optional'], $this->findMigrationDependencies($this->configuration['process']))); - return $this->configuration['migration_dependencies']; + $this->migration_dependencies = ($this->migration_dependencies ?: []) + ['required' => [], 'optional' => []]; + $this->migration_dependencies['optional'] = array_unique(array_merge($this->migration_dependencies['optional'], $this->findMigrationDependencies($this->process))); + return $this->migration_dependencies; } /** @@ -555,14 +659,14 @@ public function getPluginDefinition() { * {@inheritdoc} */ public function getDestinationConfiguration() { - return $this->configuration['destination']; + return $this->destination; } /** * {@inheritdoc} */ public function getSourceConfiguration() { - return $this->configuration['source']; + return $this->source; } /** @@ -583,52 +687,14 @@ public function getDestinationIds() { * {@inheritdoc} */ public function getMigrationTags() { - return $this->configuration['migration_tags']; + return $this->migration_tags; } /** * {@inheritdoc} */ public function isAuditable() { - return (bool) $this->configuration['audit']; - } - - /** - * {@inheritdoc} - */ - public function setConfiguration(array $configuration) { - $this->configuration = NestedArray::mergeDeepArray([ - $this->defaultConfiguration(), - $this->pluginDefinition, - $configuration - ], TRUE); - } - - /** - * {@inheritdoc} - */ - public function defaultConfiguration() { - return [ - 'id' => '', - 'label' => '', - 'migration_tags' => [], - 'audit' => FALSE, - 'idMap' => [ - 'plugin' => 'sql' - ], - 'source' => [ - 'plugin' => 'empty', - ], - 'process' => [], - 'destination' => [ - 'plugin' => 'null', - ], - 'requirements' => [], - 'migration_dependencies' => [ - 'required' => [], - 'optional' => [], - ], - ]; + return (bool) $this->audit; } }