File

src/Asset/JsCollectionOptimizer.php, line 80

Class

JsCollectionOptimizer

Namespace

Drupal\advagg\Asset

Code

public function optimize(array $js_assets) {
    // Group the assets.
    $js_groups = $this->grouper
        ->group($js_assets);
    // Now optimize (concatenate, not minify) and dump each asset group, unless
    // that was already done, in which case it should appear in
    // system.js_cache_files.
    // Drupal contrib can override this default JS aggregator to keep the same
    // grouping, optimizing and dumping, but change the strategy that is used to
    // determine when the aggregate should be rebuilt (e.g. mtime, HTTPS …).
    $js_assets = [];
    $protocol_relative = $this->config
        ->get('path.convert.absolute_to_protocol_relative');
    $force_https = $this->config
        ->get('path.convert.force_https');
    foreach ($js_groups as $order => $js_group) {
        // We have to return a single asset, not a group of assets. It is now up
        // to one of the pieces of code in the switch statement below to set the
        // 'data' property to the appropriate value.
        $js_assets[$order] = $js_group;
        unset($js_assets[$order]['items']);
        switch ($js_group['type']) {
            case 'file':
                // No preprocessing, single JS asset: just use the existing URI.
                if (!$js_group['preprocess']) {
                    $js_assets[$order]['data'] = $js_group['items'][0]['data'];
                }
                else {
                    $key = $this->generateHash($js_group) . $this->settingsHash;
                    $uri = '';
                    if ($aggregate = $this->advaggAggregates
                        ->get($key)) {
                        $uri = $aggregate['uri'];
                    }
                    if (empty($uri) || !file_exists($uri)) {
                        // Concatenate each asset within the group.
                        $data = '';
                        foreach ($js_group['items'] as $js_asset) {
                            // Optimize this JS file, but only if it's not yet minified.
                            if (isset($js_asset['minified']) && $js_asset['minified']) {
                                $content = file_get_contents($js_asset['data']);
                            }
                            else {
                                $content = $this->optimizer
                                    ->optimize($js_asset);
                            }
                            // Allow other modules to modify this file's contents.
                            // Call hook_advagg_js_contents_alter().
                            $this->moduleHandler
                                ->alter('advagg_js_contents', $content, $js_asset);
                            $data .= $content;
                            // Append a ';' and a newline after each JS file to prevent them
                            // from running together.
                            $data .= ";\n";
                        }
                        // Remove unwanted JS code that cause issues.
                        $data = $this->optimizer
                            ->clean($data);
                        // Dump the optimized JS for this group into an aggregate file.
                        list($uri, $filename) = $this->dumper
                            ->dump($data, 'js');
                        // Set the URI for this group's aggregate file.
                        $js_assets[$order]['data'] = $uri;
                        // Persist the URI for this aggregate file.
                        $aggregate_info = [
                            'uri' => $uri,
                            'contents' => $js_group['items'],
                            'hooks_hash' => advagg_current_hooks_hash_array(),
                            'uid' => $filename,
                        ];
                        $this->advaggAggregates
                            ->set($key, $aggregate_info);
                    }
                    else {
                        // Use the persisted URI for the optimized JS file.
                        $js_assets[$order]['data'] = $uri;
                    }
                    $js_assets[$order]['preprocessed'] = TRUE;
                }
                break;
            case 'external':
                // We don't do any aggregation and hence also no caching for external
                // JS assets.
                $uri = $js_group['items'][0]['data'];
                if ($force_https) {
                    $uri = advagg_path_convert_force_https($uri);
                }
                elseif ($protocol_relative) {
                    $uri = advagg_path_convert_protocol_relative($uri);
                }
                $js_assets[$order]['data'] = $uri;
                break;
        }
    }
    return $js_assets;
}