Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / advagg / advagg_mod / src / EventSubscriber / InitSubscriber.php
1 <?php
2
3 namespace Drupal\advagg_mod\EventSubscriber;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Symfony\Component\HttpKernel\KernelEvents;
7 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
9 /**
10  * Perform initialization tasks for advagg_mod.
11  */
12 class InitSubscriber implements EventSubscriberInterface {
13
14   /**
15    * A config object for the advagg_mod configuration.
16    *
17    * @var \Drupal\Core\Config\Config
18    */
19   protected $config;
20
21   /**
22    * An editable config object for the advagg configuration.
23    *
24    * @var \Drupal\Core\Config\Config
25    */
26   protected $advaggConfig;
27
28   /**
29    * Constructs the Subscriber object.
30    *
31    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
32    *   A config factory for retrieving required config objects.
33    */
34   public function __construct(ConfigFactoryInterface $config_factory) {
35     $this->config = $config_factory->get('advagg.settings');
36     $this->advaggConfig = $config_factory->getEditable('advagg.settings');
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public static function getSubscribedEvents() {
43     return [KernelEvents::REQUEST => ['onEvent', 0]];
44   }
45
46   /**
47    * Synchronize global_counter variable between sites.
48    *
49    * Only if using unified_multisite_dir.
50    */
51   public function onEvent() {
52     $dir = rtrim($this->config->get('unified_multisite_dir'), '/');
53     if (empty($dir) || !file_exists($dir) || !is_dir($dir)) {
54       return;
55     }
56
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);
61     }
62     else {
63       $shared_counter = (int) file_get_contents($counter_filename);
64
65       if ($shared_counter == $local_counter) {
66         // Counters are the same, return.
67         return;
68       }
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);
72         return;
73       }
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();
77         return;
78       }
79     }
80   }
81
82 }