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