Mercurial > hg > cmmr2012-drupal-site
diff core/modules/migrate/src/Plugin/PluginEventSubscriber.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/modules/migrate/src/Plugin/PluginEventSubscriber.php Thu Jul 05 14:24:15 2018 +0000 @@ -0,0 +1,94 @@ +<?php + +namespace Drupal\migrate\Plugin; + +use Drupal\migrate\Event\ImportAwareInterface; +use Drupal\migrate\Event\MigrateEvents; +use Drupal\migrate\Event\MigrateImportEvent; +use Drupal\migrate\Event\MigrateRollbackEvent; +use Drupal\migrate\Event\RollbackAwareInterface; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +/** + * Event subscriber to forward Migrate events to source and destination plugins. + */ +class PluginEventSubscriber implements EventSubscriberInterface { + + /** + * Tries to invoke event handling methods on source and destination plugins. + * + * @param string $method + * The method to invoke. + * @param \Drupal\migrate\Event\MigrateImportEvent|\Drupal\migrate\Event\MigrateRollbackEvent $event + * The event that has triggered the invocation. + * @param string $plugin_interface + * The interface which plugins must implement in order to be invoked. + */ + protected function invoke($method, $event, $plugin_interface) { + $migration = $event->getMigration(); + + $source = $migration->getSourcePlugin(); + if ($source instanceof $plugin_interface) { + call_user_func([$source, $method], $event); + } + + $destination = $migration->getDestinationPlugin(); + if ($destination instanceof $plugin_interface) { + call_user_func([$destination, $method], $event); + } + } + + /** + * Forwards pre-import events to the source and destination plugins. + * + * @param \Drupal\migrate\Event\MigrateImportEvent $event + * The import event. + */ + public function preImport(MigrateImportEvent $event) { + $this->invoke('preImport', $event, ImportAwareInterface::class); + } + + /** + * Forwards post-import events to the source and destination plugins. + * + * @param \Drupal\migrate\Event\MigrateImportEvent $event + * The import event. + */ + public function postImport(MigrateImportEvent $event) { + $this->invoke('postImport', $event, ImportAwareInterface::class); + } + + /** + * Forwards pre-rollback events to the source and destination plugins. + * + * @param \Drupal\migrate\Event\MigrateRollbackEvent $event + * The rollback event. + */ + public function preRollback(MigrateRollbackEvent $event) { + $this->invoke('preRollback', $event, RollbackAwareInterface::class); + } + + /** + * Forwards post-rollback events to the source and destination plugins. + * + * @param \Drupal\migrate\Event\MigrateRollbackEvent $event + * The rollback event. + */ + public function postRollback(MigrateRollbackEvent $event) { + $this->invoke('postRollback', $event, RollbackAwareInterface::class); + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + $events = []; + $events[MigrateEvents::PRE_IMPORT][] = ['preImport']; + $events[MigrateEvents::POST_IMPORT][] = ['postImport']; + $events[MigrateEvents::PRE_ROLLBACK][] = ['preRollback']; + $events[MigrateEvents::POST_ROLLBACK][] = ['postRollback']; + + return $events; + } + +}