You are here

class MigrateExecutable in Migrate Manifest 3.x

Same name and namespace in other branches
  1. 8.2 src/MigrateExecutable.php \Drupal\migrate_manifest\MigrateExecutable
  2. 8 src/MigrateExecutable.php \Drupal\migrate_manifest\MigrateExecutable

Hierarchy

Expanded class hierarchy of MigrateExecutable

File

src/MigrateExecutable.php, line 17

Namespace

Drupal\migrate_manifest
View source
class MigrateExecutable extends \Drupal\migrate\MigrateExecutable {

  /**
   * {@inheritdoc}
   */
  public function import() {

    // Only begin the import operation if the migration is currently idle.
    if ($this->migration
      ->getStatus() !== MigrationInterface::STATUS_IDLE) {
      $this->message
        ->display($this
        ->t('Migration @id is busy with another operation: @status', [
        '@id' => $this->migration
          ->id(),
        '@status' => $this
          ->t($this->migration
          ->getStatusLabel()),
      ]), 'error');
      return MigrationInterface::RESULT_FAILED;
    }
    $this
      ->getEventDispatcher()
      ->dispatch(MigrateEvents::PRE_IMPORT, new MigrateImportEvent($this->migration, $this->message));

    // Knock off migration if the requirements haven't been met.
    try {
      $this->migration
        ->checkRequirements();
    } catch (RequirementsException $e) {
      $this->message
        ->display($this
        ->t('Migration @id did not meet the requirements. @message @requirements', [
        '@id' => $this->migration
          ->id(),
        '@message' => $e
          ->getMessage(),
        '@requirements' => $e
          ->getRequirementsString(),
      ]), 'error');
      return MigrationInterface::RESULT_FAILED;
    }
    $this->migration
      ->setStatus(MigrationInterface::STATUS_IMPORTING);
    $return = MigrationInterface::RESULT_COMPLETED;
    $source = $this
      ->getSource();
    $destination = $this->migration
      ->getDestinationPlugin();
    try {
      foreach ($source as $row) {
        $this
          ->importRow($row, $destination);

        // Check for memory exhaustion.
        if (($return = $this
          ->checkStatus()) != MigrationInterface::RESULT_COMPLETED) {
          break;
        }

        // If anyone has requested we stop, return the requested result.
        if ($this->migration
          ->getStatus() == MigrationInterface::STATUS_STOPPING) {
          $return = $this->migration
            ->getInterruptionResult();
          $this->migration
            ->clearInterruptionResult();
          break;
        }
      }
    } catch (\Exception $e) {
      $this->message
        ->display($this
        ->t('Migration failed with source plugin exception: @e', [
        '@e' => $e
          ->getMessage(),
      ]), 'error');
      $this->migration
        ->setStatus(MigrationInterface::STATUS_IDLE);
      return MigrationInterface::RESULT_FAILED;
    }
    $this
      ->getEventDispatcher()
      ->dispatch(MigrateEvents::POST_IMPORT, new MigrateImportEvent($this->migration, $this->message));
    $this->migration
      ->setStatus(MigrationInterface::STATUS_IDLE);
    return $return;
  }

  /**
   * Helper method that imports a single row.
   *
   * @param \Drupal\migrate\Row $row
   * @param \Drupal\migrate\Plugin\MigrateDestinationInterface $destination
   */
  private function importRow(Row $row, MigrateDestinationInterface $destination) {
    $id_map = $this->migration
      ->getIdMap();

    // Hide values in internal property so saveMessages can use them.
    $this->sourceIdValues = $row
      ->getSourceIdValues();
    try {
      $this
        ->processRow($row);
      $this
        ->getEventDispatcher()
        ->dispatch(MigrateEvents::PRE_ROW_SAVE, new MigratePreRowSaveEvent($this->migration, $this->message, $row));
      $destination_ids = $id_map
        ->lookupDestinationIds($this->sourceIdValues);
      $destination_id_values = $destination_ids ? reset($destination_ids) : [];
      $destination_id_values = $destination
        ->import($row, $destination_id_values);
      $this
        ->getEventDispatcher()
        ->dispatch(MigrateEvents::POST_ROW_SAVE, new MigratePostRowSaveEvent($this->migration, $this->message, $row, $destination_id_values));
      if ($destination_id_values) {

        // We do not save an idMap entry for config.
        if ($destination_id_values !== TRUE) {
          $id_map
            ->saveIdMapping($row, $destination_id_values, $this->sourceRowStatus, $destination
            ->rollbackAction());
        }
      }
      else {
        $id_map
          ->saveIdMapping($row, [], MigrateIdMapInterface::STATUS_FAILED);
        if (!$id_map
          ->messageCount()) {
          $message = $this
            ->t('New object was not saved, no error provided');
          $this
            ->saveMessage($message);
          $this->message
            ->display($message);
        }
      }
    } catch (MigrateException $e) {
      $id_map
        ->saveIdMapping($row, [], $e
        ->getStatus());
      $this
        ->saveMessage($e
        ->getMessage(), $e
        ->getLevel());
    } catch (MigrateSkipRowException $e) {
      if ($e
        ->getSaveToMap()) {
        $id_map
          ->saveIdMapping($row, [], MigrateIdMapInterface::STATUS_IGNORED);
      }
      if ($message = trim($e
        ->getMessage())) {
        $this
          ->saveMessage($message, MigrationInterface::MESSAGE_INFORMATIONAL);
      }
    } catch (\Exception $e) {
      $id_map
        ->saveIdMapping($row, [], MigrateIdMapInterface::STATUS_FAILED);
      $this
        ->handleException($e);
    }
    $this->sourceRowStatus = MigrateIdMapInterface::STATUS_IMPORTED;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateExecutable::$counts protected property An array of counts. Initially used for cache hit/miss tracking.
MigrateExecutable::$eventDispatcher protected property The event dispatcher.
MigrateExecutable::$memoryLimit protected property The PHP memory_limit expressed in bytes.
MigrateExecutable::$memoryThreshold protected property The ratio of the memory limit at which an operation will be interrupted.
MigrateExecutable::$message public property Migration message service.
MigrateExecutable::$migration protected property The configuration of the migration to do.
MigrateExecutable::$source protected property The source.
MigrateExecutable::$sourceIdValues protected property The configuration values of the source. 1
MigrateExecutable::$sourceRowStatus protected property Status of one row.
MigrateExecutable::attemptMemoryReclaim protected function Tries to reclaim memory. 1
MigrateExecutable::checkStatus protected function Checks for exceptional conditions, and display feedback.
MigrateExecutable::currentSourceIds protected function Fetches the key array for the current source record.
MigrateExecutable::formatSize protected function Generates a string representation for the given byte count. 1
MigrateExecutable::getEventDispatcher protected function Gets the event dispatcher.
MigrateExecutable::getIdMap protected function Get the ID map from the current migration. 2
MigrateExecutable::getMemoryUsage protected function Returns the memory usage so far. 1
MigrateExecutable::getSource protected function Returns the source. 1
MigrateExecutable::handleException protected function Takes an Exception object and both saves and displays it. 1
MigrateExecutable::import public function Performs an import operation - migrate items from source to destination. Overrides MigrateExecutable::import
MigrateExecutable::importRow private function Helper method that imports a single row.
MigrateExecutable::memoryExceeded protected function Tests whether we've exceeded the desired memory threshold. 1
MigrateExecutable::processRow public function Processes a row. Overrides MigrateExecutableInterface::processRow
MigrateExecutable::rollback public function Performs a rollback operation - remove previously-imported items. Overrides MigrateExecutableInterface::rollback
MigrateExecutable::saveMessage public function Passes messages through to the map class. Overrides MigrateExecutableInterface::saveMessage
MigrateExecutable::__construct public function Constructs a MigrateExecutable and verifies and sets the memory limit.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.