3 namespace Drupal\Core\Asset;
5 use Drupal\Component\Utility\Crypt;
8 * Dumps a CSS or JavaScript asset.
10 class AssetDumper implements AssetDumperInterface {
15 * The file name for the CSS or JS cache file is generated from the hash of
16 * the aggregated contents of the files in $data. This forces proxies and
17 * browsers to download new CSS when the CSS changes.
19 public function dump($data, $file_extension) {
20 // Prefix filename to prevent blocking by firewalls which reject files
21 // starting with "ad*".
22 $filename = $file_extension . '_' . Crypt::hashBase64($data) . '.' . $file_extension;
23 // Create the css/ or js/ path within the files folder.
24 $path = 'public://' . $file_extension;
25 $uri = $path . '/' . $filename;
26 // Create the CSS or JS file.
27 file_prepare_directory($path, FILE_CREATE_DIRECTORY);
28 if (!file_exists($uri) && !file_unmanaged_save_data($data, $uri, FILE_EXISTS_REPLACE)) {
31 // If CSS/JS gzip compression is enabled and the zlib extension is available
32 // then create a gzipped version of this file. This file is served
33 // conditionally to browsers that accept gzip using .htaccess rules.
34 // It's possible that the rewrite rules in .htaccess aren't working on this
35 // server, but there's no harm (other than the time spent generating the
36 // file) in generating the file anyway. Sites on servers where rewrite rules
37 // aren't working can set css.gzip to FALSE in order to skip
38 // generating a file that won't be used.
39 if (extension_loaded('zlib') && \Drupal::config('system.performance')->get($file_extension . '.gzip')) {
40 if (!file_exists($uri . '.gz') && !file_unmanaged_save_data(gzencode($data, 9, FORCE_GZIP), $uri . '.gz', FILE_EXISTS_REPLACE)) {