0826076e41a5f1b077ad3fbf62e41ac07a8cd46f
[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 static $config = array();
52
53         /**
54          * Call-back instance (unused)
55          */
56         private $callbackInstance = NULL;
57
58         // Some constants for the configuration system
59         const EXCEPTION_CONFIG_KEY_IS_EMPTY = 0x130;
60         const EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND = 0x131;
61         const EXCEPTION_CONFIG_VALUE_TYPE_UNSUPPORTED = 0x132;
62
63         /**
64          * Default constructor, the configuration entries are static, not the
65          * whole instance.
66          *
67          * @return      void
68          */
69         public function __construct () {
70                 // Empty for now
71         }
72
73         /**
74          * Compatiblity method to return this class' name
75          *
76          * @return      __CLASS__       This class' name
77          */
78         public function __toString () {
79                 return get_class($this);
80         }
81
82         /**
83          * Checks whether the given configuration key is set
84          *
85          * @param       $configKey      The configuration key we shall check
86          * @return      $isset  Whether the given configuration key is set
87          * @throws      NullPointerException    If $configKey is NULL
88          * @throws      InvalidArgumentException        If $configKey is empty
89          */
90         public function isConfigurationEntrySet ($configKey) {
91                 // Is it null?
92                 if (is_null($configKey)) {
93                         // Throw NPE
94                         throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER);
95                 } elseif (!is_string($configKey)) {
96                         // Is not a string
97                         throw new InvalidArgumentException(sprintf('configKey[]=%s is not a string', gettype($configKey)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
98                 } elseif ((is_string($configKey)) && (empty($configKey))) {
99                         // Entry is empty
100                         throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
101                 }
102
103                 // Is it set?
104                 $isset = ((isset(self::$config[$configKey])) || (array_key_exists($configKey, self::$config)));
105
106                 // Return the result
107                 return $isset;
108         }
109
110         /**
111          * Read a configuration element.
112          *
113          * @param       $configKey              The configuration element
114          * @return      $configValue    The fetched configuration value
115          * @throws      NullPointerException    If $configKey is NULL
116          * @throws      InvalidArgumentException        If $configKey is empty
117          * @throws      NoConfigEntryException          If a configuration element was not found
118          */
119         public function getConfigEntry ($configKey) {
120                 // Is it null?
121                 if (is_null($configKey)) {
122                         // Throw NPE
123                         throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER);
124                 } elseif (!is_string($configKey)) {
125                         // Is not a string
126                         throw new InvalidArgumentException(sprintf('configKey[]=%s is not a string', gettype($configKey)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
127                 } elseif ((is_string($configKey)) && (empty($configKey))) {
128                         // Entry is empty
129                         throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
130                 }
131
132                 // Convert dashes to underscore
133                 $configKey = BaseFrameworkSystem::convertDashesToUnderscores($configKey);
134
135                 // Is a valid configuration key provided?
136                 if (!$this->isConfigurationEntrySet($configKey)) {
137                         // Entry was not found!
138                         throw new NoConfigEntryException(array(__CLASS__, $configKey), self::EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND);
139                 } // END - if
140
141                 // Return the requested value
142                 return self::$config[$configKey];
143         }
144
145         /**
146          * Set a configuration key
147          *
148          * @param       $configKey      The configuration key we want to add/change
149          * @param       $configValue    The configuration value we want to set
150          * @return      void
151          * @throws      NullPointerException    If $configKey is NULL
152          * @throws      InvalidArgumentException        If $configKey is empty
153          * @throws      InvalidArgumentException        If $configValue has an unsupported variable type
154          */
155         public final function setConfigEntry ($configKey, $configValue) {
156                 // Is a valid configuration key key provided?
157                 if (is_null($configKey)) {
158                         // Configuration key is null
159                         throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER);
160                 } elseif (!is_string($configKey)) {
161                         // Is not a string
162                         throw new InvalidArgumentException(sprintf('configKey[]=%s is not a string', gettype($configKey)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
163                 } elseif ((is_string($configKey)) && (empty($configKey))) {
164                         // Entry is empty
165                         throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
166                 } elseif ((is_array($configValue)) || (is_object($configValue)) || (is_resource($configValue))) {
167                         // These cannot be set as this is not intended for configuration values, please use FrameworkArrayObject instead.
168                         throw new InvalidArgumentException(sprintf('configValue[]=%s for configKey=%s is not supported.', gettype($configValue), $configKey), self::EXCEPTION_CONFIG_VALUE_TYPE_UNSUPPORTED);
169                 }
170
171                 // Cast to string
172                 $configKey = BaseFrameworkSystem::convertDashesToUnderscores($configKey);
173
174                 // Set the configuration value
175                 //* NOISY-DEBUG: */ print(__METHOD__ . ':configEntry=' . $configKey . ',configValue[' . gettype($configValue) . ']=' . $configValue . PHP_EOL);
176                 self::$config[$configKey] = $configValue;
177
178                 // Resort the array
179                 ksort(self::$config);
180         }
181
182         /**
183          * Getter for whole configuration array
184          *
185          * @return      $config         Configuration array
186          */
187         public final function getConfigurationArray () {
188                 // Return it
189                 return self::$config;
190         }
191
192         /**
193          * Unset a configuration key, the entry must be there or else an
194          * exception is thrown.
195          *
196          * @param       $configKey      Configuration key to unset
197          * @return      void
198          * @throws      NullPointerException    If $configKey is NULL
199          * @throws      InvalidArgumentException        If $configKey is empty
200          * @throws      NoConfigEntryException  If a configuration element was not found
201          */
202         public final function unsetConfigEntry ($configKey) {
203                 // Validate parameters
204                 if (is_null($configKey)) {
205                         // Configuration key is null
206                         throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER);
207                 } elseif (!is_string($configKey)) {
208                         // Entry is empty
209                         throw new InvalidArgumentException(sprintf('configKey[]=%s is not a string', gettype($configKey)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
210                 } elseif ((is_string($configKey)) && (empty($configKey))) {
211                         // Entry is empty
212                         throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
213                 }
214
215                 // Convert dashes to underscore
216                 $configKey = BaseFrameworkSystem::convertDashesToUnderscores($configKey);
217
218                 // Is the configuration key there?
219                 if (!$this->isConfigurationEntrySet($configKey)) {
220                         // Entry was not found!
221                         throw new NoConfigEntryException(array(__CLASS__, $configKey), self::EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND);
222                 } // END - if
223
224                 // Unset it
225                 unset(self::$config[$configKey]);
226         }
227
228         /**
229          * Getter for field name
230          *
231          * @param       $fieldName              Field name which we shall get
232          * @return      $fieldValue             Field value from the user
233          * @throws      NullPointerException    If the result instance is null
234          */
235         public final function getField ($fieldName) {
236                 // The super interface "FrameworkInterface" requires this
237                 throw new UnsupportedOperationException(array($this, __FUNCTION__), BaseFrameworkSystem::EXCEPTION_UNSPPORTED_OPERATION);
238         }
239
240         /**
241          * Checks if given field is set
242          *
243          * @param       $fieldName      Field name to check
244          * @return      $isSet          Whether the given field name is set
245          * @throws      NullPointerException    If the result instance is null
246          */
247         public function isFieldSet ($fieldName) {
248                 // The super interface "FrameworkInterface" requires this
249                 throw new UnsupportedOperationException(array($this, __FUNCTION__), BaseFrameworkSystem::EXCEPTION_UNSPPORTED_OPERATION);
250         }
251
252         /**
253          * Generates a code for hashes from this class
254          *
255          * @return      $hashCode       The hash code respresenting this class
256          */
257         public function hashCode () {
258                 return crc32($this->__toString());
259         }
260
261         /**
262          * Checks whether an object equals this object. You should overwrite this
263          * method to implement own equality checks
264          *
265          * @param       $objectInstance         An instance of a FrameworkInterface object
266          * @return      $equals                         Whether both objects equals
267          */
268         public function equals (FrameworkInterface $objectInstance) {
269                 // Now test it
270                 $equals = ((
271                                 $this->__toString() === $objectInstance->__toString()
272                                 ) && (
273                                 $this->hashCode() === $objectInstance->hashCode()
274                                 ));
275
276                 // Return the result
277                 return $equals;
278         }
279
280         /**
281          * Setter for call-back instance
282          *
283          * @param       $callbackInstance       An instance of a FrameworkInterface class
284          * @return      void
285          */
286         public function setCallbackInstance (FrameworkInterface $callbackInstance) {
287                 $this->callbackInstance = $callbackInstance;
288         }
289
290 }