MigrationEventSubscriber.php 3.52 KB
Newer Older
Derimagia's avatar
Derimagia committed
1
<?php
Derimagia's avatar
Derimagia committed
2

Derimagia's avatar
Derimagia committed
3 4 5 6 7 8 9 10
namespace Drupal\migrate_devel\EventSubscriber;

use Drupal\migrate\Event\MigrateEvents;
use Drupal\migrate\Event\MigratePostRowSaveEvent;
use Drupal\migrate\Event\MigratePreRowSaveEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
Derimagia's avatar
Derimagia committed
11
 * MigrationEventSubscriber for Debugging Migrations.
Derimagia's avatar
Derimagia committed
12 13
 *
 * @class MigrationEventSubscriber
Derimagia's avatar
Derimagia committed
14 15
 */
class MigrationEventSubscriber implements EventSubscriberInterface {
Derimagia's avatar
Derimagia committed
16

Derimagia's avatar
Derimagia committed
17
  /**
Derimagia's avatar
Derimagia committed
18 19
   * Pre Row Save Function for --migrate-debug-pre.
   *
Derimagia's avatar
Derimagia committed
20
   * @param \Drupal\migrate\Event\MigratePreRowSaveEvent $event
Derimagia's avatar
Derimagia committed
21
   *    Pre-Row-Save Migrate Event.
Derimagia's avatar
Derimagia committed
22 23
   */
  public function debugRowPreSave(MigratePreRowSaveEvent $event) {
24 25 26 27
    if (PHP_SAPI !== 'cli') {
      return;
    }

Derimagia's avatar
Derimagia committed
28 29
    $row = $event->getRow();

30
    if (in_array('migrate-debug-pre', \Drush\Drush::config()->get('runtime.options'))) {
Derimagia's avatar
Derimagia committed
31
      // Start with capital letter for variables since this is actually a label.
Derimagia's avatar
Derimagia committed
32 33 34
      $Source = $row->getSource();
      $Destination = $row->getDestination();

35 36 37 38 39 40 41 42 43 44 45
      // Uses Symfony VarDumper.
      // @todo Explore advanced usage of CLI dumper class for nicer output.
      // https://www.drupal.org/project/migrate_devel/issues/3151276
      dump(
        '---------------------------------------------------------------------',
        '|                             $Source                               |',
        '---------------------------------------------------------------------',
        $Source,
        '---------------------------------------------------------------------',
        '|                           $Destination                            |',
        '---------------------------------------------------------------------',
46
        $Destination
47
      );
Derimagia's avatar
Derimagia committed
48 49 50 51
    }
  }

  /**
Derimagia's avatar
Derimagia committed
52 53
   * Post Row Save Function for --migrate-debug.
   *
Derimagia's avatar
Derimagia committed
54
   * @param \Drupal\migrate\Event\MigratePostRowSaveEvent $event
Derimagia's avatar
Derimagia committed
55
   *   Post-Row-Save Migrate Event.
Derimagia's avatar
Derimagia committed
56 57
   */
  public function debugRowPostSave(MigratePostRowSaveEvent $event) {
58 59 60 61
    if (PHP_SAPI !== 'cli') {
      return;
    }

Derimagia's avatar
Derimagia committed
62 63
    $row = $event->getRow();

64 65
    if (in_array('migrate-debug', \Drush\Drush::config()->get('runtime.options'))) {

Derimagia's avatar
Derimagia committed
66
      // Start with capital letter for variables since this is actually a label.
Derimagia's avatar
Derimagia committed
67 68 69 70
      $Source = $row->getSource();
      $Destination = $row->getDestination();
      $DestinationIDValues = $event->getDestinationIdValues();

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
      // Uses Symfony VarDumper.
      // @todo Explore advanced usage of CLI dumper class for nicer output.
      // https://www.drupal.org/project/migrate_devel/issues/3151276
      dump(
        '---------------------------------------------------------------------',
        '|                             $Source                               |',
        '---------------------------------------------------------------------',
        $Source,
        '---------------------------------------------------------------------',
        '|                           $Destination                            |',
        '---------------------------------------------------------------------',
        $Destination,
        '---------------------------------------------------------------------',
        '|                       $DestinationIdValues                        |',
        '---------------------------------------------------------------------',
86
        $DestinationIDValues
87
      );
Derimagia's avatar
Derimagia committed
88 89 90 91 92 93 94 95 96 97 98
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[MigrateEvents::PRE_ROW_SAVE][] = ['debugRowPreSave'];
    $events[MigrateEvents::POST_ROW_SAVE][] = ['debugRowPostSave'];
    return $events;
  }
Derimagia's avatar
Derimagia committed
99

Derimagia's avatar
Derimagia committed
100
}