You are here

protected function CdnSettings::buildLookupTable in CDN 8.3

Builds a lookup table: file extension to CDN domain(s).

@todo Abstract this out further in the future if the need arises, i.e. if more conditions besides extensions are added. For now, KISS.

Parameters

array $mapping: An array matching either of the mappings in cdn.mapping.schema.yml.

Return value

array A lookup table. Keys are lowercase file extensions or the asterisk. Values are CDN domains (either string if only one, or array of strings if multiple).

Throws

\Drupal\Core\Config\ConfigValueException

1 call to CdnSettings::buildLookupTable()
CdnSettings::getLookupTable in src/CdnSettings.php
Returns the lookup table.

File

src/CdnSettings.php, line 122

Class

CdnSettings
Wraps the CDN settings configuration, contains all parsing.

Namespace

Drupal\cdn

Code

protected function buildLookupTable(array $mapping) : array {
  assert(!\Drupal::hasContainer() || \Drupal::service('config.typed')
    ->get('cdn.settings')
    ->validate()
    ->count() === 0, 'There are validation errors for the "cdn.settings" configuration.');
  $lookup_table = [];
  if ($mapping['type'] === 'simple') {
    $domain = $mapping['domain'];
    if (empty($mapping['conditions'])) {
      $lookup_table['*'] = $domain;
    }
    else {
      if (empty($mapping['conditions']['extensions'])) {
        $lookup_table['*'] = $domain;
      }
      else {
        foreach ($mapping['conditions']['extensions'] as $extension) {
          $lookup_table[$extension] = $domain;
        }
      }
      if (isset($mapping['conditions']['not'])) {
        assert(!isset($mapping['conditions']['extensions']), 'It does not make sense to provide an \'extensions\' condition as well as a negated \'extensions\' condition.');
        if (!empty($mapping['conditions']['not']['extensions'])) {
          foreach ($mapping['conditions']['not']['extensions'] as $not_extension) {
            $lookup_table[$not_extension] = FALSE;
          }
        }
      }
    }
  }
  elseif ($mapping['type'] === 'complex') {
    $fallback_domain = NULL;
    if (isset($mapping['fallback_domain'])) {
      $fallback_domain = $mapping['fallback_domain'];
      $lookup_table['*'] = $fallback_domain;
    }
    for ($i = 0; $i < count($mapping['domains']); $i++) {
      $nested_mapping = $mapping['domains'][$i];
      assert(!empty($nested_mapping['conditions']), 'The nested mapping ' . $i . ' includes no conditions, which is not allowed for complex mappings.');
      assert(!isset($nested_mapping['conditions']['not']), 'The nested mapping ' . $i . ' includes negated conditions, which is not allowed for complex mappings: the fallback_domain already serves this purpose.');
      $lookup_table += $this
        ->buildLookupTable($nested_mapping);
    }
  }
  elseif ($mapping['type'] === 'auto-balanced') {
    if (empty($mapping['conditions']) || empty($mapping['conditions']['extensions'])) {
      throw new ConfigValueException('It does not make sense to apply auto-balancing to all files, regardless of extension.');
    }
    $domains = $mapping['domains'];
    foreach ($mapping['conditions']['extensions'] as $extension) {
      $lookup_table[$extension] = $domains;
    }
  }
  else {
    throw new ConfigValueException('Unknown CDN mapping type specified.');
  }
  return $lookup_table;
}