function CssCollectionOptimizerLazy::optimize

Same name in other branches
  1. 11.x core/lib/Drupal/Core/Asset/CssCollectionOptimizerLazy.php \Drupal\Core\Asset\CssCollectionOptimizerLazy::optimize()

Overrides AssetCollectionOptimizerInterface::optimize

File

core/lib/Drupal/Core/Asset/CssCollectionOptimizerLazy.php, line 61

Class

CssCollectionOptimizerLazy
Optimizes CSS assets.

Namespace

Drupal\Core\Asset

Code

public function optimize(array $css_assets, array $libraries) {
    // File names are generated based on library/asset definitions. This
    // includes a hash of the assets and the group index. Additionally, the full
    // set of libraries, already loaded libraries and theme are sent as query
    // parameters to allow a PHP controller to generate a valid file with
    // sufficient information. Files are not generated by this method since
    // they're assumed to be successfully returned from the URL created whether
    // on disk or not.
    // Group the assets.
    $css_groups = $this->grouper
        ->group($css_assets);
    $css_assets = [];
    foreach ($css_groups as $order => $css_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.
        $css_assets[$order] = $css_group;
        if ($css_group['type'] === 'file') {
            // No preprocessing, single CSS asset: just use the existing URI.
            if (!$css_group['preprocess']) {
                $uri = $css_group['items'][0]['data'];
                $css_assets[$order]['data'] = $uri;
            }
            else {
                // To reproduce the full context of assets outside of the request,
                // we must know the entire set of libraries used to generate all CSS
                // groups, whether or not files in a group are from a particular
                // library or not.
                $css_assets[$order]['preprocessed'] = TRUE;
            }
        }
        if ($css_group['type'] === 'external') {
            // We don't do any aggregation and hence also no caching for external
            // CSS assets.
            $uri = $css_group['items'][0]['data'];
            $css_assets[$order]['data'] = $uri;
        }
    }
    // All asset group URLs will have exactly the same query arguments, except
    // for the delta, so prepare them in advance.
    $query_args = [
        'language' => $this->languageManager
            ->getCurrentLanguage()
            ->getId(),
        'theme' => $this->themeManager
            ->getActiveTheme()
            ->getName(),
        'include' => UrlHelper::compressQueryParameter(implode(',', $this->dependencyResolver
            ->getMinimalRepresentativeSubset($libraries))),
    ];
    $ajax_page_state = $this->requestStack
        ->getCurrentRequest()
        ->get('ajax_page_state');
    $already_loaded = isset($ajax_page_state) ? explode(',', $ajax_page_state['libraries']) : [];
    if ($already_loaded) {
        $query_args['exclude'] = UrlHelper::compressQueryParameter(implode(',', $this->dependencyResolver
            ->getMinimalRepresentativeSubset($already_loaded)));
    }
    // Generate a URL for each group of assets, but do not process them inline,
    // this is done using optimizeGroup() when the asset path is requested.
    foreach ($css_assets as $order => $css_asset) {
        if (!empty($css_asset['preprocessed'])) {
            $query = [
                'delta' => "{$order}",
            ] + $query_args;
            $filename = 'css_' . $this->generateHash($css_asset) . '.css';
            $uri = 'assets://css/' . $filename;
            $css_assets[$order]['data'] = $this->fileUrlGenerator
                ->generateString($uri) . '?' . UrlHelper::buildQuery($query);
        }
        unset($css_assets[$order]['items']);
    }
    return $css_assets;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.