Dumps an (optimized) asset to persistent storage.

Parameters

string $data: The asset's contents.

string $file_extension: The file extension of this asset.

string $uri: The URI to dump to.

Return value

string An URI to access the dumped asset.

Overrides AssetDumperUriInterface::dumpToUri

1 call to AssetDumper::dumpToUri()
AssetDumper::dump in core/lib/Drupal/Core/Asset/AssetDumper.php
The file name for the CSS or JS cache file is generated from the hash of the aggregated contents of the files in $data. This forces proxies and browsers to download new CSS when the CSS changes.

File

core/lib/Drupal/Core/Asset/AssetDumper.php, line 51

Class

AssetDumper
Dumps a CSS or JavaScript asset.

Namespace

Drupal\Core\Asset

Code

public function dumpToUri(string $data, string $file_extension, string $uri) : string {
  $path = 'assets://' . $file_extension;

  // Create the CSS or JS file.
  $this->fileSystem
    ->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY);
  try {
    if (!file_exists($uri) && !$this->fileSystem
      ->saveData($data, $uri, FileExists::Replace)) {
      return FALSE;
    }
  } catch (FileException $e) {
    return FALSE;
  }

  // If CSS/JS gzip compression is enabled and the zlib extension is available
  // then create a gzipped version of this file. This file is served
  // conditionally to browsers that accept gzip using .htaccess rules.
  // It's possible that the rewrite rules in .htaccess aren't working on this
  // server, but there's no harm (other than the time spent generating the
  // file) in generating the file anyway. Sites on servers where rewrite rules
  // aren't working can set css.gzip to FALSE in order to skip
  // generating a file that won't be used.
  if (extension_loaded('zlib') && \Drupal::config('system.performance')
    ->get($file_extension . '.gzip')) {
    try {
      if (!file_exists($uri . '.gz') && !$this->fileSystem
        ->saveData(gzencode($data, 9, FORCE_GZIP), $uri . '.gz', FileExists::Replace)) {
        return FALSE;
      }
    } catch (FileException $e) {
      return FALSE;
    }
  }
  return $uri;
}