You are here

class VendorStreamWrapperAssetOptimizer in Vendor Stream Wrapper 8

Decorates the CSS and JS optimization services.

The optimization services loads the CSS and JS files based on the provided path, however, the path that is generated is virtual/non-existing (which prevents the whole vendor folder being publicly available). Therefore, we need to decorate the optimization service, used when aggregating CSS and JS files, to set the correct file path for the CSS and JS assets in the vendor folder.

Hierarchy

Expanded class hierarchy of VendorStreamWrapperAssetOptimizer

1 string reference to 'VendorStreamWrapperAssetOptimizer'
vendor_stream_wrapper.services.yml in ./vendor_stream_wrapper.services.yml
vendor_stream_wrapper.services.yml
2 services use VendorStreamWrapperAssetOptimizer
vendor_stream_wrapper.asset.css.optimizer_decorator in ./vendor_stream_wrapper.services.yml
Drupal\vendor_stream_wrapper\Asset\VendorStreamWrapperAssetOptimizer
vendor_stream_wrapper.asset.js.optimizer_decorator in ./vendor_stream_wrapper.services.yml
Drupal\vendor_stream_wrapper\Asset\VendorStreamWrapperAssetOptimizer

File

src/Asset/VendorStreamWrapperAssetOptimizer.php, line 18

Namespace

Drupal\vendor_stream_wrapper\Asset
View source
class VendorStreamWrapperAssetOptimizer implements AssetOptimizerInterface {

  /**
   * The original asset optimization service instance that is being decorated.
   *
   * @var \Drupal\Core\Asset\AssetOptimizerInterface
   */
  protected $innerService;

  /**
   * Creates a new VendorStreamWrapperCssOptimizer instance.
   *
   * @param \Drupal\Core\Asset\AssetOptimizerInterface $inner_service
   *   The original asset optimization service instance that is being decorated.
   */
  public function __construct(AssetOptimizerInterface $inner_service) {
    $this->innerService = $inner_service;
  }

  /**
   * {@inheritdoc}
   */
  public function optimize(array $asset) {

    // Translate the virtual 'vendor_files' paths into the correct path for the
    // vendor directory, so that the actual file can loaded by the optimization
    // service.
    if (!empty($asset['data']) && strpos($asset['data'], 'vendor_files/') === 0) {
      $asset['data'] = str_replace('vendor_files', VendorStreamWrapper::basePath(), $asset['data']);
    }
    return $this->innerService
      ->optimize($asset);
  }

  /**
   * {@inheritdoc}
   */
  public function clean($content) {
    return $this->innerService
      ->clean($content);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
VendorStreamWrapperAssetOptimizer::$innerService protected property The original asset optimization service instance that is being decorated.
VendorStreamWrapperAssetOptimizer::clean public function Removes unwanted content from an asset. Overrides AssetOptimizerInterface::clean
VendorStreamWrapperAssetOptimizer::optimize public function Optimizes an asset. Overrides AssetOptimizerInterface::optimize
VendorStreamWrapperAssetOptimizer::__construct public function Creates a new VendorStreamWrapperCssOptimizer instance.