ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilCachedLanguage.php
Go to the documentation of this file.
1<?php
2require_once('./Services/GlobalCache/classes/class.ilGlobalCache.php');
3
11{
12 protected $global_cache;
16 protected $loaded = false;
20 protected $language_key = 'en';
24 protected $translations = array();
28 protected static $instances = array();
29
30
34 protected function __construct($language_key)
35 {
42 $this->readFromCache();
43 if (!$this->getLoaded()) {
44 $this->readFromDB();
45 $this->writeToCache();
46 $this->setLoaded(true);
47 }
48 }
49
50
54 public function isActive()
55 {
56 return $this->global_cache->isActive();
57 }
58
59
60 protected function readFromCache()
61 {
62 if ($this->global_cache->isActive()) {
63 $translations = $this->global_cache->get('translations_' . $this->getLanguageKey());
64 if (is_array($translations)) {
66 $this->setLoaded(true);
67 }
68 }
69 }
70
71
72 public function writeToCache()
73 {
74 if ($this->global_cache->isActive()) {
75 $this->global_cache->set('translations_' . $this->getLanguageKey(), $this->getTranslations());
76 }
77 }
78
85 public function deleteInCache() {
86 if ($this->global_cache->isActive()) {
87 $this->global_cache->delete('translations_' . $this->getLanguageKey());
88 $this->setLoaded(false);
89 }
90 }
91
92
93
94 protected function readFromDB()
95 {
96 global $DIC;
97 $ilDB = $DIC->database();
98
99 $q = 'SELECT module, lang_array FROM lng_modules WHERE lang_key = %s';
100 $res = $ilDB->queryF($q, array( 'text' ), array( $this->getLanguageKey() ));
101 $translations = array();
102 while ($set = $ilDB->fetchObject($res)) {
103 $lang_array = unserialize($set->lang_array);
104 if (is_array($lang_array)) {
105 $translations[$set->module] = $lang_array;
106 }
107 }
109 }
110
111
117 public static function getInstance($key)
118 {
119 if (!isset(self::$instances[$key])) {
120 self::$instances[$key] = new self($key);
121 }
122
123 return self::$instances[$key];
124 }
125
126
127 public function flush()
128 {
129 if ($this->global_cache->isActive()) {
130 $this->global_cache->flush();
131 }
132 $this->readFromDB();
133 $this->writeToCache();
134 }
135
136
141 {
142 $this->language_key = $language_key;
143 }
144
145
149 public function getLanguageKey()
150 {
151 return $this->language_key;
152 }
153
154
158 public function setLoaded($loaded)
159 {
160 $this->loaded = $loaded;
161 }
162
163
167 public function getLoaded()
168 {
169 return $this->loaded;
170 }
171
172
177 {
178 $this->translations = $translations;
179 }
180
181
185 public function getTranslations()
186 {
187 return $this->translations;
188 }
189}
An exception for terminatinating execution or to throw for unit testing.
Class ilCachedLanguage.
setTranslations($translations)
setLanguageKey($language_key)
deleteInCache()
Delete the cache entry for this language without flushing the whole global cache Using this function ...
static getInstance($component)
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
foreach($_POST as $key=> $value) $res
global $ilDB
$DIC
Definition: xapitoken.php:46