You are here

protected function AssetOptimizer::writeFile in Advanced CSS/JS Aggregation 8.3

Same name and namespace in other branches
  1. 8.4 src/Asset/AssetOptimizer.php \Drupal\advagg\Asset\AssetOptimizer::writeFile()

The filename for the CSS or JS optimized file is the cid.

The CID is generated from the hashed original filename.

Parameters

string $data: The content to output.

string $cid: The unique segment of the filename.

Return value

bool|string FALSE or the saved filename.

2 calls to AssetOptimizer::writeFile()
CssOptimizer::optimizeFile in src/Asset/CssOptimizer.php
Perform any in-place optimization & pass to event for further optimization.
JsOptimizer::optimizeFile in src/Asset/JsOptimizer.php
Perform any in-place optimization & pass to event for further optimization.

File

src/Asset/AssetOptimizer.php, line 256

Class

AssetOptimizer
Defines the base AdvAgg optimizer.

Namespace

Drupal\advagg\Asset

Code

protected function writeFile($data, $cid) {

  // Prefix filename to prevent blocking by firewalls which reject files
  // starting with "ad*".
  // Create the css/ or js/ path within the files folder.
  $path = 'public://' . $this->extension . '/optimized';
  $version = Crypt::hashBase64($data);
  $uri = "{$path}/{$this->extension}_{$cid}.{$version}.{$this->extension}";

  // Create the CSS or JS file.
  file_prepare_directory($path, FILE_CREATE_DIRECTORY);
  if (!file_exists($uri)) {
    if (!file_unmanaged_save_data($data, $uri, FILE_EXISTS_REPLACE)) {
      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.
  if ($this->gZip && !file_exists($uri . '.gz')) {
    file_unmanaged_save_data(gzencode($data, 9, FORCE_GZIP), $uri . '.gz', FILE_EXISTS_REPLACE);
  }

  // If brotli compression is enabled and available, create br compressed
  // files and serve conditionally via .htaccess rules.
  if ($this->brotli && !file_exists($uri . '.br')) {
    file_unmanaged_save_data(brotli_compress($data, 11, BROTLI_TEXT), $uri . '.br', FILE_EXISTS_REPLACE);
  }
  return $uri;
}