You are here

class S3fsAdvAggSubscriber in S3 File System 8.3

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

Subscribe to asset optimization events and update assets urls.

Hierarchy

  • class \Drupal\s3fs\EventSubscriber\S3fsAdvAggSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of S3fsAdvAggSubscriber

File

src/EventSubscriber/S3fsAdvAggSubscriber.php, line 13

Namespace

Drupal\s3fs\EventSubscriber
View source
class S3fsAdvAggSubscriber implements EventSubscriberInterface {

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

  /**
   * Base path to use for URI rewrite.
   *
   * @var string
   */
  protected $rewriteFileURIBasePath;

  /**
   * Construct the optimizer instance.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The optimizer.
   */
  public function __construct(ConfigFactoryInterface $configFactory) {
    $this->configFactory = $configFactory;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      AssetOptimizationEvent::CSS => [
        'updateUrls',
        0,
      ],
    ];
  }

  /**
   * Update asset urls to access static files that they aren't in S3 bucket.
   *
   * @param \Drupal\advagg\Asset\AssetOptimizationEvent $asset
   *   The asset optimization event.
   */
  public function updateUrls(AssetOptimizationEvent $asset) {
    $content = $this
      ->processAssetContent($asset);
    $asset
      ->setContent($content);
  }

  /**
   * Process asset content for make urls compatible.
   *
   * @param \Drupal\advagg\Asset\AssetOptimizationEvent $asset
   *   Asset to be processed.
   *
   * @return mixed
   *   preg_replace_callback() formated return.
   *
   * @see \Drupal\Core\Asset\CssOptimizer::processFile()
   */
  public function processAssetContent(AssetOptimizationEvent $asset) {
    $content = $asset
      ->getContent();
    $css_asset = $asset
      ->getAsset();

    // Get the parent directory of this file, relative to the Drupal root.
    $css_base_path = substr($css_asset['data'], 0, strrpos($css_asset['data'], '/'));

    // Store base path.
    $this->rewriteFileURIBasePath = $css_base_path . '/';

    // Restore asset urls.
    $content = str_replace('/' . $this->rewriteFileURIBasePath, "", $content);
    return preg_replace_callback('/url\\(\\s*[\'"]?(?![a-z]+:|\\/+)([^\'")]+)[\'"]?\\s*\\)/i', [
      $this,
      'rewriteFileURI',
    ], $content);
  }

  /**
   * 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
    $reWriter = new S3fsCssOptimizer($this->configFactory);
    $reWriter->rewriteFileURIBasePath = $this->rewriteFileURIBasePath;
    return $reWriter
      ->rewriteFileURI($matches);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
S3fsAdvAggSubscriber::$configFactory protected property Drupal ConfigFactory Service.
S3fsAdvAggSubscriber::$rewriteFileURIBasePath protected property Base path to use for URI rewrite.
S3fsAdvAggSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
S3fsAdvAggSubscriber::processAssetContent public function Process asset content for make urls compatible.
S3fsAdvAggSubscriber::rewriteFileURI public function
S3fsAdvAggSubscriber::updateUrls public function Update asset urls to access static files that they aren't in S3 bucket.
S3fsAdvAggSubscriber::__construct public function Construct the optimizer instance.