You are here

public function S3fsCssOptimizer::rewriteFileURI in S3 File System 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Asset/S3fsCssOptimizer.php \Drupal\s3fs\Asset\S3fsCssOptimizer::rewriteFileURI()

Prefixes all paths within a CSS file for processFile().

Note: the only reason this method is public is so color.module can call it; it is not on the AssetOptimizerInterface, so future refactorings can make it protected.

Parameters

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 value

string The file path.

Overrides CssOptimizer::rewriteFileURI

File

src/Asset/S3fsCssOptimizer.php, line 41

Class

S3fsCssOptimizer
Optimizes a CSS asset.

Namespace

Drupal\s3fs\Asset

Code

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 . ')';
}