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