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