Continued:
[core.git] / framework / config / class_FrameworkConfiguration.php
1 <?php
2
3 // Own namespace
4 namespace Org\Mxchange\CoreFramework\Configuration;
5
6 // Import framework stuff
7 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
8 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
9 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
10 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
11 use Org\Mxchange\CoreFramework\Registry\Registerable;
12
13 // Import SPL stuff
14 use \InvalidArgumentException;
15
16 /**
17  * A class for the configuration stuff implemented in a singleton design paddern
18  *
19  * NOTE: We cannot put this in framework/main/ because it would be loaded (again) in
20  * class loader. See framework/loader/class_ClassLoader.php for instance
21  *
22  * @see                 ClassLoader
23  * @author              Roland Haeder <webmaster@shipsimu.org>
24  * @version             1.0.1
25  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
26  * @license             GNU GPL 3.0 or any newer version
27  * @link                http://www.shipsimu.org
28  *
29  * This program is free software: you can redistribute it and/or modify
30  * it under the terms of the GNU General Public License as published by
31  * the Free Software Foundation, either version 3 of the License, or
32  * (at your option) any later version.
33  *
34  * This program is distributed in the hope that it will be useful,
35  * but WITHOUT ANY WARRANTY; without even the implied warranty of
36  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37  * GNU General Public License for more details.
38  *
39  * You should have received a copy of the GNU General Public License
40  * along with this program. If not, see <http://www.gnu.org/licenses/>.
41  */
42 class FrameworkConfiguration implements Registerable {
43
44         /**
45          * The framework's main configuration array which will be initialized with
46          * hard-coded configuration data and might be overwritten/extended by
47          * config data from the database.
48          */
49         private static $config = array();
50
51         /**
52          * Call-back instance (unused)
53          */
54         private $callbackInstance = NULL;
55
56         // Some constants for the configuration system
57         const EXCEPTION_CONFIG_KEY_IS_EMPTY = 0x130;
58         const EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND = 0x131;
59         const EXCEPTION_CONFIG_VALUE_TYPE_UNSUPPORTED = 0x132;
60
61         /**
62          * Default constructor, the configuration entries are static, not the
63          * whole instance.
64          *
65          * @return      void
66          */
67         public function __construct () {
68                 // Empty for now
69         }
70
71         /**
72          * Compatiblity method to return this class' name
73          *
74          * @return      __CLASS__       This class' name
75          */
76         public function __toString () {
77                 return get_class($this);
78         }
79
80         /**
81          * Checks whether the given configuration key is set
82          *
83          * @param       $configKey      The configuration key we shall check
84          * @return      $isset  Whether the given configuration key is set
85          * @throws      NullPointerException    If $configKey is NULL
86          * @throws      InvalidArgumentException        If $configKey is empty
87          */
88         public function isConfigurationEntrySet ($configKey) {
89                 // Is it null?
90                 if (is_null($configKey)) {
91                         // Throw NPE
92                         throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER);
93                 } elseif (!is_string($configKey)) {
94                         // Is not a string
95                         throw new InvalidArgumentException(sprintf('configKey[]=%s is not a string', gettype($configKey)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
96                 } elseif ((is_string($configKey)) && (empty($configKey))) {
97                         // Entry is empty
98                         throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
99                 }
100
101                 // Is it set?
102                 $isset = ((isset(self::$config[$configKey])) || (array_key_exists($configKey, self::$config)));
103
104                 // Return the result
105                 return $isset;
106         }
107
108         /**
109          * Read a configuration element.
110          *
111          * @param       $configKey              The configuration element
112          * @return      $configValue    The fetched configuration value
113          * @throws      NullPointerException    If $configKey is NULL
114          * @throws      InvalidArgumentException        If $configKey is empty
115          * @throws      NoConfigEntryException          If a configuration element was not found
116          */
117         public function getConfigEntry ($configKey) {
118                 // Is it null?
119                 if (is_null($configKey)) {
120                         // Throw NPE
121                         throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER);
122                 } elseif (!is_string($configKey)) {
123                         // Is not a string
124                         throw new InvalidArgumentException(sprintf('configKey[]=%s is not a string', gettype($configKey)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
125                 } elseif ((is_string($configKey)) && (empty($configKey))) {
126                         // Entry is empty
127                         throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
128                 }
129
130                 // Convert dashes to underscore
131                 $configKey = BaseFrameworkSystem::convertDashesToUnderscores($configKey);
132
133                 // Is a valid configuration key provided?
134                 if (!$this->isConfigurationEntrySet($configKey)) {
135                         // Entry was not found!
136                         throw new NoConfigEntryException(array(__CLASS__, $configKey), self::EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND);
137                 } // END - if
138
139                 // Return the requested value
140                 return self::$config[$configKey];
141         }
142
143         /**
144          * Set a configuration key
145          *
146          * @param       $configKey      The configuration key we want to add/change
147          * @param       $configValue    The configuration value we want to set
148          * @return      void
149          * @throws      NullPointerException    If $configKey is NULL
150          * @throws      InvalidArgumentException        If $configKey is empty
151          * @throws      InvalidArgumentException        If $configValue has an unsupported variable type
152          */
153         public final function setConfigEntry ($configKey, $configValue) {
154                 // Is a valid configuration key key provided?
155                 if (is_null($configKey)) {
156                         // Configuration key is null
157                         throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER);
158                 } elseif (!is_string($configKey)) {
159                         // Is not a string
160                         throw new InvalidArgumentException(sprintf('configKey[]=%s is not a string', gettype($configKey)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
161                 } elseif ((is_string($configKey)) && (empty($configKey))) {
162                         // Entry is empty
163                         throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
164                 } elseif ((is_array($configValue)) || (is_object($configValue)) || (is_resource($configValue))) {
165                         // These cannot be set as this is not intended for configuration values, please use FrameworkArrayObject instead.
166                         throw new InvalidArgumentException(sprintf('configValue[]=%s for configKey=%s is not supported.', gettype($configValue), $configKey), self::EXCEPTION_CONFIG_VALUE_TYPE_UNSUPPORTED);
167                 }
168
169                 // Cast to string
170                 $configKey = BaseFrameworkSystem::convertDashesToUnderscores($configKey);
171
172                 // Set the configuration value
173                 //* NOISY-DEBUG: */ print(__METHOD__ . ':configEntry=' . $configKey . ',configValue[' . gettype($configValue) . ']=' . $configValue . PHP_EOL);
174                 self::$config[$configKey] = $configValue;
175
176                 // Resort the array
177                 ksort(self::$config);
178         }
179
180         /**
181          * Getter for whole configuration array
182          *
183          * @return      $config         Configuration array
184          */
185         public final function getConfigurationArray () {
186                 // Return it
187                 return self::$config;
188         }
189
190         /**
191          * Unset a configuration key, the entry must be there or else an
192          * exception is thrown.
193          *
194          * @param       $configKey      Configuration key to unset
195          * @return      void
196          * @throws      NullPointerException    If $configKey is NULL
197          * @throws      InvalidArgumentException        If $configKey is empty
198          * @throws      NoConfigEntryException  If a configuration element was not found
199          */
200         public final function unsetConfigEntry ($configKey) {
201                 // Validate parameters
202                 if (is_null($configKey)) {
203                         // Configuration key is null
204                         throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER);
205                 } elseif (!is_string($configKey)) {
206                         // Entry is empty
207                         throw new InvalidArgumentException(sprintf('configKey[]=%s is not a string', gettype($configKey)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
208                 } elseif ((is_string($configKey)) && (empty($configKey))) {
209                         // Entry is empty
210                         throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
211                 }
212
213                 // Convert dashes to underscore
214                 $configKey = BaseFrameworkSystem::convertDashesToUnderscores($configKey);
215
216                 // Is the configuration key there?
217                 if (!$this->isConfigurationEntrySet($configKey)) {
218                         // Entry was not found!
219                         throw new NoConfigEntryException(array(__CLASS__, $configKey), self::EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND);
220                 } // END - if
221
222                 // Unset it
223                 unset(self::$config[$configKey]);
224         }
225
226         /**
227          * Getter for field name
228          *
229          * @param       $fieldName              Field name which we shall get
230          * @return      $fieldValue             Field value from the user
231          * @throws      NullPointerException    If the result instance is null
232          */
233         public final function getField ($fieldName) {
234                 // The super interface "FrameworkInterface" requires this
235                 throw new UnsupportedOperationException(array($this, __FUNCTION__), BaseFrameworkSystem::EXCEPTION_UNSPPORTED_OPERATION);
236         }
237
238         /**
239          * Checks if given field is set
240          *
241          * @param       $fieldName      Field name to check
242          * @return      $isSet          Whether the given field name is set
243          * @throws      NullPointerException    If the result instance is null
244          */
245         public function isFieldSet ($fieldName) {
246                 // The super interface "FrameworkInterface" requires this
247                 throw new UnsupportedOperationException(array($this, __FUNCTION__), BaseFrameworkSystem::EXCEPTION_UNSPPORTED_OPERATION);
248         }
249
250         /**
251          * Generates a code for hashes from this class
252          *
253          * @return      $hashCode       The hash code respresenting this class
254          */
255         public function hashCode () {
256                 return crc32($this->__toString());
257         }
258
259         /**
260          * Checks whether an object equals this object. You should overwrite this
261          * method to implement own equality checks
262          *
263          * @param       $objectInstance         An instance of a FrameworkInterface object
264          * @return      $equals                         Whether both objects equals
265          */
266         public function equals (FrameworkInterface $objectInstance) {
267                 // Now test it
268                 $equals = ((
269                                 $this->__toString() === $objectInstance->__toString()
270                                 ) && (
271                                 $this->hashCode() === $objectInstance->hashCode()
272                                 ));
273
274                 // Return the result
275                 return $equals;
276         }
277
278         /**
279          * Setter for call-back instance
280          *
281          * @param       $callbackInstance       An instance of a FrameworkInterface class
282          * @return      void
283          */
284         public function setCallbackInstance (FrameworkInterface $callbackInstance) {
285                 $this->callbackInstance = $callbackInstance;
286         }
287
288 }