Drupal investigation

DataParserPluginBase.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace Drupal\migrate_plus;
  3. use Drupal\Core\Plugin\PluginBase;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. /**
  6. * Defines a base data parser implementation.
  7. *
  8. * @see \Drupal\migrate_plus\Annotation\DataParser
  9. * @see \Drupal\migrate_plus\DataParserPluginInterface
  10. * @see \Drupal\migrate_plus\DataParserPluginManager
  11. * @see plugin_api
  12. */
  13. abstract class DataParserPluginBase extends PluginBase implements DataParserPluginInterface {
  14. /**
  15. * List of source urls.
  16. *
  17. * @var string[]
  18. */
  19. protected $urls;
  20. /**
  21. * Index of the currently-open url.
  22. *
  23. * @var int
  24. */
  25. protected $activeUrl;
  26. /**
  27. * String indicating how to select an item's data from the source.
  28. *
  29. * @var string
  30. */
  31. protected $itemSelector;
  32. /**
  33. * Current item when iterating.
  34. *
  35. * @var mixed
  36. */
  37. protected $currentItem = NULL;
  38. /**
  39. * Value of the ID for the current item when iterating.
  40. *
  41. * @var string
  42. */
  43. protected $currentId = NULL;
  44. /**
  45. * The data retrieval client.
  46. *
  47. * @var \Drupal\migrate_plus\DataFetcherPluginInterface
  48. */
  49. protected $dataFetcher;
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function __construct(array $configuration, $plugin_id, $plugin_definition) {
  54. parent::__construct($configuration, $plugin_id, $plugin_definition);
  55. $this->urls = $configuration['urls'];
  56. $this->itemSelector = $configuration['item_selector'];
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  62. return new static($configuration, $plugin_id, $plugin_definition);
  63. }
  64. /**
  65. * Returns the initialized data fetcher plugin.
  66. *
  67. * @return \Drupal\migrate_plus\DataFetcherPluginInterface
  68. * The data fetcher plugin.
  69. */
  70. public function getDataFetcherPlugin() {
  71. if (!isset($this->dataFetcherPlugin)) {
  72. $this->dataFetcherPlugin = \Drupal::service('plugin.manager.migrate_plus.data_fetcher')->createInstance($this->configuration['data_fetcher_plugin'], $this->configuration);
  73. }
  74. return $this->dataFetcherPlugin;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function rewind() {
  80. $this->activeUrl = NULL;
  81. $this->next();
  82. }
  83. /**
  84. * Implementation of Iterator::next().
  85. */
  86. public function next() {
  87. $this->currentItem = $this->currentId = NULL;
  88. if (is_null($this->activeUrl)) {
  89. if (!$this->nextSource()) {
  90. // No data to import.
  91. return;
  92. }
  93. }
  94. // At this point, we have a valid open source url, try to fetch a row from
  95. // it.
  96. $this->fetchNextRow();
  97. // If there was no valid row there, try the next url (if any).
  98. if (is_null($this->currentItem)) {
  99. if ($this->nextSource()) {
  100. $this->fetchNextRow();
  101. }
  102. }
  103. if ($this->valid()) {
  104. foreach ($this->configuration['ids'] as $id_field_name => $id_info) {
  105. $this->currentId[$id_field_name] = $this->currentItem[$id_field_name];
  106. }
  107. }
  108. }
  109. /**
  110. * Opens the specified URL.
  111. *
  112. * @param $url
  113. * URL to open.
  114. *
  115. * @return bool
  116. * TRUE if the URL was successfully opened, FALSE otherwise.
  117. */
  118. abstract protected function openSourceUrl($url);
  119. /**
  120. * Retrieves the next row of data from the open source URL, populating
  121. * currentItem.
  122. */
  123. abstract protected function fetchNextRow();
  124. /**
  125. * Advances the data parser to the next source url.
  126. *
  127. * @return bool
  128. * TRUE if a valid source URL was opened
  129. */
  130. protected function nextSource() {
  131. while ($this->activeUrl === NULL || (count($this->urls) - 1) > $this->activeUrl) {
  132. if (is_null($this->activeUrl)) {
  133. $this->activeUrl = 0;
  134. }
  135. else {
  136. // Increment the activeUrl so we try to load the next source.
  137. $this->activeUrl = $this->activeUrl + 1;
  138. if ($this->activeUrl >= count($this->urls)) {
  139. return FALSE;
  140. }
  141. }
  142. if ($this->openSourceUrl($this->urls[$this->activeUrl])) {
  143. // We have a valid source.
  144. return TRUE;
  145. }
  146. }
  147. return FALSE;
  148. }
  149. /**
  150. * {@inheritdoc}
  151. */
  152. public function current() {
  153. return $this->currentItem;
  154. }
  155. /**
  156. * {@inheritdoc}
  157. */
  158. public function key() {
  159. return $this->currentId;
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. public function valid() {
  165. return !empty($this->currentItem);
  166. }
  167. /**
  168. * {@inheritdoc}
  169. */
  170. public function count() {
  171. $count = 0;
  172. foreach ($this as $item) {
  173. $count++;
  174. }
  175. return $count;
  176. }
  177. /**
  178. * Return the selectors used to populate each configured field.
  179. *
  180. * @return string[]
  181. * Array of selectors, keyed by field name.
  182. */
  183. protected function fieldSelectors() {
  184. $fields = [];
  185. foreach ($this->configuration['fields'] as $field_info) {
  186. if (isset($field_info['selector'])) {
  187. $fields[$field_info['name']] = $field_info['selector'];
  188. }
  189. }
  190. return $fields;
  191. }
  192. }