Skip to content

Commit

Permalink
Avoid dereferencing the source migration in the constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-vessey committed Nov 5, 2024
1 parent 970471f commit d409b3b
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions src/Plugin/migrate/source/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\dgi_migrate\MigrationIterator;
use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
Expand All @@ -23,14 +23,14 @@ class Migration extends SourcePluginBase implements ContainerFactoryPluginInterf
*
* @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
*/
protected $migrationPluginManager;
protected MigrationPluginManagerInterface $migrationPluginManager;

/**
* The target migration.
* Memoized target migration.
*
* @var \Drupal\migrate\Plugin\MigrationInterface
*/
protected $targetMigration;
protected MigrationInterface $targetMigration;

/**
* Constructor.
Expand All @@ -39,13 +39,12 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);

$this->migrationPluginManager = $migration_plugin_manager;
$this->targetMigration = $this->migrationPluginManager->createInstance($this->configuration['migration']);
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, ?MigrationInterface $migration = NULL) {
return new static(
$configuration,
$plugin_id,
Expand All @@ -55,18 +54,32 @@ public static function create(ContainerInterface $container, array $configuratio
);
}

/**
* Load and identify the target migration.
*
* @return \Drupal\migrate\Plugin\MigrationInterface
* The target migration.
*/
protected function getTargetMigration() : MigrationInterface {
if (!isset($this->targetMigration)) {
$this->targetMigration = $this->migrationPluginManager->createInstance($this->configuration['migration']);
}

return $this->targetMigration;
}

/**
* {@inheritdoc}
*/
public function initializeIterator() {
return new MigrationIterator($this->targetMigration->getIdMap(), 'currentDestination');
return new MigrationIterator($this->getTargetMigration()->getIdMap(), 'currentDestination');
}

/**
* {@inheritdoc}
*/
public function getIds() {
return (array) $this->targetMigration->getDestinationPlugin()->getIds();
return (array) $this->getTargetMigration()->getDestinationPlugin()->getIds();
}

/**
Expand All @@ -81,7 +94,7 @@ public function fields() {
*/
public function __toString() {
return strtr('target migration: @migration', [
'@migration' => $this->targetMigration->id(),
'@migration' => $this->getTargetMigration()->id(),
]);
}

Expand All @@ -106,13 +119,4 @@ public function __sleep() {
return $vars;
}

/**
* {@inheritdoc}
*/
public function __wakeup() {
parent::__wakeup();

$this->targetMigration = $this->migrationPluginManager->createInstance($this->configuration['migration']);
}

}

0 comments on commit d409b3b

Please sign in to comment.