3 namespace Drupal\advagg_mod\EventSubscriber;
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Symfony\Component\HttpKernel\KernelEvents;
7 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10 * Perform initialization tasks for advagg_mod.
12 class InitSubscriber implements EventSubscriberInterface {
15 * A config object for the advagg_mod configuration.
17 * @var \Drupal\Core\Config\Config
22 * An editable config object for the advagg configuration.
24 * @var \Drupal\Core\Config\Config
26 protected $advaggConfig;
29 * Constructs the Subscriber object.
31 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
32 * A config factory for retrieving required config objects.
34 public function __construct(ConfigFactoryInterface $config_factory) {
35 $this->config = $config_factory->get('advagg.settings');
36 $this->advaggConfig = $config_factory->getEditable('advagg.settings');
42 public static function getSubscribedEvents() {
43 return [KernelEvents::REQUEST => ['onEvent', 0]];
47 * Synchronize global_counter variable between sites.
49 * Only if using unified_multisite_dir.
51 public function onEvent() {
52 $dir = rtrim($this->config->get('unified_multisite_dir'), '/');
53 if (empty($dir) || !file_exists($dir) || !is_dir($dir)) {
57 $counter_filename = $dir . '/_global_counter';
58 $local_counter = advagg_get_global_counter();
59 if (!file_exists($counter_filename)) {
60 file_unmanaged_save_data($local_counter, $counter_filename, FILE_EXISTS_REPLACE);
63 $shared_counter = (int) file_get_contents($counter_filename);
65 if ($shared_counter == $local_counter) {
66 // Counters are the same, return.
69 elseif ($shared_counter < $local_counter) {
70 // Local counter is higher, update saved file and return.
71 ile_unmanaged_save_data($local_counter, $counter_filename, FILE_EXISTS_REPLACE);
74 elseif ($shared_counter > $local_counter) {
75 // Shared counter is higher, update local copy and return.
76 $this->advaggConfig->set('global_counter', $shared_counter)->save();