You are here

class MinificationSubscriber in Advanced CSS/JS Aggregation 8.3

Same name in this branch
  1. 8.3 advagg_ext_minify/src/EventSubscriber/MinificationSubscriber.php \Drupal\advagg_ext_minify\EventSubscriber\MinificationSubscriber
  2. 8.3 advagg_css_minify/src/EventSubscriber/MinificationSubscriber.php \Drupal\advagg_css_minify\EventSubscriber\MinificationSubscriber
  3. 8.3 advagg_js_minify/src/EventSubscriber/MinificationSubscriber.php \Drupal\advagg_js_minify\EventSubscriber\MinificationSubscriber
  4. 8.3 advagg_bundler/src/EventSubscriber/MinificationSubscriber.php \Drupal\advagg_js_minify\EventSubscriber\MinificationSubscriber
Same name and namespace in other branches
  1. 8.4 advagg_ext_minify/src/EventSubscriber/MinificationSubscriber.php \Drupal\advagg_ext_minify\EventSubscriber\MinificationSubscriber

Subscribe to asset optimization events and minify assets.

Hierarchy

  • class \Drupal\advagg_ext_minify\EventSubscriber\MinificationSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of MinificationSubscriber

1 string reference to 'MinificationSubscriber'
advagg_ext_minify.services.yml in advagg_ext_minify/advagg_ext_minify.services.yml
advagg_ext_minify/advagg_ext_minify.services.yml
1 service uses MinificationSubscriber
advagg.ext_subscriber in advagg_ext_minify/advagg_ext_minify.services.yml
Drupal\advagg_ext_minify\EventSubscriber\MinificationSubscriber

File

advagg_ext_minify/src/EventSubscriber/MinificationSubscriber.php, line 13

Namespace

Drupal\advagg_ext_minify\EventSubscriber
View source
class MinificationSubscriber implements EventSubscriberInterface {

  /**
   * The minifier.
   *
   * @var \Drupal\advagg\Asset\SingleAssetOptimizerBase
   */
  protected $minifier;

  /**
   * The minifier configuration.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $config;

  /**
   * Construct the optimizer instance.
   *
   * @param \Drupal\advagg\Asset\SingleAssetOptimizerBase $minifier
   *   The minifier.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(SingleAssetOptimizerBase $minifier, ConfigFactoryInterface $config_factory) {
    $this->minifier = $minifier;
    $this->config = $config_factory
      ->get('advagg_ext_minify.settings');
  }

  /**
   * Pass the minification over to the minification service.
   *
   * @param \Drupal\advagg\Asset\AssetOptimizationEvent $asset
   *   The asset optimization event.
   */
  public function minifyCss(AssetOptimizationEvent $asset) {
    if (!$this
      ->isEnabled('css')) {
      return;
    }
    $content = $asset
      ->getContent();
    $content = $this->minifier
      ->css($content, $asset
      ->getAsset(), $asset
      ->getData());
    $asset
      ->setContent($content);
  }

  /**
   * Pass the minification over to the minification service.
   *
   * @param \Drupal\advagg\Asset\AssetOptimizationEvent $asset
   *   The asset optimization event.
   */
  public function minifyJs(AssetOptimizationEvent $asset) {
    if (!$this
      ->isEnabled('js')) {
      return;
    }
    $content = $asset
      ->getContent();
    $content = $this->minifier
      ->js($content, $asset
      ->getAsset(), $asset
      ->getData());
    $asset
      ->setContent($content);
  }

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

  /**
   * Check if the external minifier is enabled & configured for this asset type.
   *
   * @param string $type
   *   The asset type.
   *
   * @return bool
   *   Whether to run the external minifier or not.
   */
  protected function isEnabled($type) {
    if (!$this->config
      ->get("{$type}_enabled")) {
      return FALSE;
    }
    if (!$this->config
      ->get("{$type}_cmd")) {
      return FALSE;
    }
    return TRUE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MinificationSubscriber::$config protected property The minifier configuration.
MinificationSubscriber::$minifier protected property The minifier.
MinificationSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
MinificationSubscriber::isEnabled protected function Check if the external minifier is enabled & configured for this asset type.
MinificationSubscriber::minifyCss public function Pass the minification over to the minification service.
MinificationSubscriber::minifyJs public function Pass the minification over to the minification service.
MinificationSubscriber::__construct public function Construct the optimizer instance.