ReinstallSourceBase.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. namespace Drupal\reinstall\Plugin\migrate\source;
  3. use Drupal\Component\Plugin\ConfigurablePluginInterface;
  4. use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  5. use Drupal\migrate\MigrateException;
  6. use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
  7. use Drupal\migrate\Plugin\MigrationInterface;
  8. use Drupal\migrate\Row;
  9. use Drupal\reinstall\ReinstallEvents;
  10. use Drupal\reinstall\SourceEvent;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  13. use Symfony\Component\Yaml\Exception\ParseException;
  14. use Symfony\Component\Yaml\Yaml;
  15. /**
  16. * Class SimpleSource provides the basic mechanisms to load a YML entity dump.
  17. *
  18. * @MigrateSource(
  19. * id = "reinstall_base"
  20. * )
  21. *
  22. * Expected configuration in migration:source
  23. * - bundle: the bundle name of the migrated entities
  24. * - plugin: "reinstall_base", this plugin.
  25. * - type: the type of the migrated entities
  26. * - keys: the entity keys to use as the entities source keys when migrating.
  27. * - Defaults to ['id'].
  28. * - Revisionable content should use ['id', 'revision'].
  29. * - Maximum stability needs may be satisfied using ['uuid']
  30. * - Other variants might be useful.
  31. */
  32. class ReinstallSourceBase extends SourcePluginBase implements ContainerFactoryPluginInterface, ConfigurablePluginInterface {
  33. use SimpleSourceTrait;
  34. const INJECTED_ID_PREFIX = '_reinstall_';
  35. /**
  36. * The event_dispatcher service.
  37. *
  38. * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
  39. */
  40. protected $eventDispatcher;
  41. /**
  42. * The source records.
  43. *
  44. * MAY be altered by subscribing to MigrateEvents::PRE_IMPORT.
  45. *
  46. * @var array
  47. */
  48. public $records;
  49. /**
  50. * ReinstallSourceBase constructor.
  51. *
  52. * @param array $configuration
  53. * The plugin configuration.
  54. * @param string $plugin_id
  55. * The plugin id.
  56. * @param mixed $plugin_definition
  57. * The plugin definition.
  58. * @param \Drupal\migrate\Plugin\MigrationInterface $migration
  59. * The migration on which the plugin is invoked.
  60. * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
  61. * The event_dispatcher service.
  62. */
  63. public function __construct(
  64. array $configuration,
  65. $plugin_id,
  66. $plugin_definition,
  67. MigrationInterface $migration,
  68. EventDispatcherInterface $eventDispatcher
  69. ) {
  70. parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
  71. $this->eventDispatcher = $eventDispatcher;
  72. $parsedRecords = $this->initialParse($configuration);
  73. $this->records = empty($this->configuration['raw'])
  74. ? array_map([$this, 'flattenRecord'], $parsedRecords)
  75. : array_map([$this, 'injectIds'], $parsedRecords);
  76. $eventDispatcher->dispatch(ReinstallEvents::POST_SOURCE_PARSE, new SourceEvent($this));
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public static function create(
  82. ContainerInterface $container,
  83. array $configuration,
  84. $pluginId,
  85. $pluginDefinition,
  86. MigrationInterface $migration = NULL
  87. ) {
  88. $importPath = $container->getParameter('reinstall.path');
  89. $configuration['importPath'] = $importPath;
  90. $dispatcher = $container->get('event_dispatcher');
  91. return new static($configuration, $pluginId, $pluginDefinition, $migration, $dispatcher);
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function doCount() {
  97. return count($this->records);
  98. }
  99. /**
  100. * Flatten the field hierarchy. Not correct for all cases.
  101. *
  102. * @param array $record
  103. * The raw source values.
  104. *
  105. * @return array
  106. * The flattened values.
  107. *
  108. * @see \Drupal\reinstall\Plugin\migrate\process\TermParent
  109. */
  110. protected function flattenRecord(array $record) {
  111. $row = new Row($record);
  112. $this->flattenRow($row);
  113. return $row->getSource();
  114. }
  115. /**
  116. * Flatten a typical Drupal 8 field array to a 1-level array.
  117. */
  118. protected function flattenRow(Row $row) {
  119. $source = $row->getSource();
  120. foreach ($source as $key => &$item_list) {
  121. if (is_scalar($item_list)) {
  122. continue;
  123. }
  124. if (count($item_list) > 1) {
  125. $item = $item_list;
  126. }
  127. else {
  128. $item = reset($item_list);
  129. }
  130. // Handle bundle['target_id']
  131. // Exclude image field to keep metadata (alt / title)
  132. if (isset($item['target_id']) && !isset($item['alt']) && !isset($item['title'])) {
  133. $value = $item['target_id'];
  134. }
  135. elseif (is_scalar($item) || (count($item) != 1 && !isset($item['width']) && !isset($item['pid']))) {
  136. $value = $item;
  137. }
  138. elseif (isset($item['value'])) {
  139. $value = $item['value'];
  140. }
  141. elseif (isset($item['pid'])) {
  142. $value = $item['alias'];
  143. }
  144. else {
  145. $value = $item;
  146. }
  147. if (empty($item)) {
  148. $value = NULL;
  149. }
  150. $row->setSourceProperty($key, $value);
  151. }
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. protected function initializeIterator() {
  157. if (!isset($this->iterator)) {
  158. $this->iterator = new \ArrayIterator($this->records);
  159. }
  160. return $this->iterator;
  161. }
  162. /**
  163. * On raw sources, inject flat versions of the source ids.
  164. *
  165. * @param array $record
  166. * The array in which to inject the ids.
  167. *
  168. * @return array
  169. * The modified array.
  170. */
  171. protected function injectIds(array $record) {
  172. $idKeys = array_keys($this->getIds());
  173. foreach ($idKeys as $idKey) {
  174. $sourceIdKey = substr($idKey, strlen(static::INJECTED_ID_PREFIX));
  175. $current = $record[$sourceIdKey];
  176. while (is_array($current)) {
  177. $current = current($current);
  178. }
  179. $record[$idKey] = $current;
  180. }
  181. return $record;
  182. }
  183. /**
  184. * Load then parse the file requested in configuration and return its records.
  185. *
  186. * @param array $configuration
  187. * The source configuration from the migration source section.
  188. * @param string $key
  189. * Optional. A top-level key for the source document. If empty, items will
  190. * be parsed from the root of the source document.
  191. *
  192. * @return array
  193. * An array of entity descriptions.
  194. *
  195. * @throws \Drupal\migrate\MigrateException
  196. */
  197. protected function initialParse(array $configuration, string $key = NULL) {
  198. $this->sstEntityType = $type = $configuration['type'];
  199. $bundle = $configuration['bundle'];
  200. $baseFilePath = $this->configuration['file'] ?? "${type}/${bundle}.yml";
  201. $importPath = $configuration['importPath'] ?? NULL;
  202. $filePath = "$importPath/$baseFilePath";
  203. $realPath = realpath($filePath);
  204. if (!is_file($realPath) || !is_readable($realPath)) {
  205. throw new MigrateException("${filePath} is not a readable file.");
  206. }
  207. try {
  208. $raw = file_get_contents($filePath);
  209. $data = Yaml::parse($raw);
  210. }
  211. catch (ParseException $e) {
  212. throw new MigrateException("Cannot parse the contents of ${filePath}.");
  213. }
  214. if ($key) {
  215. return $data[$key] ?? [];
  216. }
  217. return $data ?? [];
  218. }
  219. /**
  220. * {@inheritdoc}
  221. */
  222. public function __toString() {
  223. $current = $this->getIterator()->current();
  224. $ret = json_encode($current, JSON_PRETTY_PRINT);
  225. return $ret;
  226. }
  227. /**
  228. * Gets this plugin's configuration.
  229. *
  230. * @return array
  231. * An array of this plugin's configuration.
  232. */
  233. public function getConfiguration() {
  234. return $this->configuration;
  235. }
  236. /**
  237. * Sets the configuration for this plugin instance.
  238. *
  239. * @param array $configuration
  240. * An associative array containing the plugin's configuration.
  241. */
  242. public function setConfiguration(array $configuration) {
  243. $this->configuration = $configuration;
  244. }
  245. /**
  246. * Gets default configuration for this plugin.
  247. *
  248. * @return array
  249. * An associative array with the default configuration.
  250. */
  251. public function defaultConfiguration() {
  252. return [];
  253. }
  254. /**
  255. * Calculates dependencies for the configured plugin.
  256. *
  257. * Dependencies are saved in the plugin's configuration entity and are used to
  258. * determine configuration synchronization order. For example, if the plugin
  259. * integrates with specific user roles, this method should return an array of
  260. * dependencies listing the specified roles.
  261. *
  262. * @return array
  263. * An array of dependencies grouped by type (config, content, module,
  264. * theme). For example:
  265. *
  266. * @code
  267. * array(
  268. * 'config' => array('user.role.anonymous', 'user.role.authenticated'),
  269. * 'content' => array('node:article:f0a189e6-55fb-47fb-8005-5bef81c44d6d'),
  270. * 'module' => array('node', 'user'),
  271. * 'theme' => array('seven'),
  272. * );
  273. * @endcode
  274. *
  275. * @see \Drupal\Core\Config\Entity\ConfigDependencyManager
  276. * @see \Drupal\Core\Entity\EntityInterface::getConfigDependencyName()
  277. */
  278. public function calculateDependencies() {
  279. return [];
  280. }
  281. }