diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..17b8027 --- /dev/null +++ b/composer.json @@ -0,0 +1,15 @@ +{ + "name": "drupal/migrate_devel", + "description": "Migrate Development Tools", + "type": "drupal-module", + "require": { + "php": ">=5.6.0" + }, + "extra": { + "drush": { + "services": { + "drush.services.yml": "^9" + } + } + } +} diff --git a/drush.services.yml b/drush.services.yml new file mode 100644 index 0000000..43a02a1 --- /dev/null +++ b/drush.services.yml @@ -0,0 +1,5 @@ +services: + migrate_devel.commands: + class: \Drupal\migrate_devel\Commands\MigrateDevelCommands + tags: + - { name: drush.command } diff --git a/migrate_devel.drush.inc b/migrate_devel.drush.inc index 2d686c3..04fbd4e 100644 --- a/migrate_devel.drush.inc +++ b/migrate_devel.drush.inc @@ -3,6 +3,9 @@ /** * @file * File for Drush Integration. + * + * @todo Drush 9 doesn't load this file, move the helper functions somewhere + * else, like a utility class. */ use Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface; use Drupal\config_update\ConfigListInterface; @@ -10,22 +13,12 @@ use Drupal\config_update\ConfigRevertInterface; use Drupal\Core\Config\StorageInterface; use Drush\Log\LogLevel; -/** - * Implements hook_drush_help_alter(). - */ -function migrate_devel_drush_help_alter(&$command) { - if ($command['command'] === 'migrate-import') { - $command['options']['migrate-debug'] = 'Enable Debug Mode'; - $command['options']['migrate-debug-pre'] = 'Enable Debug Mode (Before Row Save)'; - } - - if ($command['command'] === 'migrate-status') { - $command['options']['migrate-debug'] = 'Enable Debug Mode'; - } -} - /** * Implements hook_drush_command_alter(). + * + * @todo move this to the new Drush 9 command hooks. + * MigrateDevelCommands::preMigrateStatus() + * MigrateDevelCommands::preMigrateImport() */ function migrate_devel_drush_command_alter(&$command) { $cmd = $command['command']; diff --git a/src/Commands/MigrateDevelCommands.php b/src/Commands/MigrateDevelCommands.php new file mode 100644 index 0000000..e467724 --- /dev/null +++ b/src/Commands/MigrateDevelCommands.php @@ -0,0 +1,61 @@ + false, 'migrate-debug-pre' => false]) + { + } + + /** + * @hook pre-command migrate:import + */ + public function preMigrateImport(CommandData $commandData) + { + // Save command options so we can read them in migrate event subscribers. + \Drupal::state()->set('migrate_devel.drush_command_options', $commandData->input()->getOptions()); + + if ($commandData->input()->getOption('migrate-debug')) { + // @todo Reset all migrations, port this from the old Drush 8 hook. + // migrate_devel_drush_command_alter(&$command) + } + } + + /** + * @hook post-command migrate:import + */ + public function postMigrateImport() + { + // Clean up stored command options so they don't affect the next import. + \Drupal::state()->delete('migrate_devel.drush_command_options'); + } + + /** + * Reset migrations when using debug mode. + * + * @hook pre-command migrate:status + * @option migrate-debug Enable Debug Mode + */ + public function preMigrateStatus(CommandData $commandData) + { + if ($commandData->input()->getOption('migrate-debug')) { + // @todo Reset all migrations, port this from the old Drush 8 hook. + // migrate_devel_drush_command_alter(&$command) + } + } + +} diff --git a/src/EventSubscriber/MigrationEventSubscriber.php b/src/EventSubscriber/MigrationEventSubscriber.php index 588ef44..6fa0211 100644 --- a/src/EventSubscriber/MigrationEventSubscriber.php +++ b/src/EventSubscriber/MigrationEventSubscriber.php @@ -23,8 +23,8 @@ class MigrationEventSubscriber implements EventSubscriberInterface { public function debugRowPreSave(MigratePreRowSaveEvent $event) { $row = $event->getRow(); - $using_drush = function_exists('drush_get_option'); - if ($using_drush && drush_get_option('migrate-debug-pre')) { + $options = \Drupal::state()->get('migrate_devel.drush_command_options'); + if ($options['migrate-debug-pre']) { // Start with capital letter for variables since this is actually a label. $Source = $row->getSource(); $Destination = $row->getDestination(); @@ -44,8 +44,8 @@ class MigrationEventSubscriber implements EventSubscriberInterface { public function debugRowPostSave(MigratePostRowSaveEvent $event) { $row = $event->getRow(); - $using_drush = function_exists('drush_get_option'); - if ($using_drush && drush_get_option('migrate-debug')) { + $options = \Drupal::state()->get('migrate_devel.drush_command_options'); + if ($options['migrate-debug']) { // Start with capital letter for variables since this is actually a label. $Source = $row->getSource(); $Destination = $row->getDestination();