You are here

public function InitSubscriber::onEvent in Advanced CSS/JS Aggregation 8.4

Same name and namespace in other branches
  1. 8.2 advagg_mod/src/EventSubscriber/InitSubscriber.php \Drupal\advagg_mod\EventSubscriber\InitSubscriber::onEvent()
  2. 8.3 advagg_mod/src/EventSubscriber/InitSubscriber.php \Drupal\advagg_mod\EventSubscriber\InitSubscriber::onEvent()

Synchronize global_counter variable between sites.

Only if using unified_multisite_dir.

File

advagg_mod/src/EventSubscriber/InitSubscriber.php, line 129

Class

InitSubscriber
Perform initialization tasks for advagg_mod.

Namespace

Drupal\advagg_mod\EventSubscriber

Code

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)) {
    $this->fileSystem
      ->saveData($local_counter, $counter_filename, FileSystemInterface::EXISTS_REPLACE);
  }
  else {
    $shared_counter = (int) file_get_contents($counter_filename);
    if ($shared_counter == $local_counter) {

      // Counters are the same, return.
      return;
    }
    elseif ($shared_counter < $local_counter) {

      // Local counter is higher, update saved file and return.
      $this->fileSystem
        ->saveData($local_counter, $counter_filename, FileSystemInterface::EXISTS_REPLACE);
      return;
    }
    elseif ($shared_counter > $local_counter) {

      // Shared counter is higher, update local copy and return.
      $this->advaggConfig
        ->set('global_counter', $shared_counter)
        ->save();
      return;
    }
  }
}