Version 1
[yaffs-website] / web / core / modules / migrate / src / Plugin / PluginEventSubscriber.php
1 <?php
2
3 namespace Drupal\migrate\Plugin;
4
5 use Drupal\migrate\Event\ImportAwareInterface;
6 use Drupal\migrate\Event\MigrateEvents;
7 use Drupal\migrate\Event\MigrateImportEvent;
8 use Drupal\migrate\Event\MigrateRollbackEvent;
9 use Drupal\migrate\Event\RollbackAwareInterface;
10 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11
12 /**
13  * Event subscriber to forward Migrate events to source and destination plugins.
14  */
15 class PluginEventSubscriber implements EventSubscriberInterface {
16
17   /**
18    * Tries to invoke event handling methods on source and destination plugins.
19    *
20    * @param string $method
21    *   The method to invoke.
22    * @param \Drupal\migrate\Event\MigrateImportEvent|\Drupal\migrate\Event\MigrateRollbackEvent $event
23    *   The event that has triggered the invocation.
24    * @param string $plugin_interface
25    *   The interface which plugins must implement in order to be invoked.
26    */
27   protected function invoke($method, $event, $plugin_interface) {
28     $migration = $event->getMigration();
29
30     $source = $migration->getSourcePlugin();
31     if ($source instanceof $plugin_interface) {
32       call_user_func([$source, $method], $event);
33     }
34
35     $destination = $migration->getDestinationPlugin();
36     if ($destination instanceof $plugin_interface) {
37       call_user_func([$destination, $method], $event);
38     }
39   }
40
41   /**
42    * Forwards pre-import events to the source and destination plugins.
43    *
44    * @param \Drupal\migrate\Event\MigrateImportEvent $event
45    *   The import event.
46    */
47   public function preImport(MigrateImportEvent $event) {
48     $this->invoke('preImport', $event, ImportAwareInterface::class);
49   }
50
51   /**
52    * Forwards post-import events to the source and destination plugins.
53    *
54    * @param \Drupal\migrate\Event\MigrateImportEvent $event
55    *   The import event.
56    */
57   public function postImport(MigrateImportEvent $event) {
58     $this->invoke('postImport', $event, ImportAwareInterface::class);
59   }
60
61   /**
62    * Forwards pre-rollback events to the source and destination plugins.
63    *
64    * @param \Drupal\migrate\Event\MigrateRollbackEvent $event
65    *   The rollback event.
66    */
67   public function preRollback(MigrateRollbackEvent $event) {
68     $this->invoke('preRollback', $event, RollbackAwareInterface::class);
69   }
70
71   /**
72    * Forwards post-rollback events to the source and destination plugins.
73    *
74    * @param \Drupal\migrate\Event\MigrateRollbackEvent $event
75    *   The rollback event.
76    */
77   public function postRollback(MigrateRollbackEvent $event) {
78     $this->invoke('postRollback', $event, RollbackAwareInterface::class);
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public static function getSubscribedEvents() {
85     $events = [];
86     $events[MigrateEvents::PRE_IMPORT][] = ['preImport'];
87     $events[MigrateEvents::POST_IMPORT][] = ['postImport'];
88     $events[MigrateEvents::PRE_ROLLBACK][] = ['preRollback'];
89     $events[MigrateEvents::POST_ROLLBACK][] = ['postRollback'];
90
91     return $events;
92   }
93
94 }