Same name and namespace in other branches
  1. 6.0.x src/Asset/AssetOptimizer.php \Drupal\advagg\Asset\AssetOptimizer::writeFile() 1 comment
  2. 8.x-3.x src/Asset/AssetOptimizer.php \Drupal\advagg\Asset\AssetOptimizer::writeFile() 1 comment
  3. 8.x-4.x src/Asset/AssetOptimizer.php \Drupal\advagg\Asset\AssetOptimizer::writeFile() 1 comment

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 280

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.
    $this->fileSystem
        ->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY);
    if (!file_exists($uri)) {
        if (!$this->fileSystem
            ->saveData($data, $uri, FileSystemInterface::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')) {
        $this->fileSystem
            ->saveData(gzencode($data, 9, FORCE_GZIP), $uri . '.gz', FileSystemInterface::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')) {
        $this->fileSystem
            ->saveData(brotli_compress($data, 11, BROTLI_TEXT), $uri . '.br', FileSystemInterface::EXISTS_REPLACE);
    }
    return $uri;
}