You are here

class InstagramUrlResolver in Media entity Instagram 3.x

Converts oEmbed media URLs into endpoint-specific resource URLs.

Hierarchy

Expanded class hierarchy of InstagramUrlResolver

1 string reference to 'InstagramUrlResolver'
media_entity_instagram.services.yml in ./media_entity_instagram.services.yml
media_entity_instagram.services.yml
1 service uses InstagramUrlResolver
media_entity_instagram.oembed.url_resolver in ./media_entity_instagram.services.yml
Drupal\media_entity_instagram\OEmbed\InstagramUrlResolver

File

src/OEmbed/InstagramUrlResolver.php, line 11

Namespace

Drupal\media_entity_instagram\OEmbed
View source
class InstagramUrlResolver extends UrlResolver {

  /**
   * {@inheritdoc}
   */
  public function getResourceUrl($url, $max_width = NULL, $max_height = NULL, $settings = []) {

    // Try to get the resource URL from the static cache.
    if (isset($this->urlCache[$url])) {
      return $this->urlCache[$url];
    }

    // Try to get the resource URL from the persistent cache.
    $cache_id = "media:oembed_resource_url:{$url}:{$max_width}:{$max_height}:" . serialize($settings);
    $cached = $this
      ->cacheGet($cache_id);
    if ($cached) {
      $this->urlCache[$url] = $cached->data;
      return $this->urlCache[$url];
    }
    $provider = $this
      ->getProviderByUrl($url);
    $endpoints = $provider
      ->getEndpoints();
    $endpoint = reset($endpoints);
    $resource_url = $endpoint
      ->buildResourceUrl($url);
    $parsed_url = UrlHelper::parse($resource_url);
    if ($max_width) {
      $parsed_url['query']['maxwidth'] = $max_width;
    }
    if ($settings['hidecaption']) {
      $parsed_url['query']['hidecaption'] = 1;
    }

    // Let other modules alter the resource URL, because some oEmbed providers
    // provide extra parameters in the query string. For example, Instagram also
    // supports the 'omitscript' parameter.
    $this->moduleHandler
      ->alter('oembed_resource_url', $parsed_url, $provider);
    $resource_url = $parsed_url['path'] . '?' . rawurldecode(UrlHelper::buildQuery($parsed_url['query']));
    $this->urlCache[$url] = $resource_url;
    $this
      ->cacheSet($cache_id, $resource_url);
    return $resource_url;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
InstagramUrlResolver::getResourceUrl public function Builds the resource URL for a media asset URL. Overrides UrlResolver::getResourceUrl
UrlResolver::$cacheBackend protected property The cache backend.
UrlResolver::$httpClient protected property The HTTP client.
UrlResolver::$moduleHandler protected property The module handler service.
UrlResolver::$providers protected property The OEmbed provider repository service.
UrlResolver::$resourceFetcher protected property The OEmbed resource fetcher service.
UrlResolver::$urlCache protected property Static cache of discovered oEmbed resource URLs, keyed by canonical URL.
UrlResolver::discoverResourceUrl protected function Runs oEmbed discovery and returns the endpoint URL if successful.
UrlResolver::findUrl protected function Tries to find the oEmbed URL in a DOM.
UrlResolver::getEndpointMatchingUrl protected function For the given media item URL find an endpoint with schemes that match.
UrlResolver::getProviderByUrl public function Tries to determine the oEmbed provider for a media asset URL. Overrides UrlResolverInterface::getProviderByUrl
UrlResolver::__construct public function Constructs a UrlResolver object.