ILIAS  release_8 Revision v8.24
class.ilCachedLanguage.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
22require_once "./Services/GlobalCache/classes/class.ilGlobalCache.php";
23
31{
32 protected \ilGlobalCache $global_cache;
33 protected bool $loaded = false;
34 protected string $language_key = "en";
35 protected array $translations = array();
36 protected static array $instances = array();
37
41 protected function __construct(string $language_key)
42 {
43 $this->setLanguageKey($language_key);
45 $this->readFromCache();
46 if (!$this->getLoaded()) {
47 $this->readFromDB();
48 $this->writeToCache();
49 $this->setLoaded(true);
50 }
51 }
52
56 public function isActive(): bool
57 {
58 return $this->global_cache->isActive();
59 }
60
64 protected function readFromCache(): void
65 {
66 if ($this->global_cache->isActive()) {
67 $translations = $this->global_cache->get("translations_" . $this->getLanguageKey());
68 if (is_array($translations)) {
69 $this->setTranslations($translations);
70 $this->setLoaded(true);
71 }
72 }
73 }
74
78 public function writeToCache(): void
79 {
80 if ($this->global_cache->isActive()) {
81 $this->global_cache->set("translations_" . $this->getLanguageKey(), $this->getTranslations());
82 }
83 }
84
91 public function deleteInCache(): void
92 {
93 if ($this->global_cache->isActive()) {
94 $this->global_cache->delete("translations_" . $this->getLanguageKey());
95 $this->setLoaded(false);
96 }
97 }
98
102 protected function readFromDB(): void
103 {
104 global $DIC;
105 $ilDB = $DIC->database();
106
107 $q = 'SELECT module, lang_array FROM lng_modules WHERE lang_key = %s';
108 $res = $ilDB->queryF($q, array( "text" ), array( $this->getLanguageKey() ));
109 $translations = array();
110 while ($set = $ilDB->fetchObject($res)) {
111 try {
112 $lang_array = unserialize($set->lang_array, ['allowed_classes' => false]);
113 } catch (Throwable $t) {
114 continue;
115 }
116 if (is_array($lang_array)) {
117 $translations[$set->module] = $lang_array;
118 }
119 }
120 $this->setTranslations($translations);
121 }
122
123 public static function getInstance($key): self
124 {
125 if (!isset(self::$instances[$key])) {
126 self::$instances[$key] = new self($key);
127 }
128
129 return self::$instances[$key];
130 }
131
132 public function flush(): void
133 {
134 if ($this->global_cache->isActive()) {
135 $this->global_cache->flush();
136 }
137 $this->readFromDB();
138 $this->writeToCache();
139 }
140
144 public function setLanguageKey(string $language_key): void
145 {
146 $this->language_key = $language_key;
147 }
148
152 public function getLanguageKey(): string
153 {
154 return $this->language_key;
155 }
156
157 public function setLoaded(bool $loaded): void
158 {
159 $this->loaded = $loaded;
160 }
161
162 public function getLoaded(): bool
163 {
164 return $this->loaded;
165 }
166
170 public function setTranslations(array $translations): void
171 {
172 $this->translations = $translations;
173 }
174
178 public function getTranslations(): array
179 {
180 return $this->translations;
181 }
182}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
readFromDB()
Read data from table lng_module from DB.
setLanguageKey(string $language_key)
Set language key.
isActive()
Return whether the global cache is active.
readFromCache()
Read from cache.
deleteInCache()
Delete the cache entry for this language without flushing the whole global cache Using this function ...
setTranslations(array $translations)
Set translations.
getTranslations()
Return translations as array.
writeToCache()
Write to global cache.
getLanguageKey()
Return language key.
__construct(string $language_key)
ilCachedLanguage constructor.
static getInstance(?string $component)
global $DIC
Definition: feed.php:28
$res
Definition: ltiservices.php:69
string $key
Consumer key/client ID value.
Definition: System.php:193