diff --git a/core/modules/media/src/OEmbed/Endpoint.php b/core/modules/media/src/OEmbed/Endpoint.php index 0990cba83c..2eefde9a16 100644 --- a/core/modules/media/src/OEmbed/Endpoint.php +++ b/core/modules/media/src/OEmbed/Endpoint.php @@ -33,7 +33,7 @@ class Endpoint { * * @var bool */ - protected $supportDiscovery; + protected $supportsDiscovery; /** * Endpoint constructor. @@ -44,29 +44,36 @@ class Endpoint { * List of URL schemes supported by the provider. * @param array $formats * List of supported formats. Can be "json", "xml" or both. - * @param bool $supportDiscovery + * @param bool $supports_discovery * Whether the provider supports oEmbed discovery. * * @throws \Drupal\media\OEmbed\ProviderException */ - public function __construct($url, array $schemes = [], array $formats = [], $supportDiscovery = FALSE) { + public function __construct($url, array $schemes = [], array $formats = [], $supports_discovery = FALSE) { if (!$url) { throw new ProviderException('Provider does not provide any endpoint URLs.'); } $this->url = $url; $this->schemes = $schemes; $this->formats = $formats; - $this->supportDiscovery = $supportDiscovery; + $this->supportsDiscovery = $supports_discovery; } /** * Returns the endpoint's URL. * + * The URL will be build with the first defined format. If the endpoint + * doesn't provide a format, the JSON format will be used. + * * @return string * Endpoint URL. */ public function getUrl() { - return $this->url; + $format = 'json'; + if (!empty($this->getFormats()) && is_array($this->getFormats())) { + $format = reset($this->getFormats()); + } + return str_replace('{format}', $format, $this->url); } /** @@ -95,8 +102,8 @@ public function getFormats() { * @return bool * TRUE or FALSE. */ - public function supportDiscovery() { - return $this->supportDiscovery; + public function supportsDiscovery() { + return $this->supportsDiscovery; } } diff --git a/core/modules/media/src/OEmbed/Provider.php b/core/modules/media/src/OEmbed/Provider.php index dc19a6c0e7..89dfeefa81 100644 --- a/core/modules/media/src/OEmbed/Provider.php +++ b/core/modules/media/src/OEmbed/Provider.php @@ -2,6 +2,8 @@ namespace Drupal\media\OEmbed; +use Drupal\Component\Utility\UrlHelper; + /** * Value object for oEmbed providers. */ @@ -37,8 +39,13 @@ class Provider { * The provider $url. * @param \Drupal\media\OEmbed\Endpoint[] $endpoints * List of endpoints this provider exposes. + * + * @throws \Drupal\media\OEmbed\ProviderException */ public function __construct($name, $url, array $endpoints) { + if (empty($endpoints)) { + throw new ProviderException('Provider @name does not define any endpoints.', $this); + } $this->name = $name; $this->url = $url; $this->endpoints = $endpoints; @@ -74,4 +81,20 @@ public function getEndpoints() { return $this->endpoints; } + /** + * Builds the endpoint URL and returns it. + * + * @param string $url + * The canonical media URL. + * + * @return string + * URL of the oEmbed endpoint. + */ + public function buildResourceUrl($url) { + $query = ['url' => $url]; + foreach ($this->getEndpoints() as $endpoint) { + return $endpoint->getUrl() . '?' . UrlHelper::buildQuery($query); + } + } + } diff --git a/core/modules/media/src/OEmbed/ResourceFetcher.php b/core/modules/media/src/OEmbed/ResourceFetcher.php index 56e822f7e2..c119ce0948 100644 --- a/core/modules/media/src/OEmbed/ResourceFetcher.php +++ b/core/modules/media/src/OEmbed/ResourceFetcher.php @@ -93,7 +93,7 @@ public function getResourceUrl($url, $max_width = NULL, $max_height = NULL) { $provider = $this->providerDiscovery->getProviderByUrl($url); if ($provider) { - $resource_url = $this->buildResourceUrl($provider, $url); + $resource_url = $provider->buildResourceUrl($url); } else { $resource_url = $this->discoverResourceUrl($url); @@ -121,40 +121,6 @@ public function getResourceUrl($url, $max_width = NULL, $max_height = NULL) { return $resource_url; } - /** - * Builds the endpoint URL with the oEmebed provider info and returns it. - * - * @param Provider $provider_info - * Provider info returned form - * Drupal\media\OEmbed\ProviderDiscoveryInterface::get(). - * @param string $url - * The canonical media URL. - * - * @return bool|string - * URL of the oEmbed endpoint, or FALSE if the building was not successful. - * - * @throws \Drupal\media\OEmbed\ProviderException - */ - protected function buildResourceUrl(Provider $provider_info, $url) { - if (empty($provider_info->getEndpoints())) { - throw new ProviderException('Provider @name does not define any endpoints.', $provider_info); - } - - $query = [ - 'url' => $url, - ]; - foreach ($provider_info->getEndpoints() as $endpoint) { - $format = 'json'; - if (!empty($endpoint->getFormats()) && is_array($endpoint->getFormats())) { - $format = reset($endpoint->getFormats()); - } - $endpoint_url = str_replace('{format}', $format, $endpoint->getUrl()); - - return $endpoint_url . '?' . UrlHelper::buildQuery($query); - } - return FALSE; - } - /** * Runs oEmbed discovery and returns the endpoint URL if successful. *