You are here

class ConfigSubscriber in HTTP/2 Server Push 8

Invalidates cache tags & rebuilds container when necessary.

Hierarchy

  • class \Drupal\http2_server_push\EventSubscriber\ConfigSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of ConfigSubscriber

1 string reference to 'ConfigSubscriber'
http2_server_push.services.yml in ./http2_server_push.services.yml
http2_server_push.services.yml
1 service uses ConfigSubscriber
http2_server_push.config_subscriber in ./http2_server_push.services.yml
Drupal\http2_server_push\EventSubscriber\ConfigSubscriber

File

src/EventSubscriber/ConfigSubscriber.php, line 15

Namespace

Drupal\http2_server_push\EventSubscriber
View source
class ConfigSubscriber implements EventSubscriberInterface {

  /**
   * The cache tags invalidator.
   *
   * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
   */
  protected $cacheTagsInvalidator;

  /**
   * The Drupal kernel.
   *
   * @var \Drupal\Core\DrupalKernelInterface
   */
  protected $drupalKernel;

  /**
   * The config installer.
   *
   * @var \Drupal\Core\Config\ConfigInstallerInterface
   */
  protected $configInstaller;

  /**
   * Constructs a ConfigSubscriber object.
   *
   * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator
   *   The cache tags invalidator.
   * @param \Drupal\Core\DrupalKernelInterface $drupal_kernel
   *   The Drupal kernel.
   * @param \Drupal\Core\Config\ConfigInstallerInterface $config_installer
   *   The config installer.
   */
  public function __construct(CacheTagsInvalidatorInterface $cache_tags_invalidator, DrupalKernelInterface $drupal_kernel, ConfigInstallerInterface $config_installer) {
    $this->cacheTagsInvalidator = $cache_tags_invalidator;
    $this->drupalKernel = $drupal_kernel;
    $this->configInstaller = $config_installer;
  }

  /**
   * Invalidates all render caches when CSS/JS aggregation gets toggled.
   *
   * @param \Drupal\Core\Config\ConfigCrudEvent $event
   *   The Event to process.
   */
  public function onSave(ConfigCrudEvent $event) {
    if ($this->configInstaller
      ->isSyncing()) {
      return;
    }
    if ($event
      ->getConfig()
      ->getName() !== 'system.performance') {
      return;
    }
    if (!$event
      ->isChanged('css.preprocess') && !$event
      ->isChanged('js.preprocess')) {
      return;
    }
    $this->cacheTagsInvalidator
      ->invalidateTags([
      // Rendered output that is cached. (HTML containing Link headers.)
      'rendered',
    ]);

    // Rebuild the container whenever CSS/JS aggregation gets toggled.
    // @see \Drupal\http2_server_push\Http2ServerPushServiceProvider
    $this->drupalKernel
      ->invalidateContainer();
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[ConfigEvents::SAVE][] = [
      'onSave',
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigSubscriber::$cacheTagsInvalidator protected property The cache tags invalidator.
ConfigSubscriber::$configInstaller protected property The config installer.
ConfigSubscriber::$drupalKernel protected property The Drupal kernel.
ConfigSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
ConfigSubscriber::onSave public function Invalidates all render caches when CSS/JS aggregation gets toggled.
ConfigSubscriber::__construct public function Constructs a ConfigSubscriber object.