"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/migrate/src/MigrateStub.php" (26 Nov 2020, 5273 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "MigrateStub.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\migrate;
    4 
    5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
    6 use Drupal\migrate\Plugin\MigrateIdMapInterface;
    7 use Drupal\migrate\Plugin\MigrationInterface;
    8 use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
    9 
   10 /**
   11  * Provides the migrate stubbing service.
   12  */
   13 class MigrateStub implements MigrateStubInterface {
   14 
   15   /**
   16    * The migration plugin manager.
   17    *
   18    * @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
   19    */
   20   protected $migrationPluginManager;
   21 
   22   /**
   23    * Constructs a MigrationStub object.
   24    *
   25    * @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $migration_plugin_manager
   26    *   The migration plugin manager.
   27    */
   28   public function __construct(MigrationPluginManagerInterface $migration_plugin_manager) {
   29     $this->migrationPluginManager = $migration_plugin_manager;
   30   }
   31 
   32   /**
   33    * Creates a stub.
   34    *
   35    * @param string $migration_id
   36    *   The migration to stub.
   37    * @param array $source_ids
   38    *   An array of source ids.
   39    * @param array $default_values
   40    *   (optional) An array of default values to add to the stub.
   41    * @param bool $key_by_destination_ids
   42    *   (optional) NULL or TRUE to force indexing of the return array by
   43    *   destination id keys (default), or FALSE to return the raw return value of
   44    *   the destination plugin's ::import() method. The return value from
   45    *   MigrateDestinationInterface::import() is very poorly defined as "The
   46    *   entity ID or an indication of success". In practice, the mapping systems
   47    *   expect and all destination plugins return an array of destination
   48    *   identifiers. Unfortunately these arrays are inconsistently keyed. The
   49    *   core destination plugins return a numerically indexed array of
   50    *   destination identifiers, but several contrib destinations return an array
   51    *   of identifiers indexed by the destination keys. This method will
   52    *   generally index all return arrays for consistency and to provide as much
   53    *   information as possible, but this parameter is added for backwards
   54    *   compatibility to allow accessing the original array.
   55    *
   56    * @return array|false
   57    *   An array of destination ids for the new stub, keyed by destination id
   58    *   key, or false if the stub failed.
   59    *
   60    * @throws \Drupal\Component\Plugin\Exception\PluginException
   61    * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   62    * @throws \Drupal\migrate\MigrateException
   63    */
   64   public function createStub($migration_id, array $source_ids, array $default_values = [], $key_by_destination_ids = NULL) {
   65     $migrations = $this->migrationPluginManager->createInstances([$migration_id]);
   66     if (!$migrations) {
   67       throw new PluginNotFoundException($migration_id);
   68     }
   69     if (count($migrations) !== 1) {
   70       throw new \LogicException(sprintf('Cannot stub derivable migration "%s".  You must specify the id of a specific derivative to stub.', $migration_id));
   71     }
   72     $migration = reset($migrations);
   73     $source_id_keys = array_keys($migration->getSourcePlugin()->getIds());
   74     if (count($source_id_keys) !== count($source_ids)) {
   75       throw new \InvalidArgumentException('Expected and provided source id counts do not match.');
   76     }
   77     if (array_keys($source_ids) === range(0, count($source_ids) - 1)) {
   78       $source_ids = array_combine($source_id_keys, $source_ids);
   79     }
   80     $stub = $this->doCreateStub($migration, $source_ids, $default_values);
   81 
   82     // If the return from ::import is numerically indexed, and we aren't
   83     // requesting the raw return value, index it associatively using the
   84     // destination id keys.
   85     if (($key_by_destination_ids !== FALSE) && array_keys($stub) === range(0, count($stub) - 1)) {
   86       $stub = array_combine(array_keys($migration->getDestinationPlugin()->getIds()), $stub);
   87     }
   88     return $stub;
   89   }
   90 
   91   /**
   92    * Creates a stub.
   93    *
   94    * @param \Drupal\migrate\Plugin\MigrationInterface $migration
   95    *   The migration to use to create the stub.
   96    * @param array $source_ids
   97    *   The source ids to map to the stub.
   98    * @param array $default_values
   99    *   (optional) An array of values to include in the stub.
  100    *
  101    * @return array|bool
  102    *   An array of destination ids for the stub.
  103    *
  104    * @throws \Drupal\migrate\MigrateException
  105    */
  106   protected function doCreateStub(MigrationInterface $migration, array $source_ids, array $default_values = []) {
  107     $destination = $migration->getDestinationPlugin(TRUE);
  108     $process = $migration->getProcess();
  109     $id_map = $migration->getIdMap();
  110     $migrate_executable = new MigrateExecutable($migration);
  111     $row = new Row($source_ids + $migration->getSourceConfiguration(), $migration->getSourcePlugin()->getIds(), TRUE);
  112     $migrate_executable->processRow($row, $process);
  113     foreach ($default_values as $key => $value) {
  114       $row->setDestinationProperty($key, $value);
  115     }
  116     $destination_ids = [];
  117     try {
  118       $destination_ids = $destination->import($row);
  119     }
  120     catch (\Exception $e) {
  121       $id_map->saveMessage($row->getSourceIdValues(), $e->getMessage());
  122     }
  123     if ($destination_ids) {
  124       $id_map->saveIdMapping($row, $destination_ids, MigrateIdMapInterface::STATUS_NEEDS_UPDATE);
  125       return $destination_ids;
  126     }
  127     return FALSE;
  128   }
  129 
  130 }