Same name and namespace in other branches
  1. 5.0.x advagg_mod/src/Asset/TranslateCss.php \Drupal\advagg_mod\Asset\TranslateCss 1 commentaire
  2. 8.x-3.x advagg_mod/src/Asset/TranslateCss.php \Drupal\advagg_mod\Asset\TranslateCss 1 commentaire
  3. 8.x-4.x advagg_mod/src/Asset/TranslateCss.php \Drupal\advagg_mod\Asset\TranslateCss 1 commentaire

Applies the t() function to strings in CSS assets.

Hierarchy

  • class \Drupal\advagg\Asset\SingleAssetOptimizerBase
    • class \Drupal\advagg_mod\Asset\TranslateCss extends \Drupal\advagg\Asset\SingleAssetOptimizerBase uses \Drupal\Core\StringTranslation\StringTranslationTrait

Expanded class hierarchy of TranslateCss

1 file declares its use of TranslateCss
InitSubscriber.php dans advagg_mod/src/EventSubscriber/InitSubscriber.php
2 string references to 'TranslateCss'
advagg_mod.services.yml dans advagg_mod/advagg_mod.services.yml
advagg_mod/advagg_mod.services.yml
InitSubscriber::getSubscribedEvents dans advagg_mod/src/EventSubscriber/InitSubscriber.php
1 service uses TranslateCss
advagg_mod.translate_css dans advagg_mod/advagg_mod.services.yml
Drupal\advagg_mod\Asset\TranslateCss

Fichier

advagg_mod/src/Asset/TranslateCss.php, line 14

Namespace

Drupal\advagg_mod\Asset
View source
class TranslateCss extends SingleAssetOptimizerBase {
    use StringTranslationTrait;
    
    /**
     * Construct the optimizer instance.
     *
     * @param \Psr\Log\LoggerInterface $logger
     *   The logger service.
     * @param \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator
     *   The file URL generator.
     * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
     *   A config factory for retrieving required config objects.
     */
    public function __construct(LoggerInterface $logger, FileUrlGeneratorInterface $file_url_generator, ConfigFactoryInterface $config_factory) {
        parent::__construct($logger, $file_url_generator);
        $this->config = $config_factory->get('advagg_mod.settings');
    }
    
    /**
     * {@inheritdoc}
     */
    public function optimize($contents, array $asset, array $data) {
        // Code taken from \Drupal\Core\Asset\CssOptimizer::processCss().
        // Regexp to match double quoted strings.
        $double_quot = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"';
        // Regexp to match single quoted strings.
        $single_quot = "'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'";
        // Extract all content inside of quotes.
        $pattern = "/content:.*?({$double_quot}|{$single_quot}|(?:\\;|\\})).*?(?:\\;|\\})/";
        // Run strings inside of quotes of the content attribute through the t
        // function.
        return preg_replace_callback($pattern, [
            $this,
            'translateCallback',
        ], $contents);
    }
    
    /**
     * Run preg matches through the t() function.
     *
     * @param array $matches
     *   Array of matches from preg_replace_callback().
     *
     * @return string
     *   Replaced string.
     */
    protected function translateCallback(array $matches) {
        // Skip if equal to ; or }.
        if ($matches[1] === ';' || $matches[1] === '}') {
            return $matches[0];
        }
        // Remove quotes for t function.
        $before = substr($matches[1], 1, -1);
        // Only run if it contains A-Za-z.
        if (!preg_match('/[A-Za-z]/', $before)) {
            return $matches[0];
        }
        // Only run if it contains characters other than unicode.
        $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];
        }
        // Run t function and put back into string.
        return str_replace($before, (string) $this->t('@before', [
            '@before' => $before,
        ]), $matches[0]);
    }

}

Members

Titre Trier par ordre décroissant Modifiers Object type Résumé Overriden Title
SingleAssetOptimizerBase::$config protected property A config object for optimizer.
SingleAssetOptimizerBase::$fileUrlGenerator protected property The file URL generator.
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::stringContainsMultibyteCharacters protected function Checks if string contains multibyte characters.
TranslateCss::optimize public function Optimize the asset's content. Overrides SingleAssetOptimizerBase::optimize
TranslateCss::translateCallback protected function Run preg matches through the t() function.
TranslateCss::__construct public function Construct the optimizer instance. Overrides SingleAssetOptimizerBase::__construct