You are here

protected function LibraryPolicyBuilder::getLibrarySources in Content-Security-Policy 8

Get the required sources for a single library.

Parameters

string $extension: The name of the extension that registered a library.

string $name: The name of a registered library to retrieve.

Return value

array An array of sources keyed by type.

1 call to LibraryPolicyBuilder::getLibrarySources()
LibraryPolicyBuilder::getExtensionSources in src/LibraryPolicyBuilder.php
Get the required sources for an extension.

File

src/LibraryPolicyBuilder.php, line 167

Class

LibraryPolicyBuilder
Service to build policy information for libraries.

Namespace

Drupal\csp

Code

protected function getLibrarySources($extension, $name) {
  $cid = implode(':', [
    'csp',
    'libraries',
    $extension,
  ]);
  if (!isset($this->librarySourcesCache[$extension])) {
    $cacheItem = $this->cache
      ->get($cid);
    if ($cacheItem) {
      $this->librarySourcesCache[$extension] = $cacheItem->data;
    }
  }
  if (isset($this->librarySourcesCache[$extension][$name])) {
    return $this->librarySourcesCache[$extension][$name];
  }
  $libraryInfo = $this->libraryDiscovery
    ->getLibraryByName($extension, $name);
  $sources = [];
  foreach ($libraryInfo['js'] as $jsInfo) {
    if ($jsInfo['type'] == 'external') {
      $host = self::getHostFromUri($jsInfo['data']);
      $sources['script-src'][] = $host;
      $sources['script-src-elem'][] = $host;
    }
  }
  foreach ($libraryInfo['css'] as $cssInfo) {
    if ($cssInfo['type'] == 'external') {
      $host = self::getHostFromUri($cssInfo['data']);
      $sources['style-src'][] = $host;
      $sources['style-src-elem'][] = $host;
    }
  }
  $this->librarySourcesCache[$extension][$name] = $sources;
  $this->cache
    ->set($cid, $this->librarySourcesCache[$extension], Cache::PERMANENT, [
    'library_info',
  ]);
  return $this->librarySourcesCache[$extension][$name];
}