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 JsCollectionOptimizer::__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 names in Chris@0: * $files 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 $files 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 $js_assets) { Chris@0: // Group the assets. Chris@0: $js_groups = $this->grouper->group($js_assets); Chris@0: Chris@0: // Now optimize (concatenate, not minify) and dump each asset group, unless Chris@0: // that was already done, in which case it should appear in Chris@0: // system.js_cache_files. Chris@0: // Drupal contrib can override this default JS 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('system.js_cache_files') ?: []; Chris@0: $js_assets = []; Chris@0: foreach ($js_groups as $order => $js_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: $js_assets[$order] = $js_group; Chris@0: unset($js_assets[$order]['items']); Chris@0: Chris@0: switch ($js_group['type']) { Chris@0: case 'file': Chris@0: // No preprocessing, single JS asset: just use the existing URI. Chris@0: if (!$js_group['preprocess']) { Chris@0: $uri = $js_group['items'][0]['data']; Chris@0: $js_assets[$order]['data'] = $uri; Chris@0: } Chris@0: // Preprocess (aggregate), unless the aggregate file already exists. Chris@0: else { Chris@0: $key = $this->generateHash($js_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: // Concatenate each asset within the group. Chris@0: $data = ''; Chris@0: foreach ($js_group['items'] as $js_asset) { Chris@0: // Optimize this JS file, but only if it's not yet minified. Chris@0: if (isset($js_asset['minified']) && $js_asset['minified']) { Chris@0: $data .= file_get_contents($js_asset['data']); Chris@0: } Chris@0: else { Chris@0: $data .= $this->optimizer->optimize($js_asset); Chris@0: } Chris@0: // Append a ';' and a newline after each JS file to prevent them Chris@0: // from running together. Chris@0: $data .= ";\n"; Chris@0: } Chris@0: // Remove unwanted JS code that cause issues. Chris@0: $data = $this->optimizer->clean($data); Chris@0: // Dump the optimized JS for this group into an aggregate file. Chris@0: $uri = $this->dumper->dump($data, 'js'); Chris@0: // Set the URI for this group's aggregate file. Chris@0: $js_assets[$order]['data'] = $uri; Chris@0: // Persist the URI for this aggregate file. Chris@0: $map[$key] = $uri; Chris@0: $this->state->set('system.js_cache_files', $map); Chris@0: } Chris@0: else { Chris@0: // Use the persisted URI for the optimized JS file. Chris@0: $js_assets[$order]['data'] = $uri; Chris@0: } Chris@0: $js_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: // JS assets. Chris@0: $uri = $js_group['items'][0]['data']; Chris@0: $js_assets[$order]['data'] = $uri; Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: return $js_assets; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Generate a hash for a given group of JavaScript assets. Chris@0: * Chris@0: * @param array $js_group Chris@0: * A group of JavaScript assets. Chris@0: * Chris@0: * @return string Chris@0: * A hash to uniquely identify the given group of JavaScript assets. Chris@0: */ Chris@0: protected function generateHash(array $js_group) { Chris@0: $js_data = []; Chris@0: foreach ($js_group['items'] as $js_file) { Chris@0: $js_data[] = $js_file['data']; Chris@0: } Chris@0: return hash('sha256', serialize($js_data)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getAll() { Chris@0: return $this->state->get('system.js_cache_files'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function deleteAll() { Chris@0: $this->state->delete('system.js_cache_files'); 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://js', '/.*/', ['callback' => $delete_stale]); Chris@0: } Chris@0: Chris@0: }