annotate core/modules/migrate/src/Plugin/PluginEventSubscriber.php @ 19:fa3358dc1485 tip

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