function CssOptimizer::loadFile

Same name in other branches
  1. 9 core/lib/Drupal/Core/Asset/CssOptimizer.php \Drupal\Core\Asset\CssOptimizer::loadFile()
  2. 8.9.x core/lib/Drupal/Core/Asset/CssOptimizer.php \Drupal\Core\Asset\CssOptimizer::loadFile()
  3. 10 core/lib/Drupal/Core/Asset/CssOptimizer.php \Drupal\Core\Asset\CssOptimizer::loadFile()

Loads the stylesheet and resolves all @import commands.

Loads a stylesheet and replaces @import commands with the contents of the imported file. Use this instead of file_get_contents when processing stylesheets.

The returned contents are compressed removing white space and comments only when CSS aggregation is enabled. This optimization will not apply for color.module enabled themes with CSS aggregation turned off.

Note: the only reason this method is public is so color.module can call it; it is not on the AssetOptimizerInterface, so any future refactoring can make it protected.

Parameters

$file: Name of the stylesheet to be processed.

$optimize: Defines if CSS contents should be compressed or not.

$reset_base_path: Used internally to facilitate recursive resolution of @import commands.

Return value

string Contents of the stylesheet, including any resolved @import commands.

2 calls to CssOptimizer::loadFile()
CssOptimizer::loadNestedFile in core/lib/Drupal/Core/Asset/CssOptimizer.php
Loads stylesheets recursively and returns contents with corrected paths.
CssOptimizer::processFile in core/lib/Drupal/Core/Asset/CssOptimizer.php
Processes CSS file and adds base URLs to any relative resource paths.

File

core/lib/Drupal/Core/Asset/CssOptimizer.php, line 122

Class

CssOptimizer
Optimizes a CSS asset.

Namespace

Drupal\Core\Asset

Code

public function loadFile($file, $optimize = NULL, $reset_base_path = TRUE) {
    // These statics are not cache variables, so we don't use drupal_static().
    static $_optimize, $base_path;
    if ($reset_base_path) {
        $base_path = '';
    }
    // Store the value of $optimize for preg_replace_callback with nested
    // @import loops.
    if (isset($optimize)) {
        $_optimize = $optimize;
    }
    // Stylesheets are relative one to each other. Start by adding a base path
    // prefix provided by the parent stylesheet (if necessary).
    if ($base_path && !StreamWrapperManager::getScheme($file)) {
        $file = $base_path . '/' . $file;
    }
    // Store the parent base path to restore it later.
    $parent_base_path = $base_path;
    // Set the current base path to process possible child imports.
    $base_path = dirname($file);
    // Load the CSS stylesheet. We suppress errors because themes may specify
    // stylesheets in their .info.yml file that don't exist in the theme's path,
    // but are merely there to disable certain module CSS files.
    $content = '';
    if ($contents = @file_get_contents($file)) {
        // If a BOM is found, convert the file to UTF-8, then use substr() to
        // remove the BOM from the result.
        if ($encoding = Unicode::encodingFromBOM($contents)) {
            $contents = mb_substr(Unicode::convertToUtf8($contents, $encoding), 1);
        }
        elseif (preg_match('/^@charset "([^"]+)";/', $contents, $matches)) {
            if ($matches[1] !== 'utf-8' && $matches[1] !== 'UTF-8') {
                $contents = substr($contents, strlen($matches[0]));
                $contents = Unicode::convertToUtf8($contents, $matches[1]);
            }
        }
        // Return the processed stylesheet.
        $content = $this->processCss($contents, $_optimize);
    }
    // Restore the parent base path as the file and its children are processed.
    $base_path = $parent_base_path;
    return $content;
}

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