Chris@0: sourceUrls = $configuration['urls']; Chris@0: Chris@0: // Set a default Accept header. Chris@0: /* $this->headers = array_merge(['Accept' => 'application/json'], Chris@0: $configuration['headers'] ?: []);*/ Chris@0: Chris@0: // See if this is a paged response with next links. If so, add to the source_urls array. Chris@0: /* foreach ( (array) $configuration['urls'] as $url) { Chris@0: $this->sourceUrls += $this->getNextLinks($url); Chris@0: }*/ Chris@0: } Chris@0: Chris@0: /** Chris@0: * Return a string representing the source URLs. Chris@0: * Chris@0: * @return string Chris@0: * Comma-separated list of URLs being imported. Chris@0: */ Chris@0: public function __toString() { Chris@0: // This could cause a problem when using a lot of urls, may need to hash. Chris@0: $urls = implode(', ', $this->sourceUrls); Chris@0: return $urls; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the initialized data parser plugin. Chris@0: * Chris@0: * @return \Drupal\migrate_plus\DataParserPluginInterface Chris@0: * The data parser plugin. Chris@0: */ Chris@0: public function getDataParserPlugin() { Chris@0: if (!isset($this->dataParserPlugin)) { Chris@0: $this->dataParserPlugin = \Drupal::service('plugin.manager.migrate_plus.data_parser')->createInstance($this->configuration['data_parser_plugin'], $this->configuration); Chris@0: } Chris@0: return $this->dataParserPlugin; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates and returns a filtered Iterator over the documents. Chris@0: * Chris@0: * @return \Iterator Chris@0: * An iterator over the documents providing source rows that match the Chris@0: * configured item_selector. Chris@0: */ Chris@0: protected function initializeIterator() { Chris@0: return $this->getDataParserPlugin(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Collect an array of next links from a paged response. Chris@0: */ Chris@0: /* protected function getNextLinks($url) { Chris@0: $urls = array(); Chris@0: $more = TRUE; Chris@0: while ($more == TRUE) { Chris@0: $response = $this->dataParserPlugin->getDataFetcher()->getResponse($url); Chris@0: if ($url = $this->getNextFromHeaders($response)) { Chris@0: $urls[] = $url; Chris@0: } Chris@0: elseif ($url = $this->getNextFromLinks($response)) { Chris@0: $urls[] = $url; Chris@0: } Chris@0: else { Chris@0: $more = FALSE; Chris@0: } Chris@0: } Chris@0: return $urls; Chris@0: } Chris@0: */ Chris@0: /** Chris@0: * See if the next link is in a 'links' group in the response. Chris@0: * Chris@0: * @param \Psr\Http\Message\ResponseInterface $response Chris@0: */ Chris@0: /* protected function getNextFromLinks(ResponseInterface $response) { Chris@0: $body = json_decode($response->getBody(), TRUE); Chris@0: if (!empty($body['links']) && array_key_exists('next', $body['links'])) { Chris@0: return $body['links']['next']; Chris@0: } Chris@0: return FALSE; Chris@0: } Chris@0: */ Chris@0: /** Chris@0: * See if the next link is in the header. Chris@0: * Chris@0: * @param \Psr\Http\Message\ResponseInterface $response Chris@0: */ Chris@0: /* protected function getNextFromHeaders(ResponseInterface $response) { Chris@0: $headers = $response->getHeader('Link'); Chris@0: foreach ($headers as $header) { Chris@0: $matches = array(); Chris@0: preg_match('/^<(.*)>; rel="next"$/', $header, $matches); Chris@0: if (!empty($matches) && !empty($matches[1])) { Chris@0: return $matches[1]; Chris@0: } Chris@0: } Chris@0: return FALSE; Chris@0: } Chris@0: */ Chris@0: }