Chris@0: urls = $configuration['urls']; Chris@0: $this->itemSelector = $configuration['item_selector']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { Chris@0: return new static($configuration, $plugin_id, $plugin_definition); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the initialized data fetcher plugin. Chris@0: * Chris@0: * @return \Drupal\migrate_plus\DataFetcherPluginInterface Chris@0: * The data fetcher plugin. Chris@0: */ Chris@0: public function getDataFetcherPlugin() { Chris@0: if (!isset($this->dataFetcherPlugin)) { Chris@0: $this->dataFetcherPlugin = \Drupal::service('plugin.manager.migrate_plus.data_fetcher')->createInstance($this->configuration['data_fetcher_plugin'], $this->configuration); Chris@0: } Chris@0: return $this->dataFetcherPlugin; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function rewind() { Chris@0: $this->activeUrl = NULL; Chris@0: $this->next(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implementation of Iterator::next(). Chris@0: */ Chris@0: public function next() { Chris@0: $this->currentItem = $this->currentId = NULL; Chris@0: if (is_null($this->activeUrl)) { Chris@0: if (!$this->nextSource()) { Chris@0: // No data to import. Chris@0: return; Chris@0: } Chris@0: } Chris@0: // At this point, we have a valid open source url, try to fetch a row from Chris@0: // it. Chris@0: $this->fetchNextRow(); Chris@0: // If there was no valid row there, try the next url (if any). Chris@0: if (is_null($this->currentItem)) { Chris@4: while ($this->nextSource()) { Chris@0: $this->fetchNextRow(); Chris@4: if ($this->valid()) { Chris@4: break; Chris@4: } Chris@0: } Chris@0: } Chris@0: if ($this->valid()) { Chris@0: foreach ($this->configuration['ids'] as $id_field_name => $id_info) { Chris@0: $this->currentId[$id_field_name] = $this->currentItem[$id_field_name]; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Opens the specified URL. Chris@0: * Chris@4: * @param string $url Chris@0: * URL to open. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if the URL was successfully opened, FALSE otherwise. Chris@0: */ Chris@0: abstract protected function openSourceUrl($url); Chris@0: Chris@0: /** Chris@4: * Retrieves the next row of data. populating currentItem. Chris@4: * Chris@4: * Retrieves from the open source URL. Chris@0: */ Chris@0: abstract protected function fetchNextRow(); Chris@0: Chris@0: /** Chris@0: * Advances the data parser to the next source url. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if a valid source URL was opened Chris@0: */ Chris@0: protected function nextSource() { Chris@0: while ($this->activeUrl === NULL || (count($this->urls) - 1) > $this->activeUrl) { Chris@0: if (is_null($this->activeUrl)) { Chris@0: $this->activeUrl = 0; Chris@0: } Chris@0: else { Chris@0: // Increment the activeUrl so we try to load the next source. Chris@0: $this->activeUrl = $this->activeUrl + 1; Chris@0: if ($this->activeUrl >= count($this->urls)) { Chris@0: return FALSE; Chris@0: } Chris@0: } Chris@0: Chris@0: if ($this->openSourceUrl($this->urls[$this->activeUrl])) { Chris@0: // We have a valid source. Chris@0: return TRUE; Chris@0: } Chris@0: } Chris@0: Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function current() { Chris@0: return $this->currentItem; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function key() { Chris@0: return $this->currentId; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function valid() { Chris@0: return !empty($this->currentItem); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function count() { Chris@0: $count = 0; Chris@0: foreach ($this as $item) { Chris@0: $count++; Chris@0: } Chris@0: return $count; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Return the selectors used to populate each configured field. Chris@0: * Chris@0: * @return string[] Chris@0: * Array of selectors, keyed by field name. Chris@0: */ Chris@0: protected function fieldSelectors() { Chris@0: $fields = []; Chris@0: foreach ($this->configuration['fields'] as $field_info) { Chris@0: if (isset($field_info['selector'])) { Chris@0: $fields[$field_info['name']] = $field_info['selector']; Chris@0: } Chris@0: } Chris@0: return $fields; Chris@0: } Chris@0: Chris@0: }