annotate core/lib/Drupal/Core/Asset/AssetDumper.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
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@18 6 use Drupal\Core\File\Exception\FileException;
Chris@18 7 use Drupal\Core\File\FileSystemInterface;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Dumps a CSS or JavaScript asset.
Chris@0 11 */
Chris@0 12 class AssetDumper implements AssetDumperInterface {
Chris@0 13
Chris@0 14 /**
Chris@18 15 * The file system service.
Chris@18 16 *
Chris@18 17 * @var \Drupal\Core\File\FileSystemInterface
Chris@18 18 */
Chris@18 19 protected $fileSystem;
Chris@18 20
Chris@18 21 /**
Chris@18 22 * AssetDumper constructor.
Chris@18 23 *
Chris@18 24 * @param \Drupal\Core\File\FileSystemInterface $file_system
Chris@18 25 * The file handler.
Chris@18 26 */
Chris@18 27 public function __construct(FileSystemInterface $file_system = NULL) {
Chris@18 28 $this->fileSystem = $file_system;
Chris@18 29 }
Chris@18 30
Chris@18 31 /**
Chris@0 32 * {@inheritdoc}
Chris@0 33 *
Chris@0 34 * The file name for the CSS or JS cache file is generated from the hash of
Chris@0 35 * the aggregated contents of the files in $data. This forces proxies and
Chris@0 36 * browsers to download new CSS when the CSS changes.
Chris@0 37 */
Chris@0 38 public function dump($data, $file_extension) {
Chris@0 39 // Prefix filename to prevent blocking by firewalls which reject files
Chris@0 40 // starting with "ad*".
Chris@0 41 $filename = $file_extension . '_' . Crypt::hashBase64($data) . '.' . $file_extension;
Chris@0 42 // Create the css/ or js/ path within the files folder.
Chris@0 43 $path = 'public://' . $file_extension;
Chris@0 44 $uri = $path . '/' . $filename;
Chris@0 45 // Create the CSS or JS file.
Chris@18 46 $this->getFileSystem()->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY);
Chris@18 47 try {
Chris@18 48 if (!file_exists($uri) && !$this->getFileSystem()->saveData($data, $uri, FileSystemInterface::EXISTS_REPLACE)) {
Chris@18 49 return FALSE;
Chris@18 50 }
Chris@18 51 }
Chris@18 52 catch (FileException $e) {
Chris@0 53 return FALSE;
Chris@0 54 }
Chris@0 55 // If CSS/JS gzip compression is enabled and the zlib extension is available
Chris@0 56 // then create a gzipped version of this file. This file is served
Chris@0 57 // conditionally to browsers that accept gzip using .htaccess rules.
Chris@0 58 // It's possible that the rewrite rules in .htaccess aren't working on this
Chris@0 59 // server, but there's no harm (other than the time spent generating the
Chris@0 60 // file) in generating the file anyway. Sites on servers where rewrite rules
Chris@0 61 // aren't working can set css.gzip to FALSE in order to skip
Chris@0 62 // generating a file that won't be used.
Chris@0 63 if (extension_loaded('zlib') && \Drupal::config('system.performance')->get($file_extension . '.gzip')) {
Chris@18 64 try {
Chris@18 65 if (!file_exists($uri . '.gz') && !$this->getFileSystem()->saveData(gzencode($data, 9, FORCE_GZIP), $uri . '.gz', FILE_EXISTS_REPLACE)) {
Chris@18 66 return FALSE;
Chris@18 67 }
Chris@18 68 }
Chris@18 69 catch (FileException $e) {
Chris@0 70 return FALSE;
Chris@0 71 }
Chris@0 72 }
Chris@0 73 return $uri;
Chris@0 74 }
Chris@0 75
Chris@18 76 /**
Chris@18 77 * Helper method for returning the file system service.
Chris@18 78 *
Chris@18 79 * @return \Drupal\Core\File\FileSystemInterface
Chris@18 80 * The file system service.
Chris@18 81 */
Chris@18 82 private function getFileSystem() {
Chris@18 83 if (!$this->fileSystem) {
Chris@18 84 @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 85 $this->fileSystem = \Drupal::service('file_system');
Chris@18 86 }
Chris@18 87 return $this->fileSystem;
Chris@18 88 }
Chris@18 89
Chris@0 90 }