3 namespace Drupal\advagg\Asset;
5 use Drupal\Component\Utility\Crypt;
6 use Drupal\Core\Asset\AssetDumperInterface;
7 use Drupal\Core\Config\ConfigFactoryInterface;
8 use Drupal\Core\Extension\ModuleHandlerInterface;
11 * Dumps a CSS or JavaScript asset.
13 class AssetDumper implements AssetDumperInterface {
16 * The path to use for saving the asset.
23 * The extension to use.
30 * Hash of the AdvAgg settings.
37 * A config object for the system performance configuration.
39 * @var \Drupal\Core\Config\Config
44 * Module handler service.
46 * @var \Drupal\Core\Extension\ModuleHandlerInterface
48 protected $moduleHandler;
51 * Construct the AssetDumper instance.
53 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
54 * A config factory for retrieving required config objects.
55 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
58 public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
59 $this->config = $config_factory->get('system.performance');
60 $this->hash = '_' . advagg_get_current_hooks_hash() . '.';
61 $this->moduleHandler = $module_handler;
65 * Set the folder path to save the data to.
67 * @param string $file_extension
68 * The extension of the file.
70 public function preparePath($file_extension) {
71 $this->extension = $file_extension;
72 $this->path = 'public://' . $this->extension . '/';
74 // Allow other modules to alter the file path.
75 // Call hook_advagg_asset_path_alter().
76 $this->moduleHandler->alter('advagg_asset_path', $this->path, $this->extension);
77 file_prepare_directory($this->path, FILE_CREATE_DIRECTORY);
84 * The file name for the CSS or JS cache file is generated from the hash of
85 * the aggregated contents of the files in $data. This forces proxies and
86 * browsers to download new CSS when the CSS changes.
88 public function dump($data, $file_extension) {
89 if (!isset($this->extension) || $this->extension != $file_extension) {
90 $this->preparePath($file_extension);
93 // Prefix filename to prevent blocking by firewalls which reject files
94 // starting with "ad*".
95 $filename = $this->extension . '_' . Crypt::hashBase64($data) . $this->hash . $this->extension;
96 $uri = $this->path . $filename;
98 // Create the CSS or JS file.
99 if (!file_exists($uri) && !file_unmanaged_save_data($data, $uri, FILE_EXISTS_REPLACE)) {
103 // If CSS/JS gzip compression is enabled and the zlib extension is available
104 // then create a gzipped version of this file. This file is served
105 // conditionally to browsers that accept gzip using .htaccess rules.
106 // It's possible that the rewrite rules in .htaccess aren't working on this
107 // server, but there's no harm (other than the time spent generating the
108 // file) in generating the file anyway. Sites on servers where rewrite rules
109 // aren't working can set css.gzip to FALSE in order to skip
110 // generating a file that won't be used.
111 if (extension_loaded('zlib') && $this->config->get($file_extension . '.gzip')) {
112 if (!file_exists($uri . '.gz') && !file_unmanaged_save_data(gzencode($data, 9, FORCE_GZIP), $uri . '.gz', FILE_EXISTS_REPLACE)) {
116 return [$uri, $filename];