Mercurial > hg > cmmr2012-drupal-site
comparison core/lib/Drupal/Core/Asset/AssetDumper.php @ 5:12f9dff5fda9 tip
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:34:47 +0100 |
parents | c75dbcec494b |
children |
comparison
equal
deleted
inserted
replaced
4:a9cd425dd02b | 5:12f9dff5fda9 |
---|---|
1 <?php | 1 <?php |
2 | 2 |
3 namespace Drupal\Core\Asset; | 3 namespace Drupal\Core\Asset; |
4 | 4 |
5 use Drupal\Component\Utility\Crypt; | 5 use Drupal\Component\Utility\Crypt; |
6 use Drupal\Core\File\Exception\FileException; | |
7 use Drupal\Core\File\FileSystemInterface; | |
6 | 8 |
7 /** | 9 /** |
8 * Dumps a CSS or JavaScript asset. | 10 * Dumps a CSS or JavaScript asset. |
9 */ | 11 */ |
10 class AssetDumper implements AssetDumperInterface { | 12 class AssetDumper implements AssetDumperInterface { |
13 | |
14 /** | |
15 * The file system service. | |
16 * | |
17 * @var \Drupal\Core\File\FileSystemInterface | |
18 */ | |
19 protected $fileSystem; | |
20 | |
21 /** | |
22 * AssetDumper constructor. | |
23 * | |
24 * @param \Drupal\Core\File\FileSystemInterface $file_system | |
25 * The file handler. | |
26 */ | |
27 public function __construct(FileSystemInterface $file_system = NULL) { | |
28 $this->fileSystem = $file_system; | |
29 } | |
11 | 30 |
12 /** | 31 /** |
13 * {@inheritdoc} | 32 * {@inheritdoc} |
14 * | 33 * |
15 * The file name for the CSS or JS cache file is generated from the hash of | 34 * The file name for the CSS or JS cache file is generated from the hash of |
22 $filename = $file_extension . '_' . Crypt::hashBase64($data) . '.' . $file_extension; | 41 $filename = $file_extension . '_' . Crypt::hashBase64($data) . '.' . $file_extension; |
23 // Create the css/ or js/ path within the files folder. | 42 // Create the css/ or js/ path within the files folder. |
24 $path = 'public://' . $file_extension; | 43 $path = 'public://' . $file_extension; |
25 $uri = $path . '/' . $filename; | 44 $uri = $path . '/' . $filename; |
26 // Create the CSS or JS file. | 45 // Create the CSS or JS file. |
27 file_prepare_directory($path, FILE_CREATE_DIRECTORY); | 46 $this->getFileSystem()->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY); |
28 if (!file_exists($uri) && !file_unmanaged_save_data($data, $uri, FILE_EXISTS_REPLACE)) { | 47 try { |
48 if (!file_exists($uri) && !$this->getFileSystem()->saveData($data, $uri, FileSystemInterface::EXISTS_REPLACE)) { | |
49 return FALSE; | |
50 } | |
51 } | |
52 catch (FileException $e) { | |
29 return FALSE; | 53 return FALSE; |
30 } | 54 } |
31 // If CSS/JS gzip compression is enabled and the zlib extension is available | 55 // 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 | 56 // then create a gzipped version of this file. This file is served |
33 // conditionally to browsers that accept gzip using .htaccess rules. | 57 // conditionally to browsers that accept gzip using .htaccess rules. |
35 // server, but there's no harm (other than the time spent generating the | 59 // 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 | 60 // 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 | 61 // aren't working can set css.gzip to FALSE in order to skip |
38 // generating a file that won't be used. | 62 // generating a file that won't be used. |
39 if (extension_loaded('zlib') && \Drupal::config('system.performance')->get($file_extension . '.gzip')) { | 63 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)) { | 64 try { |
65 if (!file_exists($uri . '.gz') && !$this->getFileSystem()->saveData(gzencode($data, 9, FORCE_GZIP), $uri . '.gz', FILE_EXISTS_REPLACE)) { | |
66 return FALSE; | |
67 } | |
68 } | |
69 catch (FileException $e) { | |
41 return FALSE; | 70 return FALSE; |
42 } | 71 } |
43 } | 72 } |
44 return $uri; | 73 return $uri; |
45 } | 74 } |
46 | 75 |
76 /** | |
77 * Helper method for returning the file system service. | |
78 * | |
79 * @return \Drupal\Core\File\FileSystemInterface | |
80 * The file system service. | |
81 */ | |
82 private function getFileSystem() { | |
83 if (!$this->fileSystem) { | |
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); | |
85 $this->fileSystem = \Drupal::service('file_system'); | |
86 } | |
87 return $this->fileSystem; | |
88 } | |
89 | |
47 } | 90 } |