File

src/Asset/AssetResolver.php, line 82

Class

AssetResolver
The default asset resolver.

Namespace

Drupal\advagg\Asset

Code

public function getCssAssets(AttachedAssetsInterface $assets, $optimize) {
    $theme_info = $this->themeManager
        ->getActiveTheme();
    // Add the theme name to the cache key since themes may implement
    // hook_css_alter().
    $libraries_to_load = $this->getLibrariesToLoad($assets);
    $cid = 'css:' . $theme_info->getName() . ':' . Crypt::hashBase64(serialize($libraries_to_load)) . (int) $optimize;
    if ($cached = $this->cache
        ->get($cid)) {
        return $cached->data;
    }
    $css = [];
    $default_options = [
        'type' => 'file',
        'group' => CSS_AGGREGATE_DEFAULT,
        'weight' => 0,
        'media' => 'all',
        'preprocess' => TRUE,
        'browsers' => [],
    ];
    foreach ($libraries_to_load as $library) {
        list($extension, $name) = explode('/', $library, 2);
        $definition = $this->libraryDiscovery
            ->getLibraryByName($extension, $name);
        if (isset($definition['css'])) {
            foreach ($definition['css'] as $options) {
                $options += $default_options;
                $options['browsers'] += [
                    'IE' => TRUE,
                    '!IE' => TRUE,
                ];
                // Files with a query string cannot be preprocessed.
                if ($options['type'] === 'file' && $options['preprocess'] && strpos($options['data'], '?') !== FALSE) {
                    $options['preprocess'] = FALSE;
                }
                // Always add a tiny value to the weight, to conserve the insertion
                // order.
                $options['weight'] += count($css) / 1000;
                // CSS files are being keyed by the full path.
                $css[$options['data']] = $options;
            }
        }
    }
    // Allow modules and themes to alter the CSS assets.
    $this->moduleHandler
        ->alter('css', $css, $assets);
    $this->themeManager
        ->alter('css', $css, $assets);
    // After alter get file information (in case alter changes things).
    $this->advaggFiles
        ->getMultiple(array_column($css, 'data'));
    // Sort CSS items, so that they appear in the correct order.
    uasort($css, 'static::sort');
    // Allow themes to remove CSS files by CSS files full path and file name.
    // @todo Remove in Drupal 9.0.x.
    if ($stylesheet_remove = $theme_info->getStyleSheetsRemove()) {
        foreach ($css as $key => $options) {
            if (isset($stylesheet_remove[$key])) {
                unset($css[$key]);
            }
        }
    }
    if ($optimize) {
        $css = $this->cssCollectionOptimizer
            ->optimize($css);
    }
    $this->cache
        ->set($cid, $css, CacheBackendInterface::CACHE_PERMANENT, [
        'library_info',
    ]);
    return $css;
}