class UrlResolver implements UrlResolverInterface (View source)

Converts oEmbed media URLs into endpoint-specific resource URLs.

Traits

Provides methods to use a cache backend while respecting a 'use caches' flag.

Properties

protected CacheBackendInterface $cacheBackend

Cache backend instance.

from  UseCacheBackendTrait
protected bool $useCaches

Flag whether caches should be used or skipped.

from  UseCacheBackendTrait
protected Client $httpClient

The HTTP client.

protected ProviderRepositoryInterface $providers

The OEmbed provider repository service.

protected ResourceFetcherInterface $resourceFetcher

The OEmbed resource fetcher service.

protected ModuleHandlerInterface $moduleHandler

The module handler service.

protected string[] $urlCache

Static cache of discovered oEmbed resource URLs, keyed by canonical URL.

Methods

object|false
cacheGet(string $cid)

Fetches from the cache backend, respecting the use caches flag.

cacheSet(string $cid, mixed $data, int $expire = Cache::PERMANENT, array $tags = [])

Stores data in the persistent cache, respecting the use caches flag.

__construct(ProviderRepositoryInterface $providers, ResourceFetcherInterface $resource_fetcher, ClientInterface $http_client, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend = NULL)

Constructs a UrlResolver object.

string|bool
discoverResourceUrl(string $url)

Runs oEmbed discovery and returns the endpoint URL if successful.

bool|string
findUrl(DOMXPath $xpath, string $format)

Tries to find the oEmbed URL in a DOM.

getProviderByUrl(string $url)

Tries to determine the oEmbed provider for a media asset URL.

string
getResourceUrl(string $url, int $max_width = NULL, int $max_height = NULL)

Builds the resource URL for a media asset URL.

Details

protected object|false cacheGet(string $cid)

Fetches from the cache backend, respecting the use caches flag.

Parameters

string $cid

The cache ID of the data to retrieve.

Return Value

object|false

The cache item or FALSE on failure.

See also

CacheBackendInterface::get

protected cacheSet(string $cid, mixed $data, int $expire = Cache::PERMANENT, array $tags = [])

Stores data in the persistent cache, respecting the use caches flag.

Parameters

string $cid

The cache ID of the data to store.

mixed $data

The data to store in the cache. Some storage engines only allow objects up to a maximum of 1MB in size to be stored by default. When caching large arrays or similar, take care to ensure $data does not exceed this size.

int $expire

One of the following values:

  • CacheBackendInterface::CACHE_PERMANENT: Indicates that the item should not be removed unless it is deleted explicitly.
  • A Unix timestamp: Indicates that the item will be considered invalid after this time, i.e. it will not be returned by get() unless $allow_invalid has been set to TRUE. When the item has expired, it may be permanently deleted by the garbage collector at any time.
array $tags

An array of tags to be stored with the cache item. These should normally identify objects used to build the cache item, which should trigger cache invalidation when updated. For example if a cached item represents a node, both the node ID and the author's user ID might be passed in as tags. For example array('node' => array(123), 'user' => array(92)).

See also

CacheBackendInterface::set

__construct(ProviderRepositoryInterface $providers, ResourceFetcherInterface $resource_fetcher, ClientInterface $http_client, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend = NULL)

Constructs a UrlResolver object.

Parameters

ProviderRepositoryInterface $providers

The oEmbed provider repository service.

ResourceFetcherInterface $resource_fetcher

The OEmbed resource fetcher service.

ClientInterface $http_client

The HTTP client.

ModuleHandlerInterface $module_handler

The module handler service.

CacheBackendInterface $cache_backend

(optional) The cache backend.

protected string|bool discoverResourceUrl(string $url)

Runs oEmbed discovery and returns the endpoint URL if successful.

Parameters

string $url

The resource's URL.

Return Value

string|bool

URL of the oEmbed endpoint, or FALSE if the discovery was unsuccessful.

protected bool|string findUrl(DOMXPath $xpath, string $format)

Tries to find the oEmbed URL in a DOM.

Parameters

DOMXPath $xpath

Page HTML as DOMXPath.

string $format

Format of oEmbed resource. Possible values are 'json' and 'xml'.

Return Value

bool|string

A URL to an oEmbed resource or FALSE if not found.

Provider getProviderByUrl(string $url)

Tries to determine the oEmbed provider for a media asset URL.

Parameters

string $url

The media asset URL.

Return Value

Provider

The oEmbed provider for the asset.

Exceptions

ResourceException
ProviderException

string getResourceUrl(string $url, int $max_width = NULL, int $max_height = NULL)

Builds the resource URL for a media asset URL.

Parameters

string $url

The media asset URL.

int $max_width

(optional) Maximum width of the oEmbed resource, in pixels.

int $max_height

(optional) Maximum height of the oEmbed resource, in pixels.

Return Value

string

Returns the resource URL corresponding to the given media item URL.