You are here

public function CdnSettings::getCdnDomain in CDN 8.3

Maps a URI to a CDN domain.

Parameters

string $uri: The URI to map.

Return value

string|bool The mapped domain, or FALSE if it could not be matched.

File

src/CdnSettings.php, line 187

Class

CdnSettings
Wraps the CDN settings configuration, contains all parsing.

Namespace

Drupal\cdn

Code

public function getCdnDomain($uri) {

  // Extension-specific mapping.
  $file_extension = mb_strtolower(pathinfo($uri, PATHINFO_EXTENSION));
  $lookup_table = $this
    ->getLookupTable();
  if (isset($lookup_table[$file_extension])) {
    $key = $file_extension;
  }
  elseif (isset($lookup_table['*'])) {
    $key = '*';
  }
  else {
    return FALSE;
  }
  $result = $lookup_table[$key];
  if ($result === FALSE) {
    return FALSE;
  }
  elseif (is_array($result)) {
    $filename = basename($uri);
    $hash = hexdec(substr(md5($filename), 0, 5));
    $cdn_domain = $result[$hash % count($result)];
  }
  else {
    $cdn_domain = $result;
  }
  return $cdn_domain;
}