TranslateCss.php in Advanced CSS/JS Aggregation 8.4
File
advagg_mod/src/Asset/TranslateCss.php
View source
<?php
namespace Drupal\advagg_mod\Asset;
use Drupal\advagg\Asset\SingleAssetOptimizerBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Psr\Log\LoggerInterface;
class TranslateCss extends SingleAssetOptimizerBase {
use StringTranslationTrait;
public function __construct(LoggerInterface $logger, ConfigFactoryInterface $config_factory) {
parent::__construct($logger);
$this->config = $config_factory
->get('advagg_mod.settings');
}
public function optimize($contents, array $asset, array $data) {
$double_quot = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"';
$single_quot = "'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'";
$pattern = "/content:.*?({$double_quot}|{$single_quot}|(?:\\;|\\})).*?(?:\\;|\\})/";
return preg_replace_callback($pattern, [
$this,
'translateCallback',
], $contents);
}
protected function translateCallback(array $matches) {
if ($matches[1] === ';' || $matches[1] === '}') {
return $matches[0];
}
$before = substr($matches[1], 1, -1);
if (!preg_match('/[A-Za-z]/', $before)) {
return $matches[0];
}
$css_unicode_pattern = '/\\\\[0-9a-fA-F]{1,6}(?:\\r\\n|[ \\t\\r\\n\\f])?/';
$unicode_removed = preg_replace($css_unicode_pattern, '', $before);
if (empty($unicode_removed)) {
return $matches[0];
}
return str_replace($before, (string) $this
->t('@before', [
'@before' => $before,
]), $matches[0]);
}
}