CompileSubscriber.php in Theme Compiler 2.0.x
File
src/EventSubscriber/CompileSubscriber.php
View source
<?php
namespace Drupal\theme_compiler\EventSubscriber;
use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigEvents;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\theme_compiler\Compiler;
use Drupal\theme_compiler\Event\OnDemandCompileEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CompileSubscriber implements EventSubscriberInterface {
protected $compiler;
protected $themeHandler;
protected $typedConfigManager;
public function __construct(Compiler $compiler, ThemeHandlerInterface $theme_handler, TypedConfigManagerInterface $typed_config_manager) {
$this->compiler = $compiler;
$this->themeHandler = $theme_handler;
$this->typedConfigManager = $typed_config_manager;
}
public function compile() {
$this->compiler
->compileAssets();
}
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);
}
public static function getSubscribedEvents() {
return [
ConfigEvents::DELETE => 'onConfigChange',
ConfigEvents::SAVE => 'onConfigChange',
OnDemandCompileEvent::class => 'compile',
];
}
public function onConfigChange(ConfigCrudEvent $event) {
$name = $event
->getConfig()
->getName();
if (in_array($name, $this
->getConfigDependencies(), TRUE)) {
$this
->compile();
}
}
}
Classes
Name |
Description |
CompileSubscriber |
An event subscriber used to trigger (re)compilation of this module's assets. |