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