ILIAS  release_8 Revision v8.23
class.ilGlobalCacheSettings.php
Go to the documentation of this file.
1 <?php
2 
19 use ILIAS\Setup;
20 
26 class ilGlobalCacheSettings implements Setup\Config
27 {
31  public const LOG_LEVEL_FORCED = -1;
35  public const LOG_LEVEL_NONE = 0;
39  public const LOG_LEVEL_SHY = 1;
43  public const LOG_LEVEL_NORMAL = 2;
47  public const LOG_LEVEL_CHATTY = 3;
51  public const INI_HEADER_CACHE = 'cache';
55  public const INI_FIELD_ACTIVATE_GLOBAL_CACHE = 'activate_global_cache';
59  public const INI_FIELD_GLOBAL_CACHE_SERVICE_TYPE = 'global_cache_service_type';
63  public const INI_HEADER_CACHE_ACTIVATED_COMPONENTS = 'cache_activated_components';
67  public const INI_FIELD_LOG_LEVEL = 'log_level';
69  protected array $activated_components = [];
70  protected bool $active = false;
71  protected int $log_level = self::LOG_LEVEL_NONE;
75  protected array $memcached_nodes = [];
76 
77  public function readFromIniFile(ilIniFile $ilIniFile): void
78  {
79  $this->checkIniHeader($ilIniFile);
80  $this->setActive(
81  $ilIniFile->readVariable(
82  self::INI_HEADER_CACHE,
83  self::INI_FIELD_ACTIVATE_GLOBAL_CACHE
84  )
85  );
86  $this->setService(
87  (int)$ilIniFile->readVariable(
88  self::INI_HEADER_CACHE,
89  self::INI_FIELD_GLOBAL_CACHE_SERVICE_TYPE
90  )
91  );
92  $this->setLogLevel(
93  (int) $ilIniFile->readVariable(
94  self::INI_HEADER_CACHE,
95  self::INI_FIELD_LOG_LEVEL
96  )
97  );
98  if (!$this->isActive()) {
99  $this->resetActivatedComponents();
100  } else {
101  $cache_components = $ilIniFile->readGroup(
102  self::INI_HEADER_CACHE_ACTIVATED_COMPONENTS
103  );
104  if (is_array($cache_components)) {
105  foreach ($cache_components as $comp => $v) {
106  if ($v) {
107  $this->addActivatedComponent($comp);
108  }
109  }
110  }
111  }
112  }
113 
114  public function writeToIniFile(ilIniFile $ilIniFile): bool
115  {
116  $ilIniFile->setVariable(
117  self::INI_HEADER_CACHE,
118  self::INI_FIELD_ACTIVATE_GLOBAL_CACHE,
119  $this->isActive() ? '1' : '0'
120  );
121  $ilIniFile->setVariable(
122  self::INI_HEADER_CACHE,
123  self::INI_FIELD_GLOBAL_CACHE_SERVICE_TYPE,
124  $this->getService()
125  );
126  $ilIniFile->setVariable(
127  self::INI_HEADER_CACHE,
128  self::INI_FIELD_LOG_LEVEL,
129  $this->getLogLevel()
130  );
131 
132  $ilIniFile->removeGroup(self::INI_HEADER_CACHE_ACTIVATED_COMPONENTS);
133  $ilIniFile->addGroup(self::INI_HEADER_CACHE_ACTIVATED_COMPONENTS);
134  foreach (ilGlobalCache::getAvailableComponents() as $comp) {
135  $ilIniFile->setVariable(
136  self::INI_HEADER_CACHE_ACTIVATED_COMPONENTS,
137  $comp,
138  $this->isComponentActivated($comp) ? '1' : '0'
139  );
140  }
141  return $ilIniFile->write();
142  }
143 
144  public function activateAll(): void
145  {
146  foreach (ilGlobalCache::getAvailableComponents() as $comp) {
147  $this->addActivatedComponent($comp);
148  }
149  }
150 
154  public function addActivatedComponent($component): void
155  {
156  $this->activated_components[] = $component;
157  $this->activated_components = array_unique($this->activated_components);
158  }
159 
160  public function resetActivatedComponents(): void
161  {
162  $this->activated_components = [];
163  }
164 
168  public function isComponentActivated($component): bool
169  {
170  return in_array($component, $this->activated_components);
171  }
172 
173  public function areAllComponentActivated(): bool
174  {
175  return count($this->activated_components) === count(
177  );
178  }
179 
180  public function getService(): int
181  {
182  return $this->service;
183  }
184 
185  public function setService(int $service): void
186  {
187  $this->service = $service;
188  }
189 
190  public function getActivatedComponents(): array
191  {
193  }
194 
195  public function setActivatedComponents(array $activated_components): void
196  {
197  $this->activated_components = $activated_components;
198  }
199 
200  public function isActive(): bool
201  {
202  return $this->active;
203  }
204 
205  public function setActive(bool $active): void
206  {
207  $this->active = $active;
208  }
209 
210  protected function checkIniHeader(ilIniFile $ilIniFile): void
211  {
212  if (!$ilIniFile->readGroup(self::INI_HEADER_CACHE)) {
213  $ilIniFile->addGroup(self::INI_HEADER_CACHE);
214  }
215  if (!$ilIniFile->readGroup(
216  self::INI_HEADER_CACHE_ACTIVATED_COMPONENTS
217  )) {
218  $ilIniFile->addGroup(self::INI_HEADER_CACHE_ACTIVATED_COMPONENTS);
219  }
220  }
221 
222  public function getLogLevel(): int
223  {
224  return $this->log_level;
225  }
226 
227  public function setLogLevel(int $log_level): void
228  {
229  $this->log_level = $log_level;
230  }
231 
232  public function __toString(): string
233  {
234  $service = 'Service: ' . ($this->getService(
236  $this->getService()
237  ) : 'none');
238  $activated = 'Activated Components: ' . implode(
239  ', ',
240  $this->getActivatedComponents()
241  );
242  $log_level = 'Log Level: ' . $this->getLogLevelName();
243 
244  return implode("\n", ['', '', $service, $activated, $log_level, '']);
245  }
246 
247  protected function getLogLevelName(): string
248  {
249  return $this->lookupLogLevelName($this->getLogLevel());
250  }
251 
252  protected function lookupLogLevelName(int $level): string
253  {
254  $r = new ReflectionClass($this);
255  foreach ($r->getConstants() as $k => $v) {
256  if (strpos($k, 'LOG_LEVEL') === 0 && $v == $level) {
257  return $k;
258  }
259  }
260 
261  return '';
262  }
263 
264  public function addMemcachedNode(ilMemcacheServer $node_id): void
265  {
266  $this->memcached_nodes[] = $node_id;
267  }
268 
269  public function getMemcachedNodes(): array
270  {
271  return $this->memcached_nodes;
272  }
273 }
checkIniHeader(ilIniFile $ilIniFile)
writeToIniFile(ilIniFile $ilIniFile)
addGroup(string $a_group_name)
adds a new group
addMemcachedNode(ilMemcacheServer $node_id)
write()
save ini-file-data to filesystem
Class ilGlobalCacheSettings.
readFromIniFile(ilIniFile $ilIniFile)
setVariable(string $a_group_name, string $a_var_name, string $a_var_value)
sets a variable in a group
readGroup(string $a_group_name)
returns an associative array of the variables in one group
static lookupServiceClassName(int $service_type)
static getAvailableComponents()
removeGroup(string $a_group_name)
removes a group
setActivatedComponents(array $activated_components)
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
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...