View source
<?php
namespace Drupal\advagg_mod\EventSubscriber;
use Drupal\advagg\Asset\AssetOptimizationEvent;
use Drupal\advagg_mod\Asset\AsyncJs;
use Drupal\advagg_mod\Asset\DeferCss;
use Drupal\advagg_mod\Asset\DeferJs;
use Drupal\advagg_mod\Asset\RemoveConsoleLog;
use Drupal\advagg_mod\Asset\TranslateCss;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Render\HtmlResponse;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class InitSubscriber implements EventSubscriberInterface {
protected $config;
protected $advaggConfig;
protected $translator;
protected $cssDeferer;
protected $jsDeferer;
protected $jsAsyncer;
protected $consoleLogRemover;
public function __construct(ConfigFactoryInterface $config_factory, TranslateCss $translator, DeferCss $deferer, AsyncJs $js_asyncer, DeferJs $js_deferer, RemoveConsoleLog $js_console_log) {
$this->config = $config_factory
->getEditable('advagg_mod.settings');
$this->advaggConfig = $config_factory
->getEditable('advagg.settings');
$this->translator = $translator;
$this->cssDeferer = $deferer;
$this->jsAsyncer = $js_asyncer;
$this->jsDeferer = $js_deferer;
$this->consoleLogRemover = $js_console_log;
}
public static function getSubscribedEvents() {
return [
KernelEvents::REQUEST => [
'onEvent',
0,
],
KernelEvents::RESPONSE => [
[
'deferCss',
0,
],
[
'deferJs',
0,
],
[
'asyncJs',
0,
],
],
AssetOptimizationEvent::CSS => [
'translateCss',
0,
],
AssetOptimizationEvent::JS => [
'removeConsoleLog',
-10,
],
];
}
public function onEvent() {
$dir = rtrim($this->config
->get('unified_multisite_dir'), '/');
if (empty($dir) || !file_exists($dir) || !is_dir($dir)) {
return;
}
$counter_filename = $dir . '/_global_counter';
$local_counter = $this->advaggConfig
->get('global_counter');
if (!file_exists($counter_filename)) {
file_unmanaged_save_data($local_counter, $counter_filename, FILE_EXISTS_REPLACE);
}
else {
$shared_counter = (int) file_get_contents($counter_filename);
if ($shared_counter == $local_counter) {
return;
}
elseif ($shared_counter < $local_counter) {
file_unmanaged_save_data($local_counter, $counter_filename, FILE_EXISTS_REPLACE);
return;
}
elseif ($shared_counter > $local_counter) {
$this->advaggConfig
->set('global_counter', $shared_counter)
->save();
return;
}
}
}
public function translateCss(AssetOptimizationEvent $assetOptimizationEvent) {
if (!$this->config
->get('css_translate')) {
return;
}
$content = $assetOptimizationEvent
->getContent();
$content = $this->translator
->optimize($content, [], []);
$assetOptimizationEvent
->setContent($content);
}
public function removeConsoleLog(AssetOptimizationEvent $assetOptimizationEvent) {
if (!$this->config
->get('js_remove_console_log')) {
return;
}
$content = $assetOptimizationEvent
->getContent();
$content = $this->consoleLogRemover
->optimize($content);
$assetOptimizationEvent
->setContent($content);
}
public function deferCss(FilterResponseEvent $response) {
if (!advagg_mod_css_defer_active()) {
return;
}
$response = $response
->getResponse();
if (!$response instanceof HtmlResponse) {
return;
}
$content = $this->cssDeferer
->defer($response
->getContent());
$response
->setContent($content);
}
public function deferJs(FilterResponseEvent $response) {
if (!$this->config
->get('js_defer')) {
return;
}
$response = $response
->getResponse();
if (!$response instanceof HtmlResponse) {
return;
}
$content = $this->jsDeferer
->defer($response
->getContent());
$response
->setContent($content);
}
public function asyncJs(FilterResponseEvent $response) {
if (!$this->config
->get('js_async') || $this->config
->get('js_defer')) {
return;
}
$response = $response
->getResponse();
if (!$response instanceof HtmlResponse) {
return;
}
$content = $this->jsAsyncer
->async($response
->getContent());
$response
->setContent($content);
}
}