コード例 #1
0
ファイル: MigrateExecutable.php プロジェクト: scratch/gai
 /**
  * {@inheritdoc}
  */
 public function import()
 {
     $this->getEventDispatcher()->dispatch(MigrateEvents::PRE_IMPORT, new MigrateImportEvent($this->migration));
     // 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', array('@id' => $this->migration->id(), '@message' => $e->getMessage(), '@requirements' => $e->getRequirementsString())), 'error');
         return MigrationInterface::RESULT_FAILED;
     }
     $return = MigrationInterface::RESULT_COMPLETED;
     $source = $this->getSource();
     $id_map = $this->migration->getIdMap();
     try {
         $source->rewind();
     } catch (\Exception $e) {
         $this->message->display($this->t('Migration failed with source plugin exception: !e', array('!e' => $e->getMessage())), 'error');
         return MigrationInterface::RESULT_FAILED;
     }
     $destination = $this->migration->getDestinationPlugin();
     while ($source->valid()) {
         $row = $source->current();
         if ($this->sourceIdValues = $row->getSourceIdValues()) {
             // Wipe old messages, and save any new messages.
             $id_map->delete($this->sourceIdValues, TRUE);
             $this->saveQueuedMessages();
         }
         try {
             $this->processRow($row);
             $save = TRUE;
         } catch (MigrateSkipRowException $e) {
             $id_map->saveIdMapping($row, array(), MigrateIdMapInterface::STATUS_IGNORED, $this->rollbackAction);
             $save = FALSE;
         }
         if ($save) {
             try {
                 $this->getEventDispatcher()->dispatch(MigrateEvents::PRE_ROW_SAVE, new MigratePreRowSaveEvent($this->migration, $row));
                 $destination_id_values = $destination->import($row, $id_map->lookupDestinationId($this->sourceIdValues));
                 $this->getEventDispatcher()->dispatch(MigrateEvents::POST_ROW_SAVE, new MigratePostRowSaveEvent($this->migration, $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, $this->rollbackAction);
                     }
                 } else {
                     $id_map->saveIdMapping($row, array(), MigrateIdMapInterface::STATUS_FAILED, $this->rollbackAction);
                     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) {
                 $this->migration->getIdMap()->saveIdMapping($row, array(), $e->getStatus(), $this->rollbackAction);
                 $this->saveMessage($e->getMessage(), $e->getLevel());
                 $this->message->display($e->getMessage(), 'error');
             } catch (\Exception $e) {
                 $this->migration->getIdMap()->saveIdMapping($row, array(), MigrateIdMapInterface::STATUS_FAILED, $this->rollbackAction);
                 $this->handleException($e);
             }
         }
         if ($high_water_property = $this->migration->get('highWaterProperty')) {
             $this->migration->saveHighWater($row->getSourceProperty($high_water_property['name']));
         }
         // Reset row properties.
         unset($sourceValues, $destinationValues);
         $this->sourceRowStatus = MigrateIdMapInterface::STATUS_IMPORTED;
         if (($return = $this->checkStatus()) != MigrationInterface::RESULT_COMPLETED) {
             break;
         }
         try {
             $source->next();
         } catch (\Exception $e) {
             $this->message->display($this->t('Migration failed with source plugin exception: !e', array('!e' => $e->getMessage())), 'error');
             return MigrationInterface::RESULT_FAILED;
         }
     }
     $this->migration->setMigrationResult($return);
     $this->getEventDispatcher()->dispatch(MigrateEvents::POST_IMPORT, new MigrateImportEvent($this->migration));
     return $return;
 }