You are here

protected function SingleAssetOptimizerBase::isMinificationSuccess in Advanced CSS/JS Aggregation 8.4

Same name and namespace in other branches
  1. 8.3 src/Asset/SingleAssetOptimizerBase.php \Drupal\advagg\Asset\SingleAssetOptimizerBase::isMinificationSuccess()

Check if minification was successful before saving changes.

Parameters

string $minified: The minified asset contents.

string $original: The original asset contents to compare against.

Return value

bool TRUE for success.

2 calls to SingleAssetOptimizerBase::isMinificationSuccess()
CssMinifier::optimize in advagg_css_minify/src/Asset/CssMinifier.php
Optimize the asset's content.
JsMinifier::optimize in advagg_js_minify/src/Asset/JsMinifier.php
Optimize the asset's content.

File

src/Asset/SingleAssetOptimizerBase.php, line 115

Class

SingleAssetOptimizerBase
A base class for optimizing (especially minifying) assets.

Namespace

Drupal\advagg\Asset

Code

protected function isMinificationSuccess($minified, $original) {

  // If the minified data is zero length it was a failure.
  if (empty($minified)) {
    return FALSE;
  }

  // If set, make sure the minified version doesn't have a suspiciously high
  // ratio or conversely a really low ratio.
  if (!$this->config
    ->get('ratio_max')) {
    return TRUE;
  }
  $before = strlen($original);
  $after = strlen($minified);
  $ratio = !empty($before) ? ($before - $after) / $before : 0;
  if ($ratio > $this->config
    ->get('ratio_max') || $ratio < $this->config
    ->get('ratio_min')) {
    return FALSE;
  }
  return TRUE;
}