You are here

class CssOptimizer in CDN 8.3

Decorates CSS asset optimizer: ensures file URLs are rewritten to the CDN.

Hierarchy

Expanded class hierarchy of CssOptimizer

See also

cdn_file_url_alter()

https://www.drupal.org/node/2745109

1 string reference to 'CssOptimizer'
cdn.services.yml in ./cdn.services.yml
cdn.services.yml
1 service uses CssOptimizer
cdn.asset.css.optimizer in ./cdn.services.yml
Drupal\cdn\Asset\CssOptimizer

File

src/Asset/CssOptimizer.php, line 13

Namespace

Drupal\cdn\Asset
View source
class CssOptimizer implements AssetOptimizerInterface {

  /**
   * The decorated CSS asset optimizer service.
   *
   * @var \Drupal\Core\Asset\AssetOptimizerInterface
   */
  protected $decoratedCssOptimizer;

  /**
   * @param \Drupal\Core\Asset\AssetOptimizerInterface $decorated_css_optimizer
   *   The decorated CSS asset optimizer service.
   */
  public function __construct(AssetOptimizerInterface $decorated_css_optimizer) {
    $this->decoratedCssOptimizer = $decorated_css_optimizer;
  }

  /**
   * {@inheritdoc}
   */
  public function optimize(array $css_asset) {
    return $this
      ->runWithoutCdnFileAlteration(function () use ($css_asset) {
      return $this->decoratedCssOptimizer
        ->optimize($css_asset);
    });
  }

  /**
   * {@inheritdoc}
   */
  public function clean($contents) {
    return $this
      ->runWithoutCdnFileAlteration(function () use ($contents) {
      return $this->decoratedCssOptimizer
        ->clean($contents);
    });
  }

  /**
   * Wraps callable in an environment where the global $cdn_in_css_file===FALSE.
   *
   * @param callable $callable
   *   A callable.
   *
   * @return mixed
   *   The result of the callable.
   */
  protected function runWithoutCdnFileAlteration(callable $callable) {
    global $_cdn_in_css_file;
    $_cdn_in_css_file = TRUE;
    $result = $callable();
    $_cdn_in_css_file = FALSE;
    return $result;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CssOptimizer::$decoratedCssOptimizer protected property The decorated CSS asset optimizer service.
CssOptimizer::clean public function Removes unwanted content from an asset. Overrides AssetOptimizerInterface::clean
CssOptimizer::optimize public function Optimizes an asset. Overrides AssetOptimizerInterface::optimize
CssOptimizer::runWithoutCdnFileAlteration protected function Wraps callable in an environment where the global $cdn_in_css_file===FALSE.
CssOptimizer::__construct public function