You are here

class S3fsCssOptimizer in S3 File System 8.3

Same name and namespace in other branches
  1. 4.0.x src/Asset/S3fsCssOptimizer.php \Drupal\s3fs\Asset\S3fsCssOptimizer

Optimizes a CSS asset.

Hierarchy

Expanded class hierarchy of S3fsCssOptimizer

2 files declare their use of S3fsCssOptimizer
S3fsAdvAggSubscriber.php in src/EventSubscriber/S3fsAdvAggSubscriber.php
S3fsCssOptimizerTest.php in tests/src/Unit/S3fsCssOptimizerTest.php

File

src/Asset/S3fsCssOptimizer.php, line 11

Namespace

Drupal\s3fs\Asset
View source
class S3fsCssOptimizer extends CssOptimizer {

  /**
   * Drupal ConfigFactory Service.
   *
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * Constructor for S3fs CSS rewriter.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   Drupal service config.factory.
   */
  public function __construct(ConfigFactoryInterface $configFactory) {
    $this->configFactory = $configFactory;
  }

  /**
   * Return absolute urls to access static files that aren't in S3 bucket.
   *
   * @param array $matches
   *   An array of matches by a preg_replace_callback() call that scans for
   *   url() references in CSS files, except for external or absolute ones.
   *
   * @return string
   *   The file path.
   */

  // phpcs:disable
  public function rewriteFileURI($matches) {

    // phpcs:enable
    $alwaysSecure = !empty($this->configFactory
      ->get('s3fs.settings')
      ->get('use_https'));
    $useCssjsHost = !empty($this->configFactory
      ->get('s3fs.settings')
      ->get('use_cssjs_host'));
    $cssjsHost = $this->configFactory
      ->get('s3fs.settings')
      ->get('cssjs_host');

    // Prefix with base and remove '../' segments where possible.
    $path = $this->rewriteFileURIBasePath . $matches[1];
    $last = '';
    while ($path != $last) {
      $last = $path;
      $path = preg_replace('`(^|/)(?!\\.\\./)([^/]+)/\\.\\./`', '$1', $path);
    }
    $url = file_create_url($path);
    if ($useCssjsHost && !empty($cssjsHost)) {
      global $base_url;
      $pattern = '#' . $base_url . '#';
      $url = preg_replace($pattern, $cssjsHost, $url);
    }

    // Always use https:// links.
    if ($alwaysSecure) {
      $url = preg_replace('#^http?:#', 'https:', $url);
    }
    else {

      // Strip protocol for protocol independent hyperlinks.
      $url = preg_replace('#^http?:#', '', $url);
    }
    return 'url(' . $url . ')';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CssOptimizer::$rewriteFileURIBasePath public property The base path used by rewriteFileURI().
CssOptimizer::clean public function Processes the contents of a CSS asset for cleanup. Overrides AssetOptimizerInterface::clean
CssOptimizer::loadFile public function Loads the stylesheet and resolves all @import commands.
CssOptimizer::loadNestedFile protected function Loads stylesheets recursively and returns contents with corrected paths.
CssOptimizer::optimize public function Optimizes an asset. Overrides AssetOptimizerInterface::optimize
CssOptimizer::processCss protected function Processes the contents of a stylesheet for aggregation.
CssOptimizer::processFile protected function Build aggregate CSS file.
S3fsCssOptimizer::$configFactory protected property Drupal ConfigFactory Service.
S3fsCssOptimizer::rewriteFileURI public function Prefixes all paths within a CSS file for processFile(). Overrides CssOptimizer::rewriteFileURI
S3fsCssOptimizer::__construct public function Constructor for S3fs CSS rewriter.