3 namespace Drupal\Core\Asset;
5 use Drupal\Core\State\StateInterface;
8 * Optimizes CSS assets.
10 class CssCollectionOptimizer implements AssetCollectionOptimizerInterface {
13 * A CSS asset grouper.
15 * @var \Drupal\Core\Asset\CssCollectionGrouper
20 * A CSS asset optimizer.
22 * @var \Drupal\Core\Asset\CssOptimizer
29 * @var \Drupal\Core\Asset\AssetDumper
34 * The state key/value store.
36 * @var \Drupal\Core\State\StateInterface
41 * Constructs a CssCollectionOptimizer.
43 * @param \Drupal\Core\Asset\AssetCollectionGrouperInterface $grouper
44 * The grouper for CSS assets.
45 * @param \Drupal\Core\Asset\AssetOptimizerInterface $optimizer
46 * The optimizer for a single CSS asset.
47 * @param \Drupal\Core\Asset\AssetDumperInterface $dumper
48 * The dumper for optimized CSS assets.
49 * @param \Drupal\Core\State\StateInterface $state
50 * The state key/value store.
52 public function __construct(AssetCollectionGrouperInterface $grouper, AssetOptimizerInterface $optimizer, AssetDumperInterface $dumper, StateInterface $state) {
53 $this->grouper = $grouper;
54 $this->optimizer = $optimizer;
55 $this->dumper = $dumper;
56 $this->state = $state;
62 * The cache file name is retrieved on a page load via a lookup variable that
63 * contains an associative array. The array key is the hash of the file names
64 * in $css while the value is the cache file name. The cache file is generated
65 * in two cases. First, if there is no file name value for the key, which will
66 * happen if a new file name has been added to $css or after the lookup
67 * variable is emptied to force a rebuild of the cache. Second, the cache file
68 * is generated if it is missing on disk. Old cache files are not deleted
69 * immediately when the lookup variable is emptied, but are deleted after a
70 * configurable period (@code system.performance.stale_file_threshold @endcode)
71 * to ensure that files referenced by a cached page will still be available.
73 public function optimize(array $css_assets) {
75 $css_groups = $this->grouper->group($css_assets);
77 // Now optimize (concatenate + minify) and dump each asset group, unless
78 // that was already done, in which case it should appear in
79 // drupal_css_cache_files.
80 // Drupal contrib can override this default CSS aggregator to keep the same
81 // grouping, optimizing and dumping, but change the strategy that is used to
82 // determine when the aggregate should be rebuilt (e.g. mtime, HTTPS …).
83 $map = $this->state->get('drupal_css_cache_files') ?: [];
85 foreach ($css_groups as $order => $css_group) {
86 // We have to return a single asset, not a group of assets. It is now up
87 // to one of the pieces of code in the switch statement below to set the
88 // 'data' property to the appropriate value.
89 $css_assets[$order] = $css_group;
90 unset($css_assets[$order]['items']);
92 switch ($css_group['type']) {
94 // No preprocessing, single CSS asset: just use the existing URI.
95 if (!$css_group['preprocess']) {
96 $uri = $css_group['items'][0]['data'];
97 $css_assets[$order]['data'] = $uri;
99 // Preprocess (aggregate), unless the aggregate file already exists.
101 $key = $this->generateHash($css_group);
103 if (isset($map[$key])) {
106 if (empty($uri) || !file_exists($uri)) {
107 // Optimize each asset within the group.
109 foreach ($css_group['items'] as $css_asset) {
110 $data .= $this->optimizer->optimize($css_asset);
112 // Per the W3C specification at
113 // http://www.w3.org/TR/REC-CSS2/cascade.html#at-import, @import
114 // rules must precede any other style, so we move those to the
116 $regexp = '/@import[^;]+;/i';
117 preg_match_all($regexp, $data, $matches);
118 $data = preg_replace($regexp, '', $data);
119 $data = implode('', $matches[0]) . $data;
120 // Dump the optimized CSS for this group into an aggregate file.
121 $uri = $this->dumper->dump($data, 'css');
122 // Set the URI for this group's aggregate file.
123 $css_assets[$order]['data'] = $uri;
124 // Persist the URI for this aggregate file.
126 $this->state->set('drupal_css_cache_files', $map);
129 // Use the persisted URI for the optimized CSS file.
130 $css_assets[$order]['data'] = $uri;
132 $css_assets[$order]['preprocessed'] = TRUE;
137 // We don't do any aggregation and hence also no caching for external
139 $uri = $css_group['items'][0]['data'];
140 $css_assets[$order]['data'] = $uri;
149 * Generate a hash for a given group of CSS assets.
151 * @param array $css_group
152 * A group of CSS assets.
155 * A hash to uniquely identify the given group of CSS assets.
157 protected function generateHash(array $css_group) {
159 foreach ($css_group['items'] as $css_file) {
160 $css_data[] = $css_file['data'];
162 return hash('sha256', serialize($css_data));
168 public function getAll() {
169 return $this->state->get('drupal_css_cache_files');
175 public function deleteAll() {
176 $this->state->delete('drupal_css_cache_files');
178 $delete_stale = function ($uri) {
179 // Default stale file threshold is 30 days.
180 if (REQUEST_TIME - filemtime($uri) > \Drupal::config('system.performance')->get('stale_file_threshold')) {
181 file_unmanaged_delete($uri);
184 file_scan_directory('public://css', '/.*/', ['callback' => $delete_stale]);