ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilGlobalCacheSettingsAdapter.php
Go to the documentation of this file.
1 <?php
2 
19 use ILIAS\Setup;
22 
26 class ilGlobalCacheSettingsAdapter implements Setup\Config
27 {
31  private ?Config $config = null;
32  private bool $active = true;
33  private string $service = Config::PHPSTATIC;
34  private array $components = [];
38  private array $nodes = [];
39 
40  public function __construct(
41  private readonly ?ilIniFile $client_ini = null,
42  private readonly ?ilDBInterface $db = null
43  ) {
44  if ($this->client_ini !== null) {
45  $this->readFromIniFile($this->client_ini);
46  }
47  $this->config = $this->toConfig();
48  }
49 
50  public function getConfig(): Config
51  {
52  return $this->config ?? $this->toConfig();
53  }
54 
55  public function toConfig(): Config
56  {
57  return new Config(
58  $this->service,
59  $this->active,
60  $this->components,
61  $this->getNodesRepository()
62  );
63  }
64 
65  public function readFromIniFile(ilIniFile $client_ini): bool
66  {
67  $this->active = (bool) $client_ini->readVariable('cache', 'activate_global_cache');
68  $service_type = $client_ini->readVariable('cache', 'global_cache_service_type');
69  if (is_numeric($service_type)) {
70  $service_type = match ((int) $service_type) {
71  0 => Config::PHPSTATIC,
72  2 => Config::MEMCACHED,
73  3 => Config::APCU,
74  default => Config::PHPSTATIC,
75  };
76  }
77  $this->service = $service_type;
78  $read_group = $client_ini->readGroup('cache_activated_components');
79 
80  $this->components = array_unique(
81  array_map(static function (string $component): string {
82  if ($component === 'all') {
83  return '*';
84  }
85  return $component;
86  }, array_keys($read_group))
87  );
88  $this->nodes = [];
89  if ($this->db !== null) {
90  $repo = $this->getNodesRepository();
91  foreach ($repo->getNodes() as $node) {
92  $this->addMemcachedNode($node);
93  }
94  }
95 
96  return true;
97  }
98 
100  {
101  return new ilMemcacheNodesRepository($this->db);
102  }
103 
104  public function storeToIniFile(ilIniFile $client_ini): bool
105  {
106  $client_ini->setVariable('cache', 'activate_global_cache', $this->active ? '1' : '0');
107  $client_ini->setVariable('cache', 'global_cache_service_type', $this->service);
108  $client_ini->removeGroup('cache_activated_components');
109  $client_ini->addGroup('cache_activated_components');
110  foreach ($this->components as $component) {
111  $client_ini->setVariable('cache_activated_components', $component, '1');
112  }
113 
114  // store nodes to db
115  if ($this->db !== null) {
116  $repo = $this->getNodesRepository();
117  $repo->deleteAll();
118  foreach ($this->nodes as $node) {
119  $repo->store($node);
120  }
121  }
122 
123  return $client_ini->write();
124  }
125 
126  public function setActive(bool $active): void
127  {
128  $this->active = $active;
129  }
130 
131  public function setService(string $service): void
132  {
133  $this->service = $service;
134  }
135 
136  public function addMemcachedNode(Node $node): void
137  {
138  $this->nodes[] = $node;
139  }
140 
141  public function getMemcachedNodes(): array
142  {
143  return $this->nodes;
144  }
145 
146  public function resetActivatedComponents(): void
147  {
148  $this->components = [];
149  }
150 
151  public function activateAll(): void
152  {
153  $this->components = ['*'];
154  }
155 
156  public function addActivatedComponent(string $component): void
157  {
158  $this->components[] = $component;
159  }
160 }
addGroup(string $a_group_name)
adds a new group
write()
save ini-file-data to filesystem
setVariable(string $a_group_name, string $a_var_name, string $a_var_value)
sets a variable in a group
__construct(private readonly ?ilIniFile $client_ini=null, private readonly ?ilDBInterface $db=null)
readGroup(string $a_group_name)
returns an associative array of the variables in one group
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
removeGroup(string $a_group_name)
removes a group
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
readVariable(string $a_group, string $a_var_name)
reads a single variable from a group
Class ilChatroomConfigFileHandler .