From: Alexander Ebert Date: Mon, 19 Sep 2011 13:07:31 +0000 (+0200) Subject: LanguageFactory is now a descendant of SingletonFactory X-Git-Tag: 2.0.0_Beta_1~1783^2~2 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=6102265821bebaffae1325f27854013fad826fe2;p=GitHub%2FWoltLab%2FWCF.git LanguageFactory is now a descendant of SingletonFactory --- diff --git a/wcfsetup/install/files/lib/acp/form/UserAddForm.class.php b/wcfsetup/install/files/lib/acp/form/UserAddForm.class.php index 00f21ad718..3ec1fa8f79 100644 --- a/wcfsetup/install/files/lib/acp/form/UserAddForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/UserAddForm.class.php @@ -156,20 +156,20 @@ class UserAddForm extends UserOptionListForm { } // validate user language - $language = LanguageFactory::getLanguage($this->languageID); + $language = LanguageFactory::getInstance()->getLanguage($this->languageID); if (!$language->languageID) { // use default language - $this->languageID = LanguageFactory::getDefaultLanguageID(); + $this->languageID = LanguageFactory::getInstance()->getDefaultLanguageID(); } // validate visible languages foreach ($this->visibleLanguages as $key => $visibleLanguage) { - $language = LanguageFactory::getLanguage($visibleLanguage); + $language = LanguageFactory::getInstance()->getLanguage($visibleLanguage); if (!$language->languageID || !$language->hasContent) { unset($this->visibleLanguages[$key]); } } - if (!count($this->visibleLanguages) && ($language = LanguageFactory::getLanguage($this->languageID)) && $language->hasContent) { + if (!count($this->visibleLanguages) && ($language = LanguageFactory::getInstance()->getLanguage($this->languageID)) && $language->hasContent) { $this->visibleLanguages[] = $this->languageID; } diff --git a/wcfsetup/install/files/lib/acp/form/UserOptionListForm.class.php b/wcfsetup/install/files/lib/acp/form/UserOptionListForm.class.php index 0247163acb..c6dfe35704 100644 --- a/wcfsetup/install/files/lib/acp/form/UserOptionListForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/UserOptionListForm.class.php @@ -39,7 +39,7 @@ abstract class UserOptionListForm extends AbstractOptionListForm { */ protected function getAvailableLanguages() { $availableLanguages = array(); - foreach (LanguageFactory::getAvailableLanguages(PACKAGE_ID) as $language) { + foreach (LanguageFactory::getInstance()->getAvailableLanguages(PACKAGE_ID) as $language) { $availableLanguages[$language['languageID']] = WCF::getLanguage()->get('wcf.global.language.'.$language['languageCode']); } @@ -56,7 +56,7 @@ abstract class UserOptionListForm extends AbstractOptionListForm { */ public static function getAvailableContentLanguages() { $availableLanguages = array(); - foreach (LanguageFactory::getAvailableContentLanguages(PACKAGE_ID) as $language) { + foreach (LanguageFactory::getInstance()->getAvailableContentLanguages(PACKAGE_ID) as $language) { $availableLanguages[$language['languageID']] = WCF::getLanguage()->get('wcf.global.language.'.$language['languageCode']); } @@ -72,7 +72,7 @@ abstract class UserOptionListForm extends AbstractOptionListForm { * @return integer $languageID */ protected function getDefaultFormLanguageID() { - return LanguageFactory::getDefaultLanguageID(); + return LanguageFactory::getInstance()->getDefaultLanguageID(); } /** diff --git a/wcfsetup/install/files/lib/data/language/Language.class.php b/wcfsetup/install/files/lib/data/language/Language.class.php index 16694313f1..76fe59a8d2 100644 --- a/wcfsetup/install/files/lib/data/language/Language.class.php +++ b/wcfsetup/install/files/lib/data/language/Language.class.php @@ -59,7 +59,7 @@ class Language extends DatabaseObject { * @return string */ public function getFixedLanguageCode() { - return LanguageFactory::fixLanguageCode($this->languageCode); + return LanguageFactory::getInstance()->fixLanguageCode($this->languageCode); } /** @@ -125,7 +125,7 @@ class Language extends DatabaseObject { * @return boolean */ protected function loadCategory($category) { - if (!LanguageFactory::isValidCategory($category)) { + if (!LanguageFactory::getInstance()->isValidCategory($category)) { return false; } @@ -137,7 +137,7 @@ class Language extends DatabaseObject { } // rebuild language file - $languageCategory = LanguageFactory::getCategory($category); + $languageCategory = LanguageFactory::getInstance()->getCategory($category); $this->editor->updateCategory(array($languageCategory->languageCategoryID), array($this->packageID)); } diff --git a/wcfsetup/install/files/lib/data/language/LanguageEditor.class.php b/wcfsetup/install/files/lib/data/language/LanguageEditor.class.php index f029106405..60d2c4ff0f 100644 --- a/wcfsetup/install/files/lib/data/language/LanguageEditor.class.php +++ b/wcfsetup/install/files/lib/data/language/LanguageEditor.class.php @@ -129,7 +129,7 @@ class LanguageEditor extends DatabaseObjectEditor { // compile dynamic language variables if ($categoryName != 'wcf.global' && strpos($languageItemValue, '{') !== false) { - $file->write("\$this->dynamicItems['".$languageItem."'] = '".str_replace("'", "\'", LanguageFactory::getScriptingCompiler()->compileString($languageItem, $languageItemValue))."';\n"); + $file->write("\$this->dynamicItems['".$languageItem."'] = '".str_replace("'", "\'", LanguageFactory::getInstance()->getScriptingCompiler()->compileString($languageItem, $languageItemValue))."';\n"); } } @@ -340,7 +340,7 @@ class LanguageEditor extends DatabaseObjectEditor { $languageCode = self::readLanguageCodeFromXML($xml); // try to find an existing language with the given language code - $language = LanguageFactory::getLanguageByCode($languageCode); + $language = LanguageFactory::getInstance()->getLanguageByCode($languageCode); // create new language if ($language === null) { diff --git a/wcfsetup/install/files/lib/data/language/SetupLanguage.class.php b/wcfsetup/install/files/lib/data/language/SetupLanguage.class.php index 37042956b7..330e081b7b 100644 --- a/wcfsetup/install/files/lib/data/language/SetupLanguage.class.php +++ b/wcfsetup/install/files/lib/data/language/SetupLanguage.class.php @@ -64,7 +64,7 @@ class SetupLanguage extends Language { // compile dynamic language variables if (strpos($name['cdata'], '{') !== false) { - $file->write("\$this->dynamicItems['".$name['name']."'] = '".str_replace("'", "\'", LanguageFactory::getScriptingCompiler()->compileString($name['name'], $name['cdata']))."';\n"); + $file->write("\$this->dynamicItems['".$name['name']."'] = '".str_replace("'", "\'", LanguageFactory::getInstance()->getScriptingCompiler()->compileString($name['name'], $name['cdata']))."';\n"); } } diff --git a/wcfsetup/install/files/lib/system/WCF.class.php b/wcfsetup/install/files/lib/system/WCF.class.php index 7e4f803a47..9e90976ffe 100644 --- a/wcfsetup/install/files/lib/system/WCF.class.php +++ b/wcfsetup/install/files/lib/system/WCF.class.php @@ -349,7 +349,7 @@ class WCF { mb_language('uni'); // get language - self::$languageObj = LanguageFactory::getLanguage(self::getSession()->getLanguageID()); + self::$languageObj = LanguageFactory::getInstance()->getLanguage(self::getSession()->getLanguageID()); } /** @@ -491,7 +491,7 @@ class WCF { * @param integer $languageID */ public static final function setLanguage($languageID) { - self::$languageObj = LanguageFactory::getLanguage($languageID); + self::$languageObj = LanguageFactory::getInstance()->getLanguage($languageID); self::getTPL()->setLanguageID(self::getLanguage()->languageID); } diff --git a/wcfsetup/install/files/lib/system/WCFSetup.class.php b/wcfsetup/install/files/lib/system/WCFSetup.class.php index 43e6488cdc..0880c81eef 100644 --- a/wcfsetup/install/files/lib/system/WCFSetup.class.php +++ b/wcfsetup/install/files/lib/system/WCFSetup.class.php @@ -91,7 +91,7 @@ class WCFSetup extends WCF { self::$selectedLanguageCode = $_REQUEST['languageCode']; } else { - self::$selectedLanguageCode = LanguageFactory::getPreferredLanguage(self::$availableLanguages, self::$selectedLanguageCode); + self::$selectedLanguageCode = LanguageFactory::getInstance()->getPreferredLanguage(self::$availableLanguages, self::$selectedLanguageCode); } if (isset($_POST['selectedLanguages']) && is_array($_POST['selectedLanguages'])) { @@ -792,8 +792,8 @@ class WCFSetup extends WCF { } // set default language - $language = LanguageFactory::getLanguageByCode(in_array(self::$selectedLanguageCode, self::$selectedLanguages) ? self::$selectedLanguageCode : self::$selectedLanguages[0]); - LanguageFactory::makeDefault($language->languageID); + $language = LanguageFactory::getInstance()->getLanguageByCode(in_array(self::$selectedLanguageCode, self::$selectedLanguages) ? self::$selectedLanguageCode : self::$selectedLanguages[0]); + LanguageFactory::getInstance()->makeDefault($language->languageID); // assign all languages to package id 0 $sql = "SELECT languageID diff --git a/wcfsetup/install/files/lib/system/language/LanguageFactory.class.php b/wcfsetup/install/files/lib/system/language/LanguageFactory.class.php index 64517a5504..8be53f8c08 100644 --- a/wcfsetup/install/files/lib/system/language/LanguageFactory.class.php +++ b/wcfsetup/install/files/lib/system/language/LanguageFactory.class.php @@ -3,6 +3,7 @@ namespace wcf\system\language; use wcf\data\language\Language; use wcf\system\cache\CacheHandler; use wcf\system\template\TemplateScriptingCompiler; +use wcf\system\SingletonFactory; use wcf\system\WCF; use wcf\util\StringUtil; @@ -16,24 +17,31 @@ use wcf\util\StringUtil; * @subpackage system.language * @category Community Framework */ -abstract class LanguageFactory { +class LanguageFactory extends SingletonFactory { /** * language cache * @var array */ - private static $cache = null; + protected $cache = null; /** * initialized languages * @var array */ - private static $languages = array(); + protected $languages = array(); /** * active template scripting compiler * @var wcf\system\template\TemplateScriptingCompiler */ - private static $scriptingCompiler = null; + protected $scriptingCompiler = null; + + /** + * @see wcf\system\SingletonFactory::init() + */ + protected function init() { + $this->loadCache(); + } /** * Returns a Language object for the language with the given id. @@ -41,22 +49,20 @@ abstract class LanguageFactory { * @param integer $languageID * @return wcf\data\language\Language */ - public static function getLanguage($languageID) { - self::loadCache(); - - if (!isset(self::$languages[$languageID])) { + public function getLanguage($languageID) { + if (!isset($this->languages[$languageID])) { $language = new Language($languageID); if (!$language->languageID) { - $languageID = self::findPreferredLanguage(); + $languageID = $this->findPreferredLanguage(); $language = new Language($languageID); } - self::$languages[$language->languageID] = $language; - self::setLocale($languageID); + $this->languages[$language->languageID] = $language; + $this->setLocale($languageID); } - return self::$languages[$languageID]; + return $this->languages[$languageID]; } /** @@ -65,10 +71,8 @@ abstract class LanguageFactory { * @param string $categoryName * @return boolean */ - public static function isValidCategory($categoryName) { - self::loadCache(); - - return isset(self::$cache['categories'][$categoryName]); + public function isValidCategory($categoryName) { + return isset($this->cache['categories'][$categoryName]); } /** @@ -77,9 +81,9 @@ abstract class LanguageFactory { * @param string $categoryName * @return wcf\data\language\category\LanguageCategory */ - public static function getCategory($categoryName) { - if (isset(self::$cache['categories'][$categoryName])) { - return self::$cache['categories'][$categoryName]; + public function getCategory($categoryName) { + if (isset($this->cache['categories'][$categoryName])) { + return $this->cache['categories'][$categoryName]; } return null; @@ -90,28 +94,28 @@ abstract class LanguageFactory { * * @return array */ - public static function getCategories() { - return self::$cache['categories']; + public function getCategories() { + return $this->cache['categories']; } /** * Searches the preferred language of the current user. */ - private static function findPreferredLanguage() { + protected function findPreferredLanguage() { // get available language codes $availableLanguageCodes = array(); - foreach (self::getAvailableLanguages(PACKAGE_ID) as $language) { + foreach ($this->getAvailableLanguages(PACKAGE_ID) as $language) { $availableLanguageCodes[] = $language['languageCode']; } // get default language - $defaultLanguageCode = self::$cache['languages'][self::$cache['default']]['languageCode']; + $defaultLanguageCode = $this->cache['languages'][$this->cache['default']]['languageCode']; // get preferred language - $languageCode = self::getPreferredLanguage($availableLanguageCodes, $defaultLanguageCode); + $languageCode = $this->getPreferredLanguage($availableLanguageCodes, $defaultLanguageCode); // get language id of preferred language - foreach (self::$cache['languages'] as $key => $language) { + foreach ($this->cache['languages'] as $key => $language) { if ($language['languageCode'] == $languageCode) { return $key; } @@ -125,12 +129,12 @@ abstract class LanguageFactory { * @param string $defaultLanguageCode * @return string */ - public static function getPreferredLanguage($availableLanguageCodes, $defaultLanguageCode) { + public function getPreferredLanguage($availableLanguageCodes, $defaultLanguageCode) { if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && $_SERVER['HTTP_ACCEPT_LANGUAGE']) { $acceptedLanguages = explode(',', str_replace('_', '-', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']))); foreach ($acceptedLanguages as $acceptedLanguage) { foreach ($availableLanguageCodes as $availableLanguageCode) { - $fixedCode = strtolower(self::fixLanguageCode($availableLanguageCode)); + $fixedCode = strtolower($this->fixLanguageCode($availableLanguageCode)); if ($fixedCode == $acceptedLanguage || $fixedCode == preg_replace('%^([a-z]{2}).*$%i', '$1', $acceptedLanguage)) { return $availableLanguageCode; @@ -149,12 +153,12 @@ abstract class LanguageFactory { * @param integer $packageID * @return array */ - public static function getAvailableLanguages($packageID = PACKAGE_ID) { + public function getAvailableLanguages($packageID = PACKAGE_ID) { // get list of all available languages $availableLanguages = array(); - if (isset(self::$cache['packages'][$packageID])) { - foreach (self::$cache['packages'][$packageID] as $availableLanguageID) { - $availableLanguages[] = self::$cache['languages'][$availableLanguageID]; + if (isset($this->cache['packages'][$packageID])) { + foreach ($this->cache['packages'][$packageID] as $availableLanguageID) { + $availableLanguages[] = $this->cache['languages'][$availableLanguageID]; } } return $availableLanguages; @@ -166,11 +170,9 @@ abstract class LanguageFactory { * @param string $languageCode * @return wcf\data\language\Language */ - public static function getLanguageByCode($languageCode) { - if (self::$cache === null) self::loadCache(); - + public function getLanguageByCode($languageCode) { // called within WCFSetup - if (self::$cache === false || !count(self::$cache['codes'])) { + if ($this->cache === false || !count($this->cache['codes'])) { $sql = "SELECT languageID FROM wcf".WCF_N."_language WHERE languageCode = ?"; @@ -179,8 +181,8 @@ abstract class LanguageFactory { $row = $statement->fetchArray(); if (isset($row['languageID'])) return new Language($row['languageID']); } - else if (isset(self::$cache['codes'][$languageCode])) { - return self::getLanguage(self::$cache['codes'][$languageCode]); + else if (isset($this->cache['codes'][$languageCode])) { + return $this->getLanguage($this->cache['codes'][$languageCode]); } return null; @@ -191,35 +193,31 @@ abstract class LanguageFactory { * * @return wcf\system\template\TemplateScriptingCompiler */ - public static function getScriptingCompiler() { - if (self::$scriptingCompiler === null) { - self::$scriptingCompiler = new TemplateScriptingCompiler(WCF::getTPL()); + public function getScriptingCompiler() { + if ($this->scriptingCompiler === null) { + $this->scriptingCompiler = new TemplateScriptingCompiler(WCF::getTPL()); } - return self::$scriptingCompiler; + return $this->scriptingCompiler; } /** * Loads the language cache. */ - private static function loadCache() { - if (self::$cache === null) { - CacheHandler::getInstance()->addResource( - 'languages', - WCF_DIR.'cache/cache.languages.php', - 'wcf\system\cache\builder\LanguageCacheBuilder' - ); - - self::$cache = CacheHandler::getInstance()->get('languages'); - } + protected function loadCache() { + CacheHandler::getInstance()->addResource( + 'languages', + WCF_DIR.'cache/cache.languages.php', + 'wcf\system\cache\builder\LanguageCacheBuilder' + ); + + $this->cache = CacheHandler::getInstance()->get('languages'); } /** * Clears languages cache. */ - public static function clearCache() { - self::$cache = null; - + public function clearCache() { CacheHandler::getInstance()->clear(WCF_DIR.'cache/', 'cache.languages.php'); } @@ -229,11 +227,11 @@ abstract class LanguageFactory { * * @param integer $languageID */ - private static function setLocale($languageID) { + protected function setLocale($languageID) { // set locale for string comparison, character classification and // conversion and date and time formatting - setlocale(LC_COLLATE, self::$languages[$languageID]->get('wcf.global.locale.unix').'.UTF-8', self::$languages[$languageID]->get('wcf.global.locale.unix'), self::$languages[$languageID]->get('wcf.global.locale.win')); - setlocale(LC_CTYPE, self::$languages[$languageID]->get('wcf.global.locale.unix').'.UTF-8', self::$languages[$languageID]->get('wcf.global.locale.unix'), self::$languages[$languageID]->get('wcf.global.locale.win')); + setlocale(LC_COLLATE, $this->languages[$languageID]->get('wcf.global.locale.unix').'.UTF-8', $this->languages[$languageID]->get('wcf.global.locale.unix'), $this->languages[$languageID]->get('wcf.global.locale.win')); + setlocale(LC_CTYPE, $this->languages[$languageID]->get('wcf.global.locale.unix').'.UTF-8', $this->languages[$languageID]->get('wcf.global.locale.unix'), $this->languages[$languageID]->get('wcf.global.locale.win')); } /** @@ -243,7 +241,7 @@ abstract class LanguageFactory { * @param string $languageCode * @return string $languageCode */ - public static function fixLanguageCode($languageCode) { + public function fixLanguageCode($languageCode) { return preg_replace('/-[a-z0-9]+/', '', $languageCode); } @@ -252,8 +250,8 @@ abstract class LanguageFactory { * * @return integer */ - public static function getDefaultLanguageID() { - return self::$cache['default']; + public function getDefaultLanguageID() { + return $this->cache['default']; } /** @@ -262,12 +260,12 @@ abstract class LanguageFactory { * @param integer $packageID * @return array $availableLanguages infos about each language (code, id, encoding, etc) */ - public static function getAvailableContentLanguages($packageID = PACKAGE_ID) { + public function getAvailableContentLanguages($packageID = PACKAGE_ID) { $availableLanguages = array(); - if (isset(self::$cache['packages'][$packageID])) { - foreach (self::$cache['packages'][$packageID] as $availableLanguageID) { - if (self::$cache['languages'][$availableLanguageID]['hasContent']) { - $availableLanguages[$availableLanguageID] = self::$cache['languages'][$availableLanguageID]; + if (isset($this->cache['packages'][$packageID])) { + foreach ($this->cache['packages'][$packageID] as $availableLanguageID) { + if ($this->cache['languages'][$availableLanguageID]['hasContent']) { + $availableLanguages[$availableLanguageID] = $this->cache['languages'][$availableLanguageID]; } } } @@ -280,7 +278,7 @@ abstract class LanguageFactory { * * @param integer $languageID */ - public static function makeDefault($languageID) { + public function makeDefault($languageID) { // remove old default language $sql = "UPDATE wcf".WCF_N."_language SET isDefault = 0 @@ -296,7 +294,7 @@ abstract class LanguageFactory { $statement->execute(array($languageID)); // rebuild language cache - self::clearCache(); + $this->clearCache(); } /** @@ -305,9 +303,9 @@ abstract class LanguageFactory { * * @return array */ - public static function getLanguages() { + public function getLanguages() { $languages = array(); - foreach (self::$cache['codes'] as $languageCode => $languageID) { + foreach ($this->cache['codes'] as $languageCode => $languageID) { $languages[$languageID] = WCF::getLanguage()->getDynamicVariable('wcf.global.language.'.$languageCode); } @@ -320,9 +318,9 @@ abstract class LanguageFactory { * * @return array */ - public static function getLanguageCodes() { + public function getLanguageCodes() { $languageCodes = array(); - foreach (self::$cache['codes'] as $languageCode => $languageID) { + foreach ($this->cache['codes'] as $languageCode => $languageID) { $languageCodes[$languageID] = $languageCode; }