UrlResolver.php in Drupal 9
File
core/modules/media/src/OEmbed/UrlResolver.php
View source
<?php
namespace Drupal\media\OEmbed;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\TransferException;
class UrlResolver implements UrlResolverInterface {
protected $httpClient;
protected $providers;
protected $resourceFetcher;
protected $moduleHandler;
protected $urlCache = [];
protected $cacheBackend;
public function __construct(ProviderRepositoryInterface $providers, ResourceFetcherInterface $resource_fetcher, ClientInterface $http_client, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend = NULL) {
$this->providers = $providers;
$this->resourceFetcher = $resource_fetcher;
$this->httpClient = $http_client;
$this->moduleHandler = $module_handler;
if (empty($cache_backend)) {
$cache_backend = \Drupal::cache();
@trigger_error('Passing NULL as the $cache_backend parameter to ' . __METHOD__ . '() is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. See https://www.drupal.org/node/3223594', E_USER_DEPRECATED);
}
$this->cacheBackend = $cache_backend;
}
protected function discoverResourceUrl($url) {
try {
$response = $this->httpClient
->get($url);
} catch (TransferException $e) {
return FALSE;
}
$document = Html::load((string) $response
->getBody());
$xpath = new \DOMXpath($document);
return $this
->findUrl($xpath, 'json') ?: $this
->findUrl($xpath, 'xml');
}
protected function findUrl(\DOMXPath $xpath, $format) {
$result = $xpath
->query("//link[@type='application/{$format}+oembed']");
return $result->length ? $result
->item(0)
->getAttribute('href') : FALSE;
}
public function getProviderByUrl($url) {
foreach ($this->providers
->getAll() as $provider_name => $provider_info) {
foreach ($provider_info
->getEndpoints() as $endpoint) {
if ($endpoint
->matchUrl($url)) {
return $provider_info;
}
}
}
$resource_url = $this
->discoverResourceUrl($url);
if ($resource_url) {
return $this->resourceFetcher
->fetchResource($resource_url)
->getProvider();
}
throw new ResourceException('No matching provider found.', $url);
}
public function getResourceUrl($url, $max_width = NULL, $max_height = NULL) {
if (isset($this->urlCache[$url])) {
return $this->urlCache[$url];
}
$cache_id = "media:oembed_resource_url:{$url}:{$max_width}:{$max_height}";
$cached = $this->cacheBackend
->get($cache_id);
if ($cached) {
$this->urlCache[$url] = $cached->data;
return $this->urlCache[$url];
}
$provider = $this
->getProviderByUrl($url);
$resource_url = $this
->getEndpointMatchingUrl($url, $provider);
$parsed_url = UrlHelper::parse($resource_url);
if ($max_width) {
$parsed_url['query']['maxwidth'] = $max_width;
}
if ($max_height) {
$parsed_url['query']['maxheight'] = $max_height;
}
$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->cacheBackend
->set($cache_id, $resource_url);
return $resource_url;
}
protected function getEndpointMatchingUrl($url, Provider $provider) {
$endpoints = $provider
->getEndpoints();
$resource_url = reset($endpoints)
->buildResourceUrl($url);
foreach ($endpoints as $endpoint) {
if ($endpoint
->matchUrl($url)) {
$resource_url = $endpoint
->buildResourceUrl($url);
break;
}
}
return $resource_url ?? reset($endpoints)
->buildResourceUrl($url);
}
}
Classes
Name |
Description |
UrlResolver |
Converts oEmbed media URLs into endpoint-specific resource URLs. |