You are here

class CompileSubscriber in Theme Compiler 2.0.x

An event subscriber used to trigger (re)compilation of this module's assets.

Copyright (C) 2021 Library Solutions, LLC (et al.).

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

@internal

Hierarchy

  • class \Drupal\theme_compiler\EventSubscriber\CompileSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of CompileSubscriber

1 string reference to 'CompileSubscriber'
theme_compiler.services.yml in ./theme_compiler.services.yml
theme_compiler.services.yml
1 service uses CompileSubscriber
theme_compiler.compile_subscriber in ./theme_compiler.services.yml
\Drupal\theme_compiler\EventSubscriber\CompileSubscriber

File

src/EventSubscriber/CompileSubscriber.php, line 27

Namespace

Drupal\theme_compiler\EventSubscriber
View source
class CompileSubscriber implements EventSubscriberInterface {

  /**
   * The compiler service.
   *
   * @var \Drupal\theme_compiler\Compiler
   */
  protected $compiler;

  /**
   * The theme handler service.
   *
   * @var \Drupal\Core\Extension\ThemeHandlerInterface
   */
  protected $themeHandler;

  /**
   * The typed config manager service.
   *
   * @var \Drupal\Core\Config\TypedConfigManagerInterface
   */
  protected $typedConfigManager;

  /**
   * Constructs an CompileSubscriber object.
   *
   * @param \Drupal\theme_compiler\Compiler $compiler
   *   The compiler service.
   * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
   *   The theme handler service.
   * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config_manager
   *   The typed config manager service.
   */
  public function __construct(Compiler $compiler, ThemeHandlerInterface $theme_handler, TypedConfigManagerInterface $typed_config_manager) {
    $this->compiler = $compiler;
    $this->themeHandler = $theme_handler;
    $this->typedConfigManager = $typed_config_manager;
  }

  /**
   * Run the compilation process for this module.
   */
  public function compile() {
    $this->compiler
      ->compileAssets();
  }

  /**
   * Get a list of configuration dependencies.
   *
   * The settings for any theme which defines a theme compiler asset, or the
   * settings for any themes that depend on such a theme, will be included.
   *
   * @return string[]
   *   A list of configuration dependencies.
   */
  protected function getConfigDependencies() {
    $dependencies = [];
    $themes = [];
    foreach ($this->compiler
      ->getThemeCompilerRouteContexts() as $context) {
      if ($theme = $context
        ->getOption('theme_compiler')['theme'] ?? NULL) {
        $themes[$theme] = $theme;
      }
    }
    $info = $this->themeHandler
      ->listInfo();
    $themes = array_map(function ($theme) use ($info) {
      $result[$theme] = "{$theme}.settings";
      foreach (array_keys($info[$theme]->required_by) as $dependant) {
        $result[$dependant] = "{$dependant}.settings";
      }
      return $result;
    }, $themes);
    foreach ($themes as $dependants) {
      foreach ($dependants as $dependant) {
        $dependencies[$dependant] = $dependant;
      }
    }
    return array_values($dependencies);
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      ConfigEvents::DELETE => 'onConfigChange',
      ConfigEvents::SAVE => 'onConfigChange',
      OnDemandCompileEvent::class => 'compile',
    ];
  }

  /**
   * Compile all assets provided by this module on configuration changes.
   *
   * @param \Drupal\Core\Config\ConfigCrudEvent $event
   *   The configuration change event.
   */
  public function onConfigChange(ConfigCrudEvent $event) {
    $name = $event
      ->getConfig()
      ->getName();

    // Check if the changed config is a compilation dependency.
    if (in_array($name, $this
      ->getConfigDependencies(), TRUE)) {
      $this
        ->compile();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CompileSubscriber::$compiler protected property The compiler service.
CompileSubscriber::$themeHandler protected property The theme handler service.
CompileSubscriber::$typedConfigManager protected property The typed config manager service.
CompileSubscriber::compile public function Run the compilation process for this module.
CompileSubscriber::getConfigDependencies protected function Get a list of configuration dependencies.
CompileSubscriber::getSubscribedEvents public static function
CompileSubscriber::onConfigChange public function Compile all assets provided by this module on configuration changes.
CompileSubscriber::__construct public function Constructs an CompileSubscriber object.