Chris@0: grouper = $grouper; Chris@0: $this->optimizer = $optimizer; Chris@0: $this->dumper = $dumper; Chris@0: $this->state = $state; Chris@18: if (!$file_system) { Chris@18: @trigger_error('The file_system service must be passed to CssCollectionOptimizer::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/3006851.', E_USER_DEPRECATED); Chris@18: $file_system = \Drupal::service('file_system'); Chris@18: } Chris@18: $this->fileSystem = $file_system; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: * Chris@0: * The cache file name is retrieved on a page load via a lookup variable that Chris@0: * contains an associative array. The array key is the hash of the file names Chris@0: * in $css while the value is the cache file name. The cache file is generated Chris@0: * in two cases. First, if there is no file name value for the key, which will Chris@0: * happen if a new file name has been added to $css or after the lookup Chris@0: * variable is emptied to force a rebuild of the cache. Second, the cache file Chris@0: * is generated if it is missing on disk. Old cache files are not deleted Chris@0: * immediately when the lookup variable is emptied, but are deleted after a Chris@0: * configurable period (@code system.performance.stale_file_threshold @endcode) Chris@0: * to ensure that files referenced by a cached page will still be available. Chris@0: */ Chris@0: public function optimize(array $css_assets) { Chris@0: // Group the assets. Chris@0: $css_groups = $this->grouper->group($css_assets); Chris@0: Chris@0: // Now optimize (concatenate + minify) and dump each asset group, unless Chris@0: // that was already done, in which case it should appear in Chris@0: // drupal_css_cache_files. Chris@0: // Drupal contrib can override this default CSS aggregator to keep the same Chris@0: // grouping, optimizing and dumping, but change the strategy that is used to Chris@0: // determine when the aggregate should be rebuilt (e.g. mtime, HTTPS …). Chris@0: $map = $this->state->get('drupal_css_cache_files') ?: []; Chris@0: $css_assets = []; Chris@0: foreach ($css_groups as $order => $css_group) { Chris@0: // We have to return a single asset, not a group of assets. It is now up Chris@0: // to one of the pieces of code in the switch statement below to set the Chris@0: // 'data' property to the appropriate value. Chris@0: $css_assets[$order] = $css_group; Chris@0: unset($css_assets[$order]['items']); Chris@0: Chris@0: switch ($css_group['type']) { Chris@0: case 'file': Chris@0: // No preprocessing, single CSS asset: just use the existing URI. Chris@0: if (!$css_group['preprocess']) { Chris@0: $uri = $css_group['items'][0]['data']; Chris@0: $css_assets[$order]['data'] = $uri; Chris@0: } Chris@0: // Preprocess (aggregate), unless the aggregate file already exists. Chris@0: else { Chris@0: $key = $this->generateHash($css_group); Chris@0: $uri = ''; Chris@0: if (isset($map[$key])) { Chris@0: $uri = $map[$key]; Chris@0: } Chris@0: if (empty($uri) || !file_exists($uri)) { Chris@0: // Optimize each asset within the group. Chris@0: $data = ''; Chris@0: foreach ($css_group['items'] as $css_asset) { Chris@0: $data .= $this->optimizer->optimize($css_asset); Chris@0: } Chris@0: // Per the W3C specification at Chris@0: // http://www.w3.org/TR/REC-CSS2/cascade.html#at-import, @import Chris@0: // rules must precede any other style, so we move those to the Chris@0: // top. Chris@0: $regexp = '/@import[^;]+;/i'; Chris@0: preg_match_all($regexp, $data, $matches); Chris@0: $data = preg_replace($regexp, '', $data); Chris@0: $data = implode('', $matches[0]) . $data; Chris@0: // Dump the optimized CSS for this group into an aggregate file. Chris@0: $uri = $this->dumper->dump($data, 'css'); Chris@0: // Set the URI for this group's aggregate file. Chris@0: $css_assets[$order]['data'] = $uri; Chris@0: // Persist the URI for this aggregate file. Chris@0: $map[$key] = $uri; Chris@0: $this->state->set('drupal_css_cache_files', $map); Chris@0: } Chris@0: else { Chris@0: // Use the persisted URI for the optimized CSS file. Chris@0: $css_assets[$order]['data'] = $uri; Chris@0: } Chris@0: $css_assets[$order]['preprocessed'] = TRUE; Chris@0: } Chris@0: break; Chris@0: Chris@0: case 'external': Chris@0: // We don't do any aggregation and hence also no caching for external Chris@0: // CSS assets. Chris@0: $uri = $css_group['items'][0]['data']; Chris@0: $css_assets[$order]['data'] = $uri; Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: return $css_assets; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Generate a hash for a given group of CSS assets. Chris@0: * Chris@0: * @param array $css_group Chris@0: * A group of CSS assets. Chris@0: * Chris@0: * @return string Chris@0: * A hash to uniquely identify the given group of CSS assets. Chris@0: */ Chris@0: protected function generateHash(array $css_group) { Chris@0: $css_data = []; Chris@0: foreach ($css_group['items'] as $css_file) { Chris@0: $css_data[] = $css_file['data']; Chris@0: } Chris@0: return hash('sha256', serialize($css_data)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getAll() { Chris@0: return $this->state->get('drupal_css_cache_files'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function deleteAll() { Chris@0: $this->state->delete('drupal_css_cache_files'); Chris@0: Chris@0: $delete_stale = function ($uri) { Chris@0: // Default stale file threshold is 30 days. Chris@0: if (REQUEST_TIME - filemtime($uri) > \Drupal::config('system.performance')->get('stale_file_threshold')) { Chris@18: $this->fileSystem->delete($uri); Chris@0: } Chris@0: }; Chris@0: file_scan_directory('public://css', '/.*/', ['callback' => $delete_stale]); Chris@0: } Chris@0: Chris@0: }