ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCachedLanguage.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23
30class ilCachedLanguage implements Request
31{
32 protected \ILIAS\Cache\Container\Container $language_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 global $DIC;
44 $this->setLanguageKey($language_key);
45 $this->language_cache = $DIC->globalCache()->get($this);
46 $this->readFromCache();
47 if (!$this->getLoaded()) {
48 $this->readFromDB();
49 $this->writeToCache();
50 $this->setLoaded(true);
51 }
52 }
53
54 public function getContainerKey(): string
55 {
56 return 'clng';
57 }
58
59
60 public function isForced(): bool
61 {
62 return true;
63 }
64
68 public function isActive(): bool
69 {
70 return true;
71 }
72
76 protected function readFromCache(): void
77 {
78 $key = "translations_" . $this->getLanguageKey();
79 if ($this->language_cache->has($key)) {
80 // This is a workaround for the fact that transformatuin cannot be created by
81 // $DIC->refinery()->xy() since we are in a hell of dependencies. E.g. we cant instantiate the
82 // caching service with $DIC->refinery() since the Refinery needs ilLanguage, but ilLanguage
83 // needs the caching service and so on...
84 $always = new Transformation(
85 function ($v) {
86 return is_array($v) ? $v : null;
87 }
88 );
89
90 $translations = $this->language_cache->get($key, $always);
91 if (is_array($translations)) {
92 $this->setTranslations($translations);
93 $this->setLoaded(true);
94 }
95 }
96 }
97
101 public function writeToCache(): void
102 {
103 $this->language_cache->set("translations_" . $this->getLanguageKey(), $this->getTranslations());
104 }
105
112 public function deleteInCache(): void
113 {
114 $this->language_cache->delete("translations_" . $this->getLanguageKey());
115 $this->setLoaded(false);
116 }
117
121 protected function readFromDB(): void
122 {
123 global $DIC;
124 $ilDB = $DIC->database();
125
126 $q = 'SELECT module, lang_array FROM lng_modules WHERE lang_key = %s';
127 $res = $ilDB->queryF($q, array( "text" ), array( $this->getLanguageKey() ));
128 $translations = array();
129 while ($set = $ilDB->fetchObject($res)) {
130 try {
131 $lang_array = unserialize($set->lang_array, ['allowed_classes' => false]);
132 } catch (Throwable $t) {
133 continue;
134 }
135 if (is_array($lang_array)) {
136 $translations[$set->module] = $lang_array;
137 }
138 }
139 $this->setTranslations($translations);
140 }
141
142 public static function getInstance($key): self
143 {
144 if (!isset(self::$instances[$key])) {
145 self::$instances[$key] = new self($key);
146 }
147
148 return self::$instances[$key];
149 }
150
151 public function flush(): void
152 {
153 $this->language_cache->flush();
154 $this->readFromDB();
155 $this->writeToCache();
156 }
157
161 public function setLanguageKey(string $language_key): void
162 {
163 $this->language_key = $language_key;
164 }
165
169 public function getLanguageKey(): string
170 {
171 return $this->language_key;
172 }
173
174 public function setLoaded(bool $loaded): void
175 {
176 $this->loaded = $loaded;
177 }
178
179 public function getLoaded(): bool
180 {
181 return $this->loaded;
182 }
183
187 public function setTranslations(array $translations): void
188 {
189 $this->translations = $translations;
190 }
191
195 public function getTranslations(): array
196 {
197 return $this->translations;
198 }
199}
Transform values according to custom configuration.
Class ilCachedLanguage.
readFromDB()
Read data from table lng_module from DB.
ILIAS Cache Container Container $language_cache
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.
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26
$q
Definition: shib_logout.php:23