Browse Source

Issue #1: Replace entity source logic by UserPreImport subscriber.

Frederic G. MARAND 7 years ago
parent
commit
892176d581

+ 5 - 0
reinstall.services.yml

@@ -25,3 +25,8 @@ services:
     class: 'Drupal\reinstall\EventSubscriber\TermPreDump'
     tags:
       - { name: 'event_subscriber' }
+
+  reinstall.pre_import:
+    class: 'Drupal\reinstall\EventSubscriber\UserPreImport'
+    tags:
+      - { name: 'event_subscriber' }

+ 2 - 2
src/DumperEvent.php

@@ -8,8 +8,8 @@ use Symfony\Component\EventDispatcher\Event;
 /**
  * Wraps a dump event for event listeners.
  *
- * @see \Drupal\reinstall\DumperEvents::REINSTALL_SERIALIZE_POST
- * @see \Drupal\reinstall\DumperEvents::SERIALIZE_PRE
+ * @see \Drupal\reinstall\ReinstallEvents::REINSTALL_SERIALIZE_POST
+ * @see \Drupal\reinstall\ReinstallEvents::SERIALIZE_PRE
  */
 class DumperEvent extends Event {
 

+ 2 - 2
src/EventSubscriber/FilePostDump.php

@@ -7,7 +7,7 @@ use Drupal\Core\File\FileSystem;
 use Drupal\Core\File\FileSystemInterface;
 use Drupal\file\Entity\File;
 use Drupal\reinstall\DumperEvent;
-use Drupal\reinstall\DumperEvents;
+use Drupal\reinstall\ReinstallEvents;
 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 
 /**
@@ -47,7 +47,7 @@ class FilePostDump implements EventSubscriberInterface {
    */
   public static function getSubscribedEvents() {
     return [
-      DumperEvents::POST_DUMP => 'onDumpPost',
+      ReinstallEvents::POST_DUMP => 'onDumpPost',
     ];
   }
 

+ 2 - 2
src/EventSubscriber/TermPreDump.php

@@ -3,7 +3,7 @@
 namespace Drupal\reinstall\EventSubscriber;
 
 use Drupal\reinstall\DumperEvent;
-use Drupal\reinstall\DumperEvents;
+use Drupal\reinstall\ReinstallEvents;
 use Drupal\taxonomy\TermInterface;
 use Drupal\taxonomy\TermStorageInterface;
 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -18,7 +18,7 @@ class TermPreDump implements EventSubscriberInterface {
    */
   public static function getSubscribedEvents() {
     return [
-      DumperEvents::PRE_DUMP => 'onDumpPre',
+      ReinstallEvents::PRE_DUMP => 'onDumpPre',
     ];
   }
 

+ 38 - 0
src/EventSubscriber/UserPreImport.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace Drupal\reinstall\EventSubscriber;
+
+use Drupal\migrate\Event\MigrateEvents;
+use Drupal\migrate\Event\MigrateImportEvent;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+
+class UserPreImport implements EventSubscriberInterface {
+
+  public static function getSubscribedEvents() {
+    return [
+      MigrateEvents::PRE_IMPORT => 'onPreImport',
+    ];
+  }
+
+  public function onPreImport(MigrateImportEvent $event) {
+    /** @var \Drupal\reinstall\Plugin\migrate\source\ReinstallUserSource $plugin */
+    $plugin = $event->getMigration()->getSourcePlugin();
+    $plugin->records = array_filter($plugin->records, [$this, 'filter01']);
+    return;
+  }
+
+  /**
+   * Skip users 0 and 1 in imports, as they are core-provided.
+   *
+   * @param array $record
+   *   The description of a user entity.
+   *
+   * @return bool
+   *   Include it (1) or filter it (0).
+   */
+  public function filter01(array $record) {
+    $ret = ($record['uid'] ?? 0) > 1;
+    return $ret;
+  }
+
+}

+ 0 - 17
src/Plugin/migrate/source/ReinstallFileSource.php

@@ -2,8 +2,6 @@
 
 namespace Drupal\reinstall\Plugin\migrate\source;
 
-use Drupal\migrate\Plugin\MigrationInterface;
-
 /**
  * Source plugin for files from a YAML file.
  *
@@ -13,19 +11,4 @@ use Drupal\migrate\Plugin\MigrationInterface;
  */
 class ReinstallFileSource extends SimpleSource {
 
-  /**
-   * Constructor.
-   */
-  public function __construct(
-    array $configuration,
-    string $pluginId,
-    array $pluginDefinition,
-    MigrationInterface $migration
-  ) {
-    parent::__construct($configuration, $pluginId, $pluginDefinition, $migration);
-
-    $rawRecords = array_map([$this, 'flattenRecord'], $this->initialParse($configuration));
-    $this->records = $rawRecords;
-  }
-
 }

+ 0 - 17
src/Plugin/migrate/source/ReinstallNodeSource.php

@@ -2,8 +2,6 @@
 
 namespace Drupal\reinstall\Plugin\migrate\source;
 
-use Drupal\migrate\Plugin\MigrationInterface;
-
 /**
  * Source plugin for nodes from a YAML file.
  *
@@ -13,19 +11,4 @@ use Drupal\migrate\Plugin\MigrationInterface;
  */
 class ReinstallNodeSource extends SimpleSource {
 
-  /**
-   * Constructor.
-   */
-  public function __construct(
-    array $configuration,
-    string $pluginId,
-    array $pluginDefinition,
-    MigrationInterface $migration
-  ) {
-    parent::__construct($configuration, $pluginId, $pluginDefinition, $migration);
-
-    $rawRecords = array_map([$this, 'flattenRecord'], $this->initialParse($configuration));
-    $this->records = $rawRecords;
-  }
-
 }

+ 0 - 17
src/Plugin/migrate/source/ReinstallParagraphSource.php

@@ -2,8 +2,6 @@
 
 namespace Drupal\reinstall\Plugin\migrate\source;
 
-use Drupal\migrate\Plugin\MigrationInterface;
-
 /**
  * Source plugin for terms from a YAML file.
  *
@@ -13,19 +11,4 @@ use Drupal\migrate\Plugin\MigrationInterface;
  */
 class ReinstallParagraphSource extends SimpleSource {
 
-  /**
-   * Constructor.
-   */
-  public function __construct(
-    array $configuration,
-    string $pluginId,
-    array $pluginDefinition,
-    MigrationInterface $migration
-  ) {
-    parent::__construct($configuration, $pluginId, $pluginDefinition, $migration);
-
-    $rawRecords = array_map([$this, 'flattenRecord'], $this->initialParse($configuration));
-    $this->records = $rawRecords;
-  }
-
 }

+ 1 - 15
src/Plugin/migrate/source/ReinstallTermSource.php

@@ -3,6 +3,7 @@
 namespace Drupal\reinstall\Plugin\migrate\source;
 
 use Drupal\migrate\Plugin\MigrationInterface;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 
 /**
  * Source plugin for terms from a YAML file.
@@ -13,19 +14,4 @@ use Drupal\migrate\Plugin\MigrationInterface;
  */
 class ReinstallTermSource extends SimpleSource {
 
-  /**
-   * Constructor.
-   */
-  public function __construct(
-    array $configuration,
-    string $pluginId,
-    array $pluginDefinition,
-    MigrationInterface $migration
-  ) {
-    parent::__construct($configuration, $pluginId, $pluginDefinition, $migration);
-
-    $rawRecords = array_map([$this, 'flattenRecord'], $this->initialParse($configuration));
-    $this->records = $rawRecords;
-  }
-
 }

+ 0 - 31
src/Plugin/migrate/source/ReinstallUserSource.php

@@ -2,8 +2,6 @@
 
 namespace Drupal\reinstall\Plugin\migrate\source;
 
-use Drupal\migrate\Plugin\MigrationInterface;
-
 /**
  * Source plugin for users from a YAML file.
  *
@@ -13,33 +11,4 @@ use Drupal\migrate\Plugin\MigrationInterface;
  */
 class ReinstallUserSource extends SimpleSource {
 
-  /**
-   * Constructor.
-   */
-  public function __construct(
-    array $configuration,
-    string $pluginId,
-    array $pluginDefinition,
-    MigrationInterface $migration
-  ) {
-    parent::__construct($configuration, $pluginId, $pluginDefinition, $migration);
-
-    $rawRecords = array_map([$this, 'flattenRecord'], $this->initialParse($configuration));
-    $rawRecords = array_filter($rawRecords, [$this, 'filter01']);
-    $this->records = $rawRecords;
-  }
-
-  /**
-   * Skip users 0 and 1 in imports, as they are core-provided.
-   *
-   * @param array $record
-   *   The description of a user entity.
-   *
-   * @return bool
-   *   Include it (1) or filter it (0).
-   */
-  protected function filter01(array $record) {
-    return ($record['uid'] ?? 0) > 1;
-  }
-
 }

+ 31 - 4
src/Plugin/migrate/source/SimpleSource.php

@@ -8,6 +8,8 @@ use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
 use Drupal\migrate\Plugin\MigrationInterface;
 use Drupal\migrate\Row;
 use Symfony\Component\DependencyInjection\ContainerInterface;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 use Symfony\Component\Yaml\Exception\ParseException;
 use Symfony\Component\Yaml\Yaml;
 
@@ -18,11 +20,32 @@ abstract class SimpleSource extends SourcePluginBase implements ContainerFactory
   use SimpleSourceTrait;
 
   /**
-   * The source records. MUST be initialized in concrete classes __construct().
+   * The event_dispatcher service.
+   *
+   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
+   */
+  protected $eventDispatcher;
+
+  /**
+   * The source records.
+   *
+   * MAY be altered by subscribing to MigrateEvents::PRE_IMPORT.
    *
    * @var array
    */
-  protected $records;
+  public $records;
+
+  public function __construct(
+    array $configuration,
+    $plugin_id,
+    $plugin_definition,
+    \Drupal\migrate\Plugin\MigrationInterface $migration,
+    EventDispatcherInterface $eventDispatcher
+  ) {
+    parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
+    $this->eventDispatcher = $eventDispatcher;
+    $this->records = array_map([$this, 'flattenRecord'], $this->initialParse($configuration));
+  }
 
   /**
    * {@inheritdoc}
@@ -36,7 +59,8 @@ abstract class SimpleSource extends SourcePluginBase implements ContainerFactory
   ) {
     $importPath = $container->getParameter('reinstall.path');
     $configuration['importPath'] = $importPath;
-    return new static($configuration, $pluginId, $pluginDefinition, $migration);
+    $dispatcher = $container->get('event_dispatcher');
+    return new static($configuration, $pluginId, $pluginDefinition, $migration, $dispatcher);
   }
 
   /**
@@ -111,7 +135,10 @@ abstract class SimpleSource extends SourcePluginBase implements ContainerFactory
    * {@inheritdoc}
    */
   protected function initializeIterator() {
-    return new \ArrayIterator($this->records);
+    if (!isset($this->iterator)) {
+      $this->iterator = new \ArrayIterator($this->records);
+    }
+    return $this->iterator;
   }
 
   /**

+ 1 - 1
src/DumperEvents.php → src/ReinstallEvents.php

@@ -7,7 +7,7 @@ namespace Drupal\reinstall;
  *
  * @see \Drupal\reinstall\Dumper
  */
-final class DumperEvents {
+final class ReinstallEvents {
 
   /**
    * Name of the event fired when entities have been dumped.