X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=framework%2Fconfig%2Fclass_FrameworkConfiguration.php;h=2694a537fda03aaa1d1e1eb104b588d4ef17c3ea;hb=c20af2b1b05f92040a8cfabfb1d0c5cd2771f7c5;hp=16ebaf86dcd0d90eebdc46d98125e64468e20bb8;hpb=b79a12c65fa8548095e367b48ec964e0a7a692a2;p=core.git diff --git a/framework/config/class_FrameworkConfiguration.php b/framework/config/class_FrameworkConfiguration.php index 16ebaf86..2694a537 100644 --- a/framework/config/class_FrameworkConfiguration.php +++ b/framework/config/class_FrameworkConfiguration.php @@ -1,22 +1,21 @@ * @version 1.0.1 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * @@ -42,35 +41,30 @@ use \InvalidArgumentException; * along with this program. If not, see . */ class FrameworkConfiguration implements Registerable { + // Some constants for the configuration system + const EXCEPTION_CONFIG_KEY_IS_EMPTY = 0x130; + const EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND = 0x131; + const EXCEPTION_CONFIG_VALUE_TYPE_UNSUPPORTED = 0x132; /** * The framework's main configuration array which will be initialized with * hard-coded configuration data and might be overwritten/extended by * config data from the database. */ - private $config = array(); - - /** - * The configuration instance itself - */ - private static $configInstance = NULL; + private static $configData = []; /** * Call-back instance (unused) */ private $callbackInstance = NULL; - // Some constants for the configuration system - const EXCEPTION_CONFIG_KEY_IS_EMPTY = 0x130; - const EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND = 0x131; - const EXCEPTION_CONFIG_VALUE_TYPE_UNSUPPORTED = 0x132; - /** - * Private constructor + * Default constructor, the configuration entries are static, not the + * whole instance. * * @return void */ - private function __construct () { + public function __construct () { // Empty for now } @@ -83,106 +77,26 @@ class FrameworkConfiguration implements Registerable { return get_class($this); } - /** - * Getter for a singleton instance of this class - * - * @return $configInstance A singleton instance of this class - */ - public static final function getSelfInstance () { - // is the instance there? - if (is_null(self::$configInstance)) { - // Create a config instance - self::$configInstance = new FrameworkConfiguration(); - } // END - if - - // Return singleton instance - return self::$configInstance; - } - - /** - * Converts dashes to underscores, e.g. useable for configuration entries - * - * @param $str The string with maybe dashes inside - * @return $str The converted string with no dashed, but underscores - * @throws NullPointerException If $str is null - * @throws InvalidArgumentException If $str is empty - */ - private final function convertDashesToUnderscores ($str) { - // Is it null? - if (is_null($str)) { - // Throw NPE - throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER); - } elseif (empty($str)) { - // Entry is empty - throw new InvalidArgumentException('str is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY); - } - - // Convert them all - $str = str_replace('-', '_', $str); - - // Return converted string - return $str; - } - - /** - * Setter for default time zone (must be correct!) - * - * @param $timezone The timezone string (e.g. Europe/Berlin) - * @return $success If timezone was accepted - * @throws NullPointerException If $timezone is NULL - * @throws InvalidArgumentException If $timezone is empty - */ - public final function setDefaultTimezone ($timezone) { - // Is it null? - if (is_null($timezone)) { - // Throw NPE - throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER); - } elseif (!is_string($timezone)) { - // Is not a string - throw new InvalidArgumentException(sprintf('timezone[]=%s is not a string', gettype($timezone)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY); - } elseif (empty($timezone)) { - // Entry is empty - throw new InvalidArgumentException('timezone is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY); - } - - // Default success - $success = FALSE; - - /* - * Set desired time zone to prevent date() and related functions to - * issue an E_WARNING. - */ - $success = date_default_timezone_set($timezone); - - // Return status - return $success; - } - /** * Checks whether the given configuration key is set * * @param $configKey The configuration key we shall check * @return $isset Whether the given configuration key is set - * @throws NullPointerException If $configKey is NULL * @throws InvalidArgumentException If $configKey is empty */ - public function isConfigurationEntrySet ($configKey) { + public function isConfigurationEntrySet (string $configKey) { // Is it null? - if (is_null($configKey)) { - // Throw NPE - throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER); - } elseif (!is_string($configKey)) { - // Is not a string - throw new InvalidArgumentException(sprintf('configKey[]=%s is not a string', gettype($configKey)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY); - } elseif (empty($configKey)) { + //* NOISY-DEBUG: */ printf('[%s:%d]: configKey=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $configKey); + if (empty($configKey)) { // Entry is empty - throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY); + throw new InvalidArgumentException('Parameter "configKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } // Is it set? - $isset = ((isset($this->config[$configKey])) || (array_key_exists($configKey, $this->config))); + $isset = ((isset(self::$configData[$configKey])) || (array_key_exists($configKey, self::$configData))); // Return the result + //* NOISY-DEBUG: */ printf('[%s:%d]: isset=%s - EXIT!' . PHP_EOL, __METHOD__, __LINE__, intval($isset)); return $isset; } @@ -191,34 +105,30 @@ class FrameworkConfiguration implements Registerable { * * @param $configKey The configuration element * @return $configValue The fetched configuration value - * @throws NullPointerException If $configKey is NULL * @throws InvalidArgumentException If $configKey is empty * @throws NoConfigEntryException If a configuration element was not found */ - public function getConfigEntry ($configKey) { + public function getConfigEntry (string $configKey) { // Is it null? - if (is_null($configKey)) { - // Throw NPE - throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER); - } elseif (!is_string($configKey)) { - // Is not a string - throw new InvalidArgumentException(sprintf('configKey[]=%s is not a string', gettype($configKey)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY); - } elseif (empty($configKey)) { + //* NOISY-DEBUG: */ printf('[%s:%d]: configKey=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $configKey); + if (empty($configKey)) { // Entry is empty - throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY); + throw new InvalidArgumentException('Parameter "configKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } // Convert dashes to underscore - $configKey = self::convertDashesToUnderscores($configKey); + $configKey = StringUtils::convertDashesToUnderscores($configKey); // Is a valid configuration key provided? + //* NOISY-DEBUG: */ printf('[%s:%d]: configKey=%s - AFTER!' . PHP_EOL, __METHOD__, __LINE__, $configKey); if (!$this->isConfigurationEntrySet($configKey)) { // Entry was not found! throw new NoConfigEntryException(array(__CLASS__, $configKey), self::EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND); - } // END - if + } // Return the requested value - return $this->config[$configKey]; + //* NOISY-DEBUG: */ printf('[%s:%d]: Returning configData[%s]=[%s]:%s - EXIT!' . PHP_EOL, __METHOD__, __LINE__, $configKey, gettype(self::$configData[$configKey]), self::$configData[$configKey]); + return self::$configData[$configKey]; } /** @@ -227,35 +137,26 @@ class FrameworkConfiguration implements Registerable { * @param $configKey The configuration key we want to add/change * @param $configValue The configuration value we want to set * @return void - * @throws NullPointerException If $configKey is NULL * @throws InvalidArgumentException If $configKey is empty * @throws InvalidArgumentException If $configValue has an unsupported variable type */ - public final function setConfigEntry ($configKey, $configValue) { + public final function setConfigEntry (string $configKey, $configValue) { // Is a valid configuration key key provided? - if (is_null($configKey)) { - // Configuration key is null - throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER); - } elseif (!is_string($configKey)) { - // Is not a string - throw new InvalidArgumentException(sprintf('configKey[]=%s is not a string', gettype($configKey)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY); - } elseif (empty($configKey)) { + //* NOISY-DEBUG: */ printf('[%s:%d]: configKey=%s,configValue[]=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $configKey, gettype($configValue)); + if (empty($configKey)) { // Entry is empty - throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY); + throw new InvalidArgumentException('Parameter "configKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } elseif ((is_array($configValue)) || (is_object($configValue)) || (is_resource($configValue))) { // These cannot be set as this is not intended for configuration values, please use FrameworkArrayObject instead. throw new InvalidArgumentException(sprintf('configValue[]=%s for configKey=%s is not supported.', gettype($configValue), $configKey), self::EXCEPTION_CONFIG_VALUE_TYPE_UNSUPPORTED); } // Cast to string - $configKey = self::convertDashesToUnderscores($configKey); + $configKey = StringUtils::convertDashesToUnderscores($configKey); // Set the configuration value - //* NOISY-DEBUG: */ print(__METHOD__ . ':configEntry=' . $configKey . ',configValue[' . gettype($configValue) . ']=' . $configValue . PHP_EOL); - $this->config[$configKey] = $configValue; - - // Resort the array - ksort($this->config); + //* NOISY-DEBUG: */ printf('[%s:%d]: Setting configKey=%s,configValue[%s]=%s - EXIT!' . PHP_EOL, __METHOD__, __LINE__, $configKey, gettype($configValue), $configValue); + self::$configData[$configKey] = $configValue; } /** @@ -265,7 +166,24 @@ class FrameworkConfiguration implements Registerable { */ public final function getConfigurationArray () { // Return it - return $this->config; + //* NOISY-DEBUG: */ printf('[%s:%d]: self::configData()=%d - EXIT!' . PHP_EOL, __METHOD__, __LINE__, count(self::$configData)); + return self::$configData; + } + + /** + * Sorts the configuration array, saves A LOT calls if done after all configuration files have been loaded. You should NOT + * set any configuration entries by your own, means outside any configuration file. If you still do so, you HAVE to call + * this method afterwards + * + * @return void + */ + public final function sortConfigurationArray () { + // Resort the array + //* NOISY-DEBUG: */ printf('[%s:%d]: Sorting %d records - CALLED!' . PHP_EOL, __METHOD__, __LINE__, count(self::$configData)); + ksort(self::$configData); + + // Debug message + //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__); } /** @@ -274,222 +192,68 @@ class FrameworkConfiguration implements Registerable { * * @param $configKey Configuration key to unset * @return void - * @throws NullPointerException If $configKey is NULL * @throws InvalidArgumentException If $configKey is empty * @throws NoConfigEntryException If a configuration element was not found */ - public final function unsetConfigEntry ($configKey) { + public final function unsetConfigEntry (string $configKey) { // Validate parameters - if (is_null($configKey)) { - // Configuration key is null - throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER); - } elseif (!is_string($configKey)) { + //* NOISY-DEBUG: */ printf('[%s:%d]: configKey=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $configKey); + if (empty($configKey)) { // Entry is empty - throw new InvalidArgumentException(sprintf('configKey[]=%s is not a string', gettype($configKey)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY); - } elseif (empty($configKey)) { - // Entry is empty - throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY); + throw new InvalidArgumentException('Parameter "configKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } // Convert dashes to underscore - $configKey = self::convertDashesToUnderscores($configKey); + $configKey = StringUtils::convertDashesToUnderscores($configKey); // Is the configuration key there? + //* NOISY-DEBUG: */ printf('[%s:%d]: configKey=%s - AFTER!' . PHP_EOL, __METHOD__, __LINE__, $configKey); if (!$this->isConfigurationEntrySet($configKey)) { // Entry was not found! throw new NoConfigEntryException(array(__CLASS__, $configKey), self::EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND); - } // END - if + } // Unset it - unset($this->config[$configKey]); - } + //* NOISY-DEBUG: */ printf('[%s:%d]: Unsetting configKey=%s ...' . PHP_EOL, __METHOD__, __LINE__, $configKey); + unset(self::$configData[$configKey]); - /** - * Detects the server address (SERVER_ADDR) and set it in configuration - * - * @return $serverAddress The detected server address - * @throws UnknownHostnameException If SERVER_NAME cannot be resolved to an IP address - * @todo Have to check some more entries from $_SERVER here - */ - public function detectServerAddress () { - // Is the entry set? - if (!$this->isConfigurationEntrySet('server_addr')) { - // Is it set in $_SERVER? - if (isset($_SERVER['SERVER_ADDR'])) { - // Set it from $_SERVER - $this->setServerAddress($_SERVER['SERVER_ADDR']); - } elseif (isset($_SERVER['SERVER_NAME'])) { - // Resolve IP address - $serverIp = ConsoleTools::resolveIpAddress($_SERVER['SERVER_NAME']); - - // Is it valid? - if ($serverIp === false) { - /* - * Why is gethostbyname() returning the host name and not - * false as many other PHP functions are doing? ;-( - */ - throw new UnknownHostnameException(sprintf('Cannot resolve "%s" to an IP address. Please fix your setup.', $_SERVER['SERVER_NAME'])); - } // END - if - - // Al fine, set it - $this->setServerAddress($serverIp); - } else { - // Run auto-detecting through console tools lib - ConsoleTools::acquireSelfIpAddress(); - } - } // END - if - - // Now get it from configuration - $serverAddress = $this->getServerAddress(); - - // Return it - return $serverAddress; + // Debug message + //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__); } /** - * Setter for SERVER_ADDR + * Checks if a configuration entry is_*_enabled set to 'Y' * - * @param $serverAddress New SERVER_ADDR value to set - * @return void + * @param $keyPart Configuration to expand with is_$keyPart_enabled + * @return $enabled Whether it has been set to Y or N + * @throws InvalidArgumentException If a parameter is invalid + * @throws UnexpectedValueException If a returned value is of an unexpected type or value */ - public function setServerAddress ($serverAddress) { - // Is a valid configuration key key provided? - if (is_null($serverAddress)) { - // Configuration key is null - throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER); - } elseif (!is_string($serverAddress)) { - // Is not a string - throw new InvalidArgumentException(sprintf('serverAddress[]=%s is not a string', gettype($serverAddress)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY); - } elseif (empty($serverAddress)) { + public function isEnabled (string $keyPart) { + // Validate parameters + //* NOISY-DEBUG: */ printf('[%s:%d]: keyPart=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $keyPart); + if (empty($keyPart)) { // Entry is empty - throw new InvalidArgumentException('serverAddress is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY); + throw new InvalidArgumentException('Parameter "keyPart" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } - // Set it, please don't do it yourself here - $this->setConfigEntry('server_addr', (string) $serverAddress); - } - - /** - * Getter for SERVER_ADDR - * - * @return $serverAddress New SERVER_ADDR value to set - */ - public function getServerAddress () { - return $this->getConfigEntry('server_addr'); - } - - /** - * Detects the HTTPS flag - * - * @return $https The detected HTTPS flag or null if failed - */ - public function detectHttpSecured () { - // Default is null - $https = NULL; + // Construct final config key + $configKey = sprintf('is_%s_enabled', $keyPart); - // Is HTTPS set? - if ($this->isHttpSecured()) { - // Then use it - $https = $_SERVER['HTTPS']; - } // END - if + // Get value from it + //* NOISY-DEBUG: */ printf('[%s:%d]: configKey=%s' . PHP_EOL, __METHOD__, __LINE__, $configKey); + $isEnabled = $this->getConfigEntry($configKey); - // Return it - return $https; - } - - /** - * Checks whether HTTPS is set in $_SERVER - * - * @return $isset Whether HTTPS is set - * @todo Test more fields - */ - public function isHttpSecured () { - return (isset($_SERVER['HTTPS'])); - } - - /** - * Dectect and return the base URL for all URLs and forms - * - * @return $baseUrl Detected base URL - */ - public function detectBaseUrl () { - // Initialize the URL - $protocol = 'http'; - - // Do we have HTTPS? - if ($this->isHttpSecured()) { - // Add the >s< for HTTPS - $protocol = 's'; - } // END - if - - // Construct the full URL and secure it against CSRF attacks - $baseUrl = $protocol . '://' . $this->detectDomain() . $this->detectScriptPath(); - - // Return the URL - return $baseUrl; - } - - /** - * Detect safely and return the full domain where this script is installed - * - * @return $fullDomain The detected full domain - */ - public function detectDomain () { - // Full domain is localnet.invalid by default - $fullDomain = 'localnet.invalid'; - - // Is the server name there? - if (isset($_SERVER['SERVER_NAME'])) { - // Detect the full domain - $fullDomain = htmlentities(strip_tags($_SERVER['SERVER_NAME']), ENT_QUOTES); - } // END - if - - // Return it - return $fullDomain; - } - - /** - * Detect safely the script path without trailing slash which is the glue - * between "http://your-domain.invalid/" and "script-name.php" - * - * @return $scriptPath The script path extracted from $_SERVER['SCRIPT_NAME'] - */ - public function detectScriptPath () { - // Default is empty - $scriptPath = ''; - - // Is the scriptname set? - if (isset($_SERVER['SCRIPT_NAME'])) { - // Get dirname from it and replace back-slashes with slashes for lame OSes... - $scriptPath = str_replace("\\", '/', dirname($_SERVER['SCRIPT_NAME'])); - } // END - if + // Is it Y/N? + //* NOISY-DEBUG: */ printf('[%s:%d]: isEnabled[]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype($isEnabled)); + if (!is_bool($isEnabled)) { + // Throw exception + throw new UnexpectedValueException(sprintf('isEnabled[]=%s is unexpected', gettype($isEnabled))); + } // Return it - return $scriptPath; - } - - /** - * Getter for field name - * - * @param $fieldName Field name which we shall get - * @return $fieldValue Field value from the user - * @throws NullPointerException If the result instance is null - */ - public final function getField ($fieldName) { - // The super interface "FrameworkInterface" requires this - throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); - } - - /** - * Checks if given field is set - * - * @param $fieldName Field name to check - * @return $isSet Whether the given field name is set - * @throws NullPointerException If the result instance is null - */ - public function isFieldSet ($fieldName) { - // The super interface "FrameworkInterface" requires this - throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + //* NOISY-DEBUG: */ printf('[%s:%d]: isEnabled=%d - EXIT!' . PHP_EOL, __METHOD__, __LINE__, intval($isEnabled)); + return $isEnabled; } /** @@ -530,4 +294,28 @@ class FrameworkConfiguration implements Registerable { $this->callbackInstance = $callbackInstance; } + /** + * Getter for field name + * + * @param $fieldName Field name which we shall get + * @return $fieldValue Field value from the user + * @throws NullPointerException If the result instance is null + */ + public final function getField (string $fieldName) { + // The super interface "FrameworkInterface" requires this + throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Checks if given field is set + * + * @param $fieldName Field name to check + * @return $isSet Whether the given field name is set + * @throws NullPointerException If the result instance is null + */ + public function isFieldSet (string $fieldName) { + // The super interface "FrameworkInterface" requires this + throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION); + } + }