]> git.mxchange.org Git - core.git/blobdiff - framework/config/class_FrameworkConfiguration.php
Continued:
[core.git] / framework / config / class_FrameworkConfiguration.php
index 16ebaf86dcd0d90eebdc46d98125e64468e20bb8..be9d7d6dc137c73a39f464a1d14873efc3f638b8 100644 (file)
@@ -1,16 +1,14 @@
 <?php
 
 // Own namespace
-namespace CoreFramework\Configuration;
+namespace Org\Mxchange\CoreFramework\Configuration;
 
 // Import framework stuff
-use CoreFramework\Console\Tools\ConsoleTools;
-use CoreFramework\Dns\UnknownHostnameException;
-use CoreFramework\Generic\FrameworkInterface;
-use CoreFramework\Generic\NullPointerException;
-use CoreFramework\Generic\UnsupportedOperationException;
-use CoreFramework\Object\BaseFrameworkSystem;
-use CoreFramework\Registry\Registerable;
+use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
+use Org\Mxchange\CoreFramework\Generic\NullPointerException;
+use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
+use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
+use Org\Mxchange\CoreFramework\Registry\Registerable;
 
 // Import SPL stuff
 use \InvalidArgumentException;
@@ -48,12 +46,7 @@ class FrameworkConfiguration implements Registerable {
         * 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 $config = array();
 
        /**
         * Call-back instance (unused)
@@ -66,11 +59,12 @@ class FrameworkConfiguration implements Registerable {
        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,81 +77,6 @@ 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
         *
@@ -174,13 +93,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);
                }
 
                // Is it set?
-               $isset = ((isset($this->config[$configKey])) || (array_key_exists($configKey, $this->config)));
+               $isset = ((isset(self::$config[$configKey])) || (array_key_exists($configKey, self::$config)));
 
                // Return the result
                return $isset;
@@ -203,13 +122,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)) {
@@ -218,7 +137,7 @@ class FrameworkConfiguration implements Registerable {
                } // END - if
 
                // Return the requested value
-               return $this->config[$configKey];
+               return self::$config[$configKey];
        }
 
        /**
@@ -239,7 +158,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,14 +167,14 @@ 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);
-               $this->config[$configKey] = $configValue;
+               self::$config[$configKey] = $configValue;
 
                // Resort the array
-               ksort($this->config);
+               ksort(self::$config);
        }
 
        /**
@@ -265,7 +184,7 @@ class FrameworkConfiguration implements Registerable {
         */
        public final function getConfigurationArray () {
                // Return it
-               return $this->config;
+               return self::$config;
        }
 
        /**
@@ -286,13 +205,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)) {
@@ -301,171 +220,7 @@ class FrameworkConfiguration implements Registerable {
                } // END - if
 
                // Unset it
-               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;
+               unset(self::$config[$configKey]);
        }
 
        /**
@@ -477,7 +232,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 +244,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);
        }
 
        /**