Rewrote a bit:
[core.git] / framework / config / class_FrameworkConfiguration.php
index 16ebaf86dcd0d90eebdc46d98125e64468e20bb8..82d50bf964606d4882a619806f11fa96178122fc 100644 (file)
@@ -99,65 +99,6 @@ class FrameworkConfiguration implements Registerable {
                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
         *
@@ -174,7 +115,7 @@ class FrameworkConfiguration implements Registerable {
                } 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)) {
+               } elseif ((is_string($configKey)) && (empty($configKey))) {
                        // Entry is empty
                        throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
                }
@@ -203,13 +144,13 @@ class FrameworkConfiguration implements Registerable {
                } 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)) {
+               } elseif ((is_string($configKey)) && (empty($configKey))) {
                        // Entry is empty
                        throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
                }
 
                // Convert dashes to underscore
-               $configKey = self::convertDashesToUnderscores($configKey);
+               $configKey = BaseFrameworkSystem::convertDashesToUnderscores($configKey);
 
                // Is a valid configuration key provided?
                if (!$this->isConfigurationEntrySet($configKey)) {
@@ -239,7 +180,7 @@ class FrameworkConfiguration implements Registerable {
                } 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)) {
+               } elseif ((is_string($configKey)) && (empty($configKey))) {
                        // Entry is empty
                        throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
                } elseif ((is_array($configValue)) || (is_object($configValue)) || (is_resource($configValue))) {
@@ -248,7 +189,7 @@ class FrameworkConfiguration implements Registerable {
                }
 
                // Cast to string
-               $configKey = self::convertDashesToUnderscores($configKey);
+               $configKey = BaseFrameworkSystem::convertDashesToUnderscores($configKey);
 
                // Set the configuration value
                //* NOISY-DEBUG: */ print(__METHOD__ . ':configEntry=' . $configKey . ',configValue[' . gettype($configValue) . ']=' . $configValue . PHP_EOL);
@@ -286,13 +227,13 @@ class FrameworkConfiguration implements Registerable {
                } elseif (!is_string($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)) {
+               } elseif ((is_string($configKey)) && (empty($configKey))) {
                        // Entry is empty
                        throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
                }
 
                // Convert dashes to underscore
-               $configKey = self::convertDashesToUnderscores($configKey);
+               $configKey = BaseFrameworkSystem::convertDashesToUnderscores($configKey);
 
                // Is the configuration key there?
                if (!$this->isConfigurationEntrySet($configKey)) {
@@ -304,170 +245,6 @@ class FrameworkConfiguration implements Registerable {
                unset($this->config[$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;
-       }
-
-       /**
-        * Setter for SERVER_ADDR
-        *
-        * @param       $serverAddress  New SERVER_ADDR value to set
-        * @return      void
-        */
-       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)) {
-                       // Entry is empty
-                       throw new InvalidArgumentException('serverAddress is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
-               }
-
-               // 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;
-
-               // Is HTTPS set?
-               if ($this->isHttpSecured()) {
-                       // Then use it
-                       $https = $_SERVER['HTTPS'];
-               } // END - if
-
-               // 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
-
-               // Return it
-               return $scriptPath;
-       }
-
        /**
         * Getter for field name
         *
@@ -477,7 +254,7 @@ class FrameworkConfiguration implements Registerable {
         */
        public final function getField ($fieldName) {
                // The super interface "FrameworkInterface" requires this
-               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
+               throw new UnsupportedOperationException(array($this, __FUNCTION__), BaseFrameworkSystem::EXCEPTION_UNSPPORTED_OPERATION);
        }
 
        /**
@@ -489,7 +266,7 @@ class FrameworkConfiguration implements Registerable {
         */
        public function isFieldSet ($fieldName) {
                // The super interface "FrameworkInterface" requires this
-               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
+               throw new UnsupportedOperationException(array($this, __FUNCTION__), BaseFrameworkSystem::EXCEPTION_UNSPPORTED_OPERATION);
        }
 
        /**