Chris@0: fileSystem = $file_system; Chris@18: } Chris@18: Chris@18: /** Chris@0: * {@inheritdoc} Chris@0: * Chris@0: * The file name for the CSS or JS cache file is generated from the hash of Chris@0: * the aggregated contents of the files in $data. This forces proxies and Chris@0: * browsers to download new CSS when the CSS changes. Chris@0: */ Chris@0: public function dump($data, $file_extension) { Chris@0: // Prefix filename to prevent blocking by firewalls which reject files Chris@0: // starting with "ad*". Chris@0: $filename = $file_extension . '_' . Crypt::hashBase64($data) . '.' . $file_extension; Chris@0: // Create the css/ or js/ path within the files folder. Chris@0: $path = 'public://' . $file_extension; Chris@0: $uri = $path . '/' . $filename; Chris@0: // Create the CSS or JS file. Chris@18: $this->getFileSystem()->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY); Chris@18: try { Chris@18: if (!file_exists($uri) && !$this->getFileSystem()->saveData($data, $uri, FileSystemInterface::EXISTS_REPLACE)) { Chris@18: return FALSE; Chris@18: } Chris@18: } Chris@18: catch (FileException $e) { Chris@0: return FALSE; Chris@0: } Chris@0: // If CSS/JS gzip compression is enabled and the zlib extension is available Chris@0: // then create a gzipped version of this file. This file is served Chris@0: // conditionally to browsers that accept gzip using .htaccess rules. Chris@0: // It's possible that the rewrite rules in .htaccess aren't working on this Chris@0: // server, but there's no harm (other than the time spent generating the Chris@0: // file) in generating the file anyway. Sites on servers where rewrite rules Chris@0: // aren't working can set css.gzip to FALSE in order to skip Chris@0: // generating a file that won't be used. Chris@0: if (extension_loaded('zlib') && \Drupal::config('system.performance')->get($file_extension . '.gzip')) { Chris@18: try { Chris@18: if (!file_exists($uri . '.gz') && !$this->getFileSystem()->saveData(gzencode($data, 9, FORCE_GZIP), $uri . '.gz', FILE_EXISTS_REPLACE)) { Chris@18: return FALSE; Chris@18: } Chris@18: } Chris@18: catch (FileException $e) { Chris@0: return FALSE; Chris@0: } Chris@0: } Chris@0: return $uri; Chris@0: } Chris@0: Chris@18: /** Chris@18: * Helper method for returning the file system service. Chris@18: * Chris@18: * @return \Drupal\Core\File\FileSystemInterface Chris@18: * The file system service. Chris@18: */ Chris@18: private function getFileSystem() { Chris@18: if (!$this->fileSystem) { Chris@18: @trigger_error('\Drupal\Core\File\FileSystemInterface is a dependency of this class in Drupal 8.7.0 and will be required before Drupal 9.0.0. See https://www.drupal.org/node/3006851.', E_USER_DEPRECATED); Chris@18: $this->fileSystem = \Drupal::service('file_system'); Chris@18: } Chris@18: return $this->fileSystem; Chris@18: } Chris@18: Chris@0: }