You are here

public function CssMinifier::optimize in Advanced CSS/JS Aggregation 8.3

Same name and namespace in other branches
  1. 8.4 advagg_css_minify/src/Asset/CssMinifier.php \Drupal\advagg_css_minify\Asset\CssMinifier::optimize()

Optimize the asset's content.

Parameters

string $content: The content to optimize.

array $asset: A core asset array.

array $data: The cache data.

Return value

string The optimized content.

Overrides SingleAssetOptimizerBase::optimize

1 call to CssMinifier::optimize()
CssMinifier::loadFile in advagg_css_minify/src/Asset/CssMinifier.php
Loads the stylesheet and resolves all @import commands.

File

advagg_css_minify/src/Asset/CssMinifier.php, line 31

Class

CssMinifier
Optimizes a JavaScript asset.

Namespace

Drupal\advagg_css_minify\Asset

Code

public function optimize($contents, array $asset, array $data) {

  // Do nothing if css file minification is disabled.
  if (!($minifier = $this->config
    ->get('minifier'))) {
    return $contents;
  }

  // Do not re-minify if the file is already minified.
  if ($this
    ->isMinified($contents)) {
    return $contents;
  }
  $contents = $this
    ->clean($contents, $asset);
  $contents_original = $contents;

  // Do nothing if core minification is selected.
  if ($minifier === 1) {
    $contents = trim($this
      ->minifyCore($contents));
  }
  else {
    $contents = trim($this
      ->minifyCssMin($contents));
  }

  // If the contents are not empty, ensure that $data ends with ; or }.
  if (trim($contents) !== "" && strpbrk(substr(trim($contents), -1), ';})') === FALSE) {
    $contents .= ';';
  }
  if (!$this
    ->isMinificationSuccess($contents, $contents_original)) {
    return $contents_original;
  }
  return $contents;
}