ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilCachedLanguage Class Reference

Class ilCachedLanguage. More...

+ Inheritance diagram for ilCachedLanguage:
+ Collaboration diagram for ilCachedLanguage:

Public Member Functions

 getContainerKey ()
 
 isForced ()
 
 isActive ()
 Return whether the global cache is active. More...
 
 writeToCache ()
 Write to global cache. More...
 
 deleteInCache ()
 Delete the cache entry for this language without flushing the whole global cache Using this function avoids a flush loop when languages are updated A missing entry will cause the next request to refill the cache in the constructor of this class. More...
 
 flush ()
 
 setLanguageKey (string $language_key)
 Set language key. More...
 
 getLanguageKey ()
 Return language key. More...
 
 setLoaded (bool $loaded)
 
 getLoaded ()
 
 setTranslations (array $translations)
 Set translations. More...
 
 getTranslations ()
 Return translations as array. More...
 
 getContainerKey ()
 
 isForced ()
 

Static Public Member Functions

static getInstance ($key)
 

Protected Member Functions

 __construct (string $language_key)
 ilCachedLanguage constructor. More...
 
 readFromCache ()
 Read from cache. More...
 
 readFromDB ()
 Read data from table lng_module from DB. More...
 

Protected Attributes

ILIAS Cache Container Container $language_cache
 
bool $loaded = false
 
string $language_key = "en"
 
array $translations = array()
 

Static Protected Attributes

static array $instances = array()
 

Detailed Description

Class ilCachedLanguage.

Author
Fabian Schmid fs@st.nosp@m.uder.nosp@m.-raim.nosp@m.ann..nosp@m.ch
Version
1.0.0

Definition at line 30 of file class.ilCachedLanguage.php.

Constructor & Destructor Documentation

◆ __construct()

ilCachedLanguage::__construct ( string  $language_key)
protected

ilCachedLanguage constructor.

Definition at line 41 of file class.ilCachedLanguage.php.

42 {
43 global $DIC;
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 }
readFromDB()
Read data from table lng_module from DB.
setLanguageKey(string $language_key)
Set language key.
readFromCache()
Read from cache.
writeToCache()
Write to global cache.
global $DIC
Definition: shib_login.php:26

References $DIC, getLoaded(), readFromCache(), readFromDB(), setLanguageKey(), setLoaded(), and writeToCache().

+ Here is the call graph for this function:

Member Function Documentation

◆ deleteInCache()

ilCachedLanguage::deleteInCache ( )

Delete the cache entry for this language without flushing the whole global cache Using this function avoids a flush loop when languages are updated A missing entry will cause the next request to refill the cache in the constructor of this class.

See also
mantis #28818

Definition at line 112 of file class.ilCachedLanguage.php.

112 : void
113 {
114 $this->language_cache->delete("translations_" . $this->getLanguageKey());
115 $this->setLoaded(false);
116 }
getLanguageKey()
Return language key.

References getLanguageKey(), and setLoaded().

+ Here is the call graph for this function:

◆ flush()

ilCachedLanguage::flush ( )

Definition at line 151 of file class.ilCachedLanguage.php.

151 : void
152 {
153 $this->language_cache->flush();
154 $this->readFromDB();
155 $this->writeToCache();
156 }

References readFromDB(), and writeToCache().

+ Here is the call graph for this function:

◆ getContainerKey()

ilCachedLanguage::getContainerKey ( )

Implements ILIAS\Cache\Container\Request.

Definition at line 54 of file class.ilCachedLanguage.php.

54 : string
55 {
56 return 'clng';
57 }

◆ getInstance()

static ilCachedLanguage::getInstance (   $key)
static

Definition at line 142 of file class.ilCachedLanguage.php.

142 : self
143 {
144 if (!isset(self::$instances[$key])) {
145 self::$instances[$key] = new self($key);
146 }
147
148 return self::$instances[$key];
149 }

Referenced by ilLanguage\__construct(), ilObjLanguageExt\_saveValues(), ilObjLanguage\replaceLangModule(), and ilObjLanguageDBAccess\replaceLangModules().

+ Here is the caller graph for this function:

◆ getLanguageKey()

ilCachedLanguage::getLanguageKey ( )

Return language key.

Definition at line 169 of file class.ilCachedLanguage.php.

169 : string
170 {
171 return $this->language_key;
172 }

References $language_key.

Referenced by deleteInCache(), readFromCache(), readFromDB(), and writeToCache().

+ Here is the caller graph for this function:

◆ getLoaded()

ilCachedLanguage::getLoaded ( )

Definition at line 179 of file class.ilCachedLanguage.php.

179 : bool
180 {
181 return $this->loaded;
182 }

References $loaded.

Referenced by __construct().

+ Here is the caller graph for this function:

◆ getTranslations()

ilCachedLanguage::getTranslations ( )

Return translations as array.

Definition at line 195 of file class.ilCachedLanguage.php.

195 : array
196 {
197 return $this->translations;
198 }

References $translations.

Referenced by writeToCache().

+ Here is the caller graph for this function:

◆ isActive()

ilCachedLanguage::isActive ( )

Return whether the global cache is active.

Definition at line 68 of file class.ilCachedLanguage.php.

68 : bool
69 {
70 return true;
71 }

◆ isForced()

ilCachedLanguage::isForced ( )

Implements ILIAS\Cache\Container\Request.

Definition at line 60 of file class.ilCachedLanguage.php.

60 : bool
61 {
62 return true;
63 }

◆ readFromCache()

ilCachedLanguage::readFromCache ( )
protected

Read from cache.

Definition at line 76 of file class.ilCachedLanguage.php.

76 : 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 }
Transform values according to custom configuration.
setTranslations(array $translations)
Set translations.

References $translations, getLanguageKey(), setLoaded(), and setTranslations().

Referenced by __construct().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ readFromDB()

ilCachedLanguage::readFromDB ( )
protected

Read data from table lng_module from DB.

Definition at line 121 of file class.ilCachedLanguage.php.

121 : 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 }
140 }
$res
Definition: ltiservices.php:69
$q
Definition: shib_logout.php:23

References $DIC, $ilDB, $q, $res, $translations, getLanguageKey(), and setTranslations().

Referenced by __construct(), and flush().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setLanguageKey()

ilCachedLanguage::setLanguageKey ( string  $language_key)

Set language key.

Definition at line 161 of file class.ilCachedLanguage.php.

161 : void
162 {
163 $this->language_key = $language_key;
164 }

References $language_key.

Referenced by __construct().

+ Here is the caller graph for this function:

◆ setLoaded()

ilCachedLanguage::setLoaded ( bool  $loaded)

Definition at line 174 of file class.ilCachedLanguage.php.

174 : void
175 {
176 $this->loaded = $loaded;
177 }

References $loaded.

Referenced by __construct(), deleteInCache(), and readFromCache().

+ Here is the caller graph for this function:

◆ setTranslations()

ilCachedLanguage::setTranslations ( array  $translations)

Set translations.

Definition at line 187 of file class.ilCachedLanguage.php.

187 : void
188 {
189 $this->translations = $translations;
190 }

References $translations.

Referenced by readFromCache(), and readFromDB().

+ Here is the caller graph for this function:

◆ writeToCache()

ilCachedLanguage::writeToCache ( )

Write to global cache.

Definition at line 101 of file class.ilCachedLanguage.php.

101 : void
102 {
103 $this->language_cache->set("translations_" . $this->getLanguageKey(), $this->getTranslations());
104 }
getTranslations()
Return translations as array.

References getLanguageKey(), and getTranslations().

Referenced by __construct(), and flush().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $instances

array ilCachedLanguage::$instances = array()
staticprotected

Definition at line 36 of file class.ilCachedLanguage.php.

◆ $language_cache

ILIAS Cache Container Container ilCachedLanguage::$language_cache
protected

Definition at line 32 of file class.ilCachedLanguage.php.

◆ $language_key

string ilCachedLanguage::$language_key = "en"
protected

Definition at line 34 of file class.ilCachedLanguage.php.

Referenced by getLanguageKey(), and setLanguageKey().

◆ $loaded

bool ilCachedLanguage::$loaded = false
protected

Definition at line 33 of file class.ilCachedLanguage.php.

Referenced by getLoaded(), and setLoaded().

◆ $translations

array ilCachedLanguage::$translations = array()
protected

The documentation for this class was generated from the following file: