Same name in other branches
- 6.0.x src/Asset/SingleAssetOptimizerBase.php \Drupal\advagg\Asset\SingleAssetOptimizerBase
- 8.x-3.x src/Asset/SingleAssetOptimizerBase.php \Drupal\advagg\Asset\SingleAssetOptimizerBase
- 8.x-4.x src/Asset/SingleAssetOptimizerBase.php \Drupal\advagg\Asset\SingleAssetOptimizerBase
A base class for optimizing (especially minifying) assets.
Hierarchy
- class \Drupal\advagg\Asset\SingleAssetOptimizerBase
Expanded class hierarchy of SingleAssetOptimizerBase
8 files declare their use of SingleAssetOptimizerBase
- CssMinifier.php dans advagg_css_minify/
src/ Asset/ CssMinifier.php - JsMinifier.php dans advagg_js_minify/
src/ Asset/ JsMinifier.php - MinificationSubscriber.php dans advagg_bundler/
src/ EventSubscriber/ MinificationSubscriber.php - MinificationSubscriber.php dans advagg_css_minify/
src/ EventSubscriber/ MinificationSubscriber.php - MinificationSubscriber.php dans advagg_js_minify/
src/ EventSubscriber/ MinificationSubscriber.php
Fichier
-
src/
Asset/ SingleAssetOptimizerBase.php, line 10
Namespace
Drupal\advagg\AssetView source
abstract class SingleAssetOptimizerBase {
/**
* Logger service.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* A config object for optimizer.
*
* @var \Drupal\Core\Config\Config
*/
protected $config;
/**
* Construct the optimizer.
*
* @param \Psr\Log\LoggerInterface $logger
* A PSR compatible logger.
*/
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
/**
* Optimize the asset's content.
*
* @param string $content
* The content to optimize.
* @param array $asset
* A core asset array.
* @param array $data
* The cache data.
*
* @return string
* The optimized content.
*/
public abstract function optimize($content, array $asset, array $data);
/**
* If configured, add licence string to top/bottom of file.
*
* @param string $contents
* The file contents.
* @param string $path
* The original file path.
*/
public function addLicense(&$contents, $path) {
if ($this->config
->get('add_license')) {
$url = file_create_url($path);
$contents = "/* Source and licensing information for the line(s) below can be found at {$url}. */\n" . $contents . "\n/* Source and licensing information for the above line(s) can be found at {$url}. */";
}
}
/**
* Checks if string contains multibyte characters.
*
* @param string $string
* String to check.
*
* @return bool
* TRUE if string contains multibyte character.
*/
protected function stringContainsMultibyteCharacters($string) {
// Check if there are multi-byte characters: If the UTF-8 encoded string has
// multibyte chars strlen() will return a byte-count greater than the actual
// character count, returned by drupal_strlen().
if (strlen($string) === mb_strlen($string)) {
return FALSE;
}
return TRUE;
}
/**
* Check if the asset is already minified.
*
* @param string $contents
* The asset contents.
*
* @return bool
* TRUE if the asset appears to be already minified.
*/
protected function isMinified($contents) {
// If a lot of semicolons probably already minified.
$semicolon_count = substr_count($contents, ';');
if ($semicolon_count > 10 && $semicolon_count > substr_count($contents, "\n")) {
return TRUE;
}
return FALSE;
}
/**
* Check if minification was successful before saving changes.
*
* @param string $minified
* The minified asset contents.
* @param string $original
* The original asset contents to compare against.
*
* @return bool
* TRUE for success.
*/
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;
}
}
Members
Titre Trier par ordre décroissant | Modifiers | Object type | Résumé | Overrides |
---|---|---|---|---|
SingleAssetOptimizerBase::$config | protected | property | A config object for optimizer. | |
SingleAssetOptimizerBase::$logger | protected | property | Logger service. | |
SingleAssetOptimizerBase::addLicense | public | function | If configured, add licence string to top/bottom of file. | |
SingleAssetOptimizerBase::isMinificationSuccess | protected | function | Check if minification was successful before saving changes. | |
SingleAssetOptimizerBase::isMinified | protected | function | Check if the asset is already minified. | |
SingleAssetOptimizerBase::optimize | abstract public | function | Optimize the asset's content. | 4 |
SingleAssetOptimizerBase::stringContainsMultibyteCharacters | protected | function | Checks if string contains multibyte characters. | |
SingleAssetOptimizerBase::__construct | public | function | Construct the optimizer. | 4 |