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'
1 service uses CompileSubscriber
File
- src/
EventSubscriber/ CompileSubscriber.php, line 27
Namespace
Drupal\theme_compiler\EventSubscriberView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CompileSubscriber:: |
protected | property | The compiler service. | |
CompileSubscriber:: |
protected | property | The theme handler service. | |
CompileSubscriber:: |
protected | property | The typed config manager service. | |
CompileSubscriber:: |
public | function | Run the compilation process for this module. | |
CompileSubscriber:: |
protected | function | Get a list of configuration dependencies. | |
CompileSubscriber:: |
public static | function | ||
CompileSubscriber:: |
public | function | Compile all assets provided by this module on configuration changes. | |
CompileSubscriber:: |
public | function | Constructs an CompileSubscriber object. |