class DefaultFetcher

Defines a default fetcher implementation.

Uses the http_default_client service to download the feed.

Plugin annotation


@AggregatorFetcher(
  id = "aggregator",
  title = @Translation("Default fetcher"),
  description = @Translation("Downloads data from a URL using Drupal's HTTP request handler.")
)

Hierarchy

Expanded class hierarchy of DefaultFetcher

1 file declares its use of DefaultFetcher
TestFetcher.php in drupal/core/modules/aggregator/tests/modules/aggregator_test/lib/Drupal/aggregator_test/Plugin/aggregator/fetcher/TestFetcher.php
Contains \Drupal\aggregator_test\Plugin\aggregator\fetcher\TestFetcher.

File

drupal/core/modules/aggregator/lib/Drupal/aggregator/Plugin/aggregator/fetcher/DefaultFetcher.php, line 28
Contains \Drupal\aggregator\Plugin\aggregator\fetcher\DefaultFetcher.

Namespace

Drupal\aggregator\Plugin\aggregator\fetcher
View source
class DefaultFetcher implements FetcherInterface {

  /**
   * Implements \Drupal\aggregator\Plugin\FetcherInterface::fetch().
   */
  public function fetch(Feed $feed) {

    // @todo: Inject the http client.
    $request = \Drupal::httpClient()
      ->get($feed->url->value);
    $feed->source_string = FALSE;

    // Generate conditional GET headers.
    if ($feed->etag->value) {
      $request
        ->addHeader('If-None-Match', $feed->etag->value);
    }
    if ($feed->modified->value) {
      $request
        ->addHeader('If-Modified-Since', gmdate(DATE_RFC1123, $feed->modified->value));
    }
    try {
      $response = $request
        ->send();
      $feed->source_string = $response
        ->getBody(TRUE);
      $feed->etag = $response
        ->getEtag();
      $feed->modified = strtotime($response
        ->getLastModified());
      $feed->http_headers = $response
        ->getHeaders();

      // Update the feed URL in case of a 301 redirect.
      if ($previous_response = $response
        ->getPreviousResponse()) {
        if ($previous_response
          ->getStatusCode() == 301 && ($location = $previous_response
          ->getLocation())) {
          $feed->url->value = $location;
        }
      }
      return TRUE;
    } catch (BadResponseException $e) {
      $response = $e
        ->getResponse();
      watchdog('aggregator', 'The feed from %site seems to be broken due to "%error".', array(
        '%site' => $feed
          ->label(),
        '%error' => $response
          ->getStatusCode() . ' ' . $response
          ->getReasonPhrase(),
      ), WATCHDOG_WARNING);
      drupal_set_message(t('The feed from %site seems to be broken because of error "%error".', array(
        '%site' => $feed
          ->label(),
        '%error' => $response
          ->getStatusCode() . ' ' . $response
          ->getReasonPhrase(),
      )));
      return FALSE;
    } catch (RequestException $e) {
      watchdog('aggregator', 'The feed from %site seems to be broken due to "%error".', array(
        '%site' => $feed
          ->label(),
        '%error' => $e
          ->getMessage(),
      ), WATCHDOG_WARNING);
      drupal_set_message(t('The feed from %site seems to be broken because of error "%error".', array(
        '%site' => $feed
          ->label(),
        '%error' => $e
          ->getMessage(),
      )));
      return FALSE;
    }
  }

}

Members