Browse Source

Refactored to reduce the amount of code in concrete sources.

Frederic G. MARAND 7 years ago
parent
commit
ebd605ad03

+ 3 - 21
src/Plugin/migrate/source/ReinstallFileSource.php

@@ -2,19 +2,16 @@
 
 namespace Drupal\reinstall\Plugin\migrate\source;
 
-use Drupal\Core\Entity\EntityFieldManagerInterface;
-use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
 use Drupal\migrate\Plugin\MigrationInterface;
 
 /**
- * Source plugin for terms from a YAML file.
+ * Source plugin for files from a YAML file.
  *
  * @MigrateSource(
  *   id = "reinstall_files"
  * )
  */
 class ReinstallFileSource extends SimpleSource {
-  const ENTITY_TYPE = 'file';
 
   /**
    * Constructor.
@@ -23,27 +20,12 @@ class ReinstallFileSource extends SimpleSource {
     array $configuration,
     string $pluginId,
     array $pluginDefinition,
-    MigrationInterface $migration,
-    EntityTypeBundleInfoInterface $ebi,
-    EntityFieldManagerInterface $efm
+    MigrationInterface $migration
   ) {
-    parent::__construct($configuration, $pluginId, $pluginDefinition, $migration, $ebi, $efm);
+    parent::__construct($configuration, $pluginId, $pluginDefinition, $migration);
 
     $rawRecords = array_map([$this, 'flattenRecord'], $this->initialParse($configuration));
     $this->records = $rawRecords;
   }
 
-  /**
-   * {@inheritdoc}
-   */
-  public function getIds() {
-    $ids = [
-      'fid' => [
-        'type' => 'integer',
-      ],
-    ];
-
-    return $ids;
-  }
-
 }

+ 4 - 22
src/Plugin/migrate/source/ReinstallNodeSource.php

@@ -2,19 +2,16 @@
 
 namespace Drupal\reinstall\Plugin\migrate\source;
 
-use Drupal\Core\Entity\EntityFieldManagerInterface;
-use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
 use Drupal\migrate\Plugin\MigrationInterface;
 
 /**
- * Source plugin for terms from a YAML file.
+ * Source plugin for nodes from a YAML file.
  *
  * @MigrateSource(
- *   id = "reinstall_nodes"
+ *   id = "reinstall_node"
  * )
  */
 class ReinstallNodeSource extends SimpleSource {
-  const ENTITY_TYPE = 'node';
 
   /**
    * Constructor.
@@ -23,27 +20,12 @@ class ReinstallNodeSource extends SimpleSource {
     array $configuration,
     string $pluginId,
     array $pluginDefinition,
-    MigrationInterface $migration,
-    EntityTypeBundleInfoInterface $ebi,
-    EntityFieldManagerInterface $efm
+    MigrationInterface $migration
   ) {
-    parent::__construct($configuration, $pluginId, $pluginDefinition, $migration, $ebi, $efm);
+    parent::__construct($configuration, $pluginId, $pluginDefinition, $migration);
 
     $rawRecords = array_map([$this, 'flattenRecord'], $this->initialParse($configuration));
     $this->records = $rawRecords;
   }
 
-  /**
-   * {@inheritdoc}
-   */
-  public function getIds() {
-    $ids = [
-      'nid' => [
-        'type' => 'integer',
-      ],
-    ];
-
-    return $ids;
-  }
-
 }

+ 3 - 21
src/Plugin/migrate/source/ReinstallParagraphSource.php

@@ -2,8 +2,6 @@
 
 namespace Drupal\reinstall\Plugin\migrate\source;
 
-use Drupal\Core\Entity\EntityFieldManagerInterface;
-use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
 use Drupal\migrate\Plugin\MigrationInterface;
 
 /**
@@ -13,8 +11,7 @@ use Drupal\migrate\Plugin\MigrationInterface;
  *   id = "reinstall_paragraphs"
  * )
  */
-class ReinstallNodeSource extends SimpleSource {
-  const ENTITY_TYPE = 'node';
+class ReinstallParagraphSource extends SimpleSource {
 
   /**
    * Constructor.
@@ -23,27 +20,12 @@ class ReinstallNodeSource extends SimpleSource {
     array $configuration,
     string $pluginId,
     array $pluginDefinition,
-    MigrationInterface $migration,
-    EntityTypeBundleInfoInterface $ebi,
-    EntityFieldManagerInterface $efm
+    MigrationInterface $migration
   ) {
-    parent::__construct($configuration, $pluginId, $pluginDefinition, $migration, $ebi, $efm);
+    parent::__construct($configuration, $pluginId, $pluginDefinition, $migration);
 
     $rawRecords = array_map([$this, 'flattenRecord'], $this->initialParse($configuration));
     $this->records = $rawRecords;
   }
 
-  /**
-   * {@inheritdoc}
-   */
-  public function getIds() {
-    $ids = [
-      'nid' => [
-        'type' => 'integer',
-      ],
-    ];
-
-    return $ids;
-  }
-
 }

+ 2 - 20
src/Plugin/migrate/source/ReinstallTermSource.php

@@ -2,8 +2,6 @@
 
 namespace Drupal\reinstall\Plugin\migrate\source;
 
-use Drupal\Core\Entity\EntityFieldManagerInterface;
-use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
 use Drupal\migrate\Plugin\MigrationInterface;
 
 /**
@@ -14,7 +12,6 @@ use Drupal\migrate\Plugin\MigrationInterface;
  * )
  */
 class ReinstallTermSource extends SimpleSource {
-  const ENTITY_TYPE = 'taxonomy_term';
 
   /**
    * Constructor.
@@ -23,27 +20,12 @@ class ReinstallTermSource extends SimpleSource {
     array $configuration,
     string $pluginId,
     array $pluginDefinition,
-    MigrationInterface $migration,
-    EntityTypeBundleInfoInterface $ebi,
-    EntityFieldManagerInterface $efm
+    MigrationInterface $migration
   ) {
-    parent::__construct($configuration, $pluginId, $pluginDefinition, $migration, $ebi, $efm);
+    parent::__construct($configuration, $pluginId, $pluginDefinition, $migration);
 
     $rawRecords = array_map([$this, 'flattenRecord'], $this->initialParse($configuration));
     $this->records = $rawRecords;
   }
 
-  /**
-   * {@inheritdoc}
-   */
-  public function getIds() {
-    $ids = [
-      'tid' => [
-        'type' => 'integer',
-      ],
-    ];
-
-    return $ids;
-  }
-
 }

+ 3 - 21
src/Plugin/migrate/source/ReinstallUserSource.php

@@ -2,19 +2,16 @@
 
 namespace Drupal\reinstall\Plugin\migrate\source;
 
-use Drupal\Core\Entity\EntityFieldManagerInterface;
-use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
 use Drupal\migrate\Plugin\MigrationInterface;
 
 /**
- * Source plugin for terms from a YAML file.
+ * Source plugin for users from a YAML file.
  *
  * @MigrateSource(
  *   id = "reinstall_users"
  * )
  */
 class ReinstallUserSource extends SimpleSource {
-  const ENTITY_TYPE = 'user';
 
   /**
    * Constructor.
@@ -23,11 +20,9 @@ class ReinstallUserSource extends SimpleSource {
     array $configuration,
     string $pluginId,
     array $pluginDefinition,
-    MigrationInterface $migration,
-    EntityTypeBundleInfoInterface $ebi,
-    EntityFieldManagerInterface $efm
+    MigrationInterface $migration
   ) {
-    parent::__construct($configuration, $pluginId, $pluginDefinition, $migration, $ebi, $efm);
+    parent::__construct($configuration, $pluginId, $pluginDefinition, $migration);
 
     $rawRecords = array_map([$this, 'flattenRecord'], $this->initialParse($configuration));
     $rawRecords = array_filter($rawRecords, [$this, 'filter01']);
@@ -47,17 +42,4 @@ class ReinstallUserSource extends SimpleSource {
     return ($record['uid'] ?? 0) > 1;
   }
 
-  /**
-   * {@inheritdoc}
-   */
-  public function getIds() {
-    $ids = [
-      'uid' => [
-        'type' => 'integer',
-      ],
-    ];
-
-    return $ids;
-  }
-
 }

+ 3 - 3
src/Plugin/migrate/source/SimpleSource.php

@@ -2,8 +2,6 @@
 
 namespace Drupal\reinstall\Plugin\migrate\source;
 
-use Drupal\Core\Entity\EntityFieldManagerInterface;
-use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
 use Drupal\migrate\MigrateException;
 use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
@@ -131,7 +129,9 @@ abstract class SimpleSource extends SourcePluginBase implements ContainerFactory
    * @throws \Drupal\migrate\MigrateException
    */
   protected function initialParse(array $configuration, string $key = NULL) {
-    $baseFilePath = $configuration['file'] ?? NULL;
+    $this->sstEntityType = $type = $configuration['type'];
+    $bundle = $configuration['bundle'];
+    $baseFilePath = "${type}/${bundle}.yml";
     $importPath = $configuration['importPath'] ?? NULL;
     $filePath = realpath("$importPath/$baseFilePath");
     if (!is_file($filePath) || !is_readable($filePath)) {

+ 64 - 20
src/Plugin/migrate/source/SimpleSourceTrait.php

@@ -11,32 +11,25 @@ namespace Drupal\reinstall\Plugin\migrate\source;
 
 trait SimpleSourceTrait {
 
+  /**
+   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
+   */
   protected $sstEntityTypeBundleInfo;
 
+  /**
+   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
+   */
   protected $sstEntityFieldManager;
 
+  /**
+   * @var string
+   */
   protected $sstEntityType;
 
-  protected function getEntityTypeBundleInfo() {
-    if (!isset($this->sstEntityTypeBundleInfo)) {
-      $this->sstEntityTypeBundleInfo = \Drupal::service('entity_type.bundle.info');
-    }
-
-    return $this->sstEntityTypeBundleInfo;
-  }
-
-  protected function getEntityFieldManager() {
-    if (!isset($this->sstEntityFieldManager)) {
-      $this->sstEntityFieldManager = \Drupal::service('entity_field.manager');
-    }
-
-    return $this->sstEntityFieldManager;
-  }
-
-  protected function getEntityType() {
-    assert(isset($this->sstEntityType));
-    return $this->sstEntityType;
-  }
+  /**
+   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
+   */
+  protected $sstEntityTypeManager;
 
   /**
    * {@inheritdoc}
@@ -79,4 +72,55 @@ trait SimpleSourceTrait {
     return $fields;
   }
 
+  protected function getEntityTypeBundleInfo() {
+    if (!isset($this->sstEntityTypeBundleInfo)) {
+      $this->sstEntityTypeBundleInfo = \Drupal::service('entity_type.bundle.info');
+    }
+
+    return $this->sstEntityTypeBundleInfo;
+  }
+
+  protected function getEntityFieldManager() {
+    if (!isset($this->sstEntityFieldManager)) {
+      $this->sstEntityFieldManager = \Drupal::service('entity_field.manager');
+    }
+
+    return $this->sstEntityFieldManager;
+  }
+
+  protected function getEntityType() {
+    assert(isset($this->sstEntityType));
+    return $this->sstEntityType;
+  }
+
+  protected function getEntityTypeManager() {
+    if (!isset($this->sstEntityTypeManager)) {
+      $this->sstEntityTypeManager = \Drupal::service('entity_type.manager');
+    }
+
+    return $this->sstEntityTypeManager;
+  }
+  /**
+   * {@inheritdoc}
+   */
+  public function getIds() {
+    $typeName = $this->getEntityType();
+
+    $typeDefinition = $this->getEntityTypeManager()->getDefinition($typeName);
+    $idName = $typeDefinition->getKey('id');
+    assert(!empty($idName));
+
+    $definitions = $this->getEntityFieldManager()->getBaseFieldDefinitions($typeName);
+    assert(isset($definitions[$idName]));
+    $idDefinition = $definitions[$idName];
+    $idType = $idDefinition->getType();
+    $ids = [
+      $idName => [
+        'type' => $idType,
+      ],
+    ];
+
+    return $ids;
+  }
+
 }