class CssOptimizer implements AssetOptimizerInterface (View source)

Optimizes a CSS asset.

Properties

string $rewriteFileURIBasePath

The base path used by rewriteFileURI().

protected FileUrlGeneratorInterface $fileUrlGenerator

The file URL generator.

Methods

__construct(FileUrlGeneratorInterface $file_url_generator = NULL)

Constructs a CssOptimizer.

string
optimize(array $css_asset)

Optimizes an asset.

string
clean(string $contents)

Processes the contents of a CSS asset for cleanup.

processFile($css_asset)

Build aggregate CSS file.

Contents
loadFile($file, $optimize = NULL, $reset_basepath = TRUE)

Loads the stylesheet and resolves all @import commands.

The
loadNestedFile(array $matches)

Loads stylesheets recursively and returns contents with corrected paths.

Contents
processCss($contents, $optimize = FALSE)

Processes the contents of a stylesheet for aggregation.

string
rewriteFileURI(array $matches)

Prefixes all paths within a CSS file for processFile().

Details

__construct(FileUrlGeneratorInterface $file_url_generator = NULL)

Constructs a CssOptimizer.

Parameters

FileUrlGeneratorInterface $file_url_generator

The file URL generator.

string optimize(array $css_asset)

Optimizes an asset.

Parameters

array $css_asset

Return Value

string

The optimized asset's contents.

string clean(string $contents)

Processes the contents of a CSS asset for cleanup.

Parameters

string $contents

The contents of the CSS asset.

Return Value

string

The cleaned asset's contents.

protected processFile($css_asset)

Build aggregate CSS file.

Parameters

$css_asset

Contents loadFile($file, $optimize = NULL, $reset_basepath = TRUE)

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 future refactorings can make it protected.

Parameters

$file

Name of the stylesheet to be processed.

$optimize

Defines if CSS contents should be compressed or not.

$reset_basepath

Used internally to facilitate recursive resolution of @import commands.

Return Value

Contents

of the stylesheet, including any resolved @import commands.

protected The loadNestedFile(array $matches)

Loads stylesheets recursively and returns contents with corrected paths.

This function is used for recursive loading of stylesheets and returns the stylesheet content with all url() paths corrected.

Parameters

array $matches

An array of matches by a preg_replace_callback() call that scans for @import-ed CSS files, except for external CSS files.

Return Value

The

contents of the CSS file at $matches[1], with corrected paths.

See also

\Drupal\Core\Asset\AssetOptimizerInterface::loadFile()

protected Contents processCss($contents, $optimize = FALSE)

Processes the contents of a stylesheet for aggregation.

Parameters

$contents

The contents of the stylesheet.

$optimize

(optional) Boolean whether CSS contents should be minified. Defaults to FALSE.

Return Value

Contents

of the stylesheet including the imported stylesheets.

string rewriteFileURI(array $matches)

Prefixes all paths within a CSS file for processFile().

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

Parameters

array $matches

An array of matches by a preg_replace_callback() call that scans for url() references in CSS files, except for external or absolute ones.

Return Value

string

The file path.