From 7511f841d61dd745539a04da81b75184ea62456f Mon Sep 17 00:00:00 2001 From: j3nsch Date: Wed, 30 Oct 2024 16:13:41 +0100 Subject: [PATCH] #2 Moved Configuration class --- src/Configuration.php | 322 ++++++++++++++++++++++++++++++++++++++++++ src/Exception.php | 61 ++++++++ 2 files changed, 383 insertions(+) create mode 100644 src/Configuration.php create mode 100644 src/Exception.php diff --git a/src/Configuration.php b/src/Configuration.php new file mode 100644 index 0000000..7130c2c --- /dev/null +++ b/src/Configuration.php @@ -0,0 +1,322 @@ +getConfig(); + + if (isset($config->name)) { + $name = $config->name; + } else { + $name = 'OPUS 4'; + } + + return $name; + } + + /** + * Liefert die Sprachen, die von OPUS unterstützt werden. + * + * @return array + */ + public function getSupportedLanguages() + { + if ($this->supportedLanguages === null) { + $config = $this->getConfig(); + if (isset($config->supportedLanguages)) { + $this->supportedLanguages = array_map('trim', explode(',', $config->supportedLanguages)); + /* TODO only used for debugging - remove? + $this->getLogger()->debug( + Zend_Debug::dump( + $this->_supportedLanguages, 'Supported languages (' + . count($this->_supportedLanguages) . ')', false + ) + );*/ + } + } + return $this->supportedLanguages; + } + + /** + * @return string[] + */ + public function getActivatedLanguages() + { + if ($this->activatedLanguages === null) { + $config = $this->getConfig(); + if (isset($config->activatedLanguages)) { + $this->activatedLanguages = explode(',', $config->activatedLanguages); + } else { + return $this->getSupportedLanguages(); + } + } + + return $this->activatedLanguages; + } + + /** + * Prüft, ob eine Sprache unterstützt wird. + * + * @param string $language Sprachcode (z.B. 'en') + * @return bool + */ + public function isLanguageSupported($language) + { + $languages = $this->getSupportedLanguages(); + return in_array($language, $languages); + } + + /** + * Liefert Defaultsprache für Userinterface. + * + * @return string + */ + public function getDefaultLanguage() + { + if ($this->defaultLanguage === null) { + $languages = $this->getSupportedLanguages(); + $this->defaultLanguage = $languages[0]; + + if ($this->isLanguageSelectionEnabled()) { + $locale = new Zend_Locale(); + $language = $locale->getDefault(); + if (is_array($language) && count($language) > 0) { + reset($language); + $language = key($language); + } else { + $language = self::DEFAULT_LANGUAGE; + } + + if ($this->isLanguageSupported($language)) { + $this->defaultLanguage = $language; + } + } + } + + return $this->defaultLanguage; + } + + /** + * Prüft, ob mehr als eine Sprache unterstützt wird. + * + * @return bool + */ + public function isLanguageSelectionEnabled() + { + if ($this->languageSelectionEnabled === null) { + $this->languageSelectionEnabled = count($this->getSupportedLanguages()) > 1; + } + return $this->languageSelectionEnabled; + } + + /** + * Returns path to files folder for document files. + * + * @return string Folder for storing document files + * @throws ApplicationException + */ + public function getFilesPath() + { + return $this->getWorkspacePath() . 'files' . DIRECTORY_SEPARATOR; + } + + /** + * Returns path to files folder for cached document files. + * + * @return string Folder for storing cached document files + * @throws ApplicationException + */ + public function getFilecachePath() + { + return $this->getWorkspacePath() . 'filecache' . DIRECTORY_SEPARATOR; + } + + /** + * Liest Inhalt von VERSION.txt um die installierte Opusversion zu ermitteln. + * + * @return string + */ + public static function getOpusVersion() + { + $config = Config::get(); + $localVersion = $config->version; + return $localVersion ?? 'unknown'; + } + + /** + * Liefert Informationen als Key -> Value Paare in einem Array. + * + * @return array + */ + public static function getOpusInfo() + { + return []; + } + + /** + * Saves configuration as XML file. + * + * @param Zend_Config $update + * @throws Zend_Config_Exception + */ + public static function save($update) + { + $config = self::load(); + + $config->merge($update); + + $writer = new Zend_Config_Writer_Xml(); + $writer->write(APPLICATION_PATH . '/application/configs/config.xml', $config); + } + + /** + * @return Zend_Config_Xml + * @throws Zend_Config_Exception + */ + public static function load() + { + return new Zend_Config_Xml(APPLICATION_PATH . '/application/configs/config.xml', null, true); + } + + /** + * Returns value for key in current configuration. + * + * @param string $key Name of option + * @return string + */ + public function getValue($key) + { + return $this->getValueFromConfig($this->getConfig(), $key); + } + + /** + * Removes instance. + * + * This is used to reset the configuration to defaults in ini files. + */ + public static function clearInstance() + { + self::$instance = null; + } + + /** + * Returns Zend_Translate instance for application. + * + * @return Zend_Translate + * @throws Zend_Exception + */ + public function getTranslate() + { + return Application_Translate::getInstance(); + } + + /** + * @return bool + */ + public static function isUpdateInProgress() + { + $config = Config::get(); + + return isset($config->updateInProgress) && filter_var($config->updateInProgress, FILTER_VALIDATE_BOOLEAN); + } +} diff --git a/src/Exception.php b/src/Exception.php new file mode 100644 index 0000000..446110b --- /dev/null +++ b/src/Exception.php @@ -0,0 +1,61 @@ +httpResponseCode = $code; + return $this; + } + + /** + * @return int + */ + public function getHttpResponseCode() + { + return $this->httpResponseCode; + } +}