You are here

public function ProviderRepositoryDecorator::getAll in oEmbed Providers 1.0.x

Same name and namespace in other branches
  1. 2.x src/OEmbed/ProviderRepositoryDecorator.php \Drupal\oembed_providers\OEmbed\ProviderRepositoryDecorator::getAll()
  2. 1.1.x src/OEmbed/ProviderRepositoryDecorator.php \Drupal\oembed_providers\OEmbed\ProviderRepositoryDecorator::getAll()

Returns information on all available oEmbed providers.

Return value

\Drupal\media\OEmbed\Provider[] Returns an array of provider value objects, keyed by provider name.

Throws

\Drupal\media\OEmbed\ProviderException If the oEmbed provider information cannot be retrieved.

Overrides ProviderRepositoryInterface::getAll

1 call to ProviderRepositoryDecorator::getAll()
ProviderRepositoryDecorator::get in src/OEmbed/ProviderRepositoryDecorator.php
Returns information for a specific oEmbed provider.

File

src/OEmbed/ProviderRepositoryDecorator.php, line 106

Class

ProviderRepositoryDecorator
Decorates the oEmbed ProviderRepository provided by core Media module.

Namespace

Drupal\oembed_providers\OEmbed

Code

public function getAll() {
  $cache_id = 'oembed_providers:oembed_providers';
  $cached = $this
    ->cacheGet($cache_id);
  if ($cached) {
    return $cached->data;
  }
  $custom_providers = $this
    ->getCustomProviders();
  if ($this->externalFetch) {
    try {
      $response = $this->httpClient
        ->request('GET', $this->providersUrl);
    } catch (RequestException $e) {
      throw new ProviderException("Could not retrieve the oEmbed provider database from {$this->providersUrl}", NULL, $e);
    }
    $providers = Json::decode((string) $response
      ->getBody());
    if (!is_array($providers) || empty($providers)) {
      throw new ProviderException('Remote oEmbed providers database returned invalid or empty list.');
    }

    // Providers defined by provider database cannot be modified by
    // custom oEmbed provider definitions.
    $providers = array_merge($custom_providers, $providers);
  }
  else {
    $providers = $custom_providers;
  }
  usort($providers, function ($a, $b) {
    return strcasecmp($a['provider_name'], $b['provider_name']);
  });
  $keyed_providers = [];
  foreach ($providers as $provider) {
    try {
      $name = (string) $provider['provider_name'];
      $keyed_providers[$name] = new Provider($provider['provider_name'], $provider['provider_url'], $provider['endpoints']);
    } catch (ProviderException $e) {

      // Just skip all the invalid providers.
      // @todo Log the exception message to help with debugging.
    }
  }
  $this
    ->cacheSet($cache_id, $keyed_providers, $this->time
    ->getCurrentTime() + $this->maxAge);
  return $keyed_providers;
}