]> git.mxchange.org Git - core.git/blob - framework/config/class_FrameworkConfiguration.php
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\Configuration\NoConfigEntryException;
8 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
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\Utils\Strings\StringUtils;
13
14 // Import SPL stuff
15 use \InvalidArgumentException;
16
17 /**
18  * A class for the configuration stuff implemented in a singleton design pattern
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 - 2022 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         // Some constants for the configuration system
45         const EXCEPTION_CONFIG_KEY_IS_EMPTY = 0x130;
46         const EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND = 0x131;
47         const EXCEPTION_CONFIG_VALUE_TYPE_UNSUPPORTED = 0x132;
48
49         /**
50          * The framework's main configuration array which will be initialized with
51          * hard-coded configuration data and might be overwritten/extended by
52          * config data from the database.
53          */
54         private static $configData = [];
55
56         /**
57          * Call-back instance (unused)
58          */
59         private $callbackInstance = NULL;
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      InvalidArgumentException        If $configKey is empty
86          */
87         public function isConfigurationEntrySet (string $configKey) {
88                 // Is it null?
89                 if (empty($configKey)) {
90                         // Entry is empty
91                         throw new InvalidArgumentException('Parameter "configKey" is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
92                 }
93
94                 // Is it set?
95                 $isset = ((isset(self::$configData[$configKey])) || (array_key_exists($configKey, self::$configData)));
96
97                 // Return the result
98                 return $isset;
99         }
100
101         /**
102          * Read a configuration element.
103          *
104          * @param       $configKey              The configuration element
105          * @return      $configValue    The fetched configuration value
106          * @throws      InvalidArgumentException        If $configKey is empty
107          * @throws      NoConfigEntryException          If a configuration element was not found
108          */
109         public function getConfigEntry (string $configKey) {
110                 // Is it null?
111                 //* NOISY-DEBUG: */ printf('[%s:%d]: configKey=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $configKey);
112                 if (empty($configKey)) {
113                         // Entry is empty
114                         throw new InvalidArgumentException('Parameter "configKey" is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
115                 }
116
117                 // Convert dashes to underscore
118                 $configKey = StringUtils::convertDashesToUnderscores($configKey);
119
120                 // Is a valid configuration key provided?
121                 //* NOISY-DEBUG: */ printf('[%s:%d]: configKey=%s - AFTER!' . PHP_EOL, __METHOD__, __LINE__, $configKey);
122                 if (!$this->isConfigurationEntrySet($configKey)) {
123                         // Entry was not found!
124                         throw new NoConfigEntryException(array(__CLASS__, $configKey), self::EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND);
125                 }
126
127                 // Return the requested value
128                 //* NOISY-DEBUG: */ printf('[%s:%d]: Returning configData[%s]=[%s]:%s - EXIT!' . PHP_EOL, __METHOD__, __LINE__, $configKey, gettype(self::$configData[$configKey]), self::$configData[$configKey]);
129                 return self::$configData[$configKey];
130         }
131
132         /**
133          * Set a configuration key
134          *
135          * @param       $configKey      The configuration key we want to add/change
136          * @param       $configValue    The configuration value we want to set
137          * @return      void
138          * @throws      InvalidArgumentException        If $configKey is empty
139          * @throws      InvalidArgumentException        If $configValue has an unsupported variable type
140          */
141         public final function setConfigEntry (string $configKey, $configValue) {
142                 // Is a valid configuration key key provided?
143                 //* NOISY-DEBUG: */ printf('[%s:%d]: configKey=%s,configValue[]=%s' . PHP_EOL, __METHOD__, __LINE__, $configKey, gettype($configValue));
144                 if (empty($configKey)) {
145                         // Entry is empty
146                         throw new InvalidArgumentException('Parameter "configKey" is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
147                 } elseif ((is_array($configValue)) || (is_object($configValue)) || (is_resource($configValue))) {
148                         // These cannot be set as this is not intended for configuration values, please use FrameworkArrayObject instead.
149                         throw new InvalidArgumentException(sprintf('configValue[]=%s for configKey=%s is not supported.', gettype($configValue), $configKey), self::EXCEPTION_CONFIG_VALUE_TYPE_UNSUPPORTED);
150                 }
151
152                 // Cast to string
153                 $configKey = StringUtils::convertDashesToUnderscores($configKey);
154
155                 // Set the configuration value
156                 //* NOISY-DEBUG: */ printf('[%s:%d]: configKey=%s,configValue[%s]=%s' . PHP_EOL, __METHOD__, __LINE__, $configKey, gettype($configValue), $configValue);
157                 self::$configData[$configKey] = $configValue;
158         }
159
160         /**
161          * Getter for whole configuration array
162          *
163          * @return      $config         Configuration array
164          */
165         public final function getConfigurationArray () {
166                 // Return it
167                 //* NOISY-DEBUG: */ printf('[%s:%d]: self::configData()=%d - EXIT!' . PHP_EOL, __METHOD__, __LINE__, count(self::$configData));
168                 return self::$configData;
169         }
170
171         /**
172          * Sorts the configuration array, saves A LOT calls if done after all configuration files have been loaded. You should NOT
173          * set any configuration entries by your own, means outside any configuration file. If you still do so, you HAVE to call
174          * this method afterwards
175          *
176          * @return      void
177          */
178         public final function sortConfigurationArray () {
179                 // Resort the array
180                 //* NOISY-DEBUG: */ printf('[%s:%d]: Sorting %d records - CALLED!' . PHP_EOL, __METHOD__, __LINE__, count(self::$configData));
181                 ksort(self::$configData);
182
183                 // Debug message
184                 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
185         }
186
187         /**
188          * Unset a configuration key, the entry must be there or else an
189          * exception is thrown.
190          *
191          * @param       $configKey      Configuration key to unset
192          * @return      void
193          * @throws      InvalidArgumentException        If $configKey is empty
194          * @throws      NoConfigEntryException  If a configuration element was not found
195          */
196         public final function unsetConfigEntry (string $configKey) {
197                 // Validate parameters
198                 //* NOISY-DEBUG: */ printf('[%s:%d]: configKey=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $configKey);
199                 if (empty($configKey)) {
200                         // Entry is empty
201                         throw new InvalidArgumentException('Parameter "configKey" is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
202                 }
203
204                 // Convert dashes to underscore
205                 $configKey = StringUtils::convertDashesToUnderscores($configKey);
206
207                 // Is the configuration key there?
208                 //* NOISY-DEBUG: */ printf('[%s:%d]: configKey=%s - AFTER!' . PHP_EOL, __METHOD__, __LINE__, $configKey);
209                 if (!$this->isConfigurationEntrySet($configKey)) {
210                         // Entry was not found!
211                         throw new NoConfigEntryException(array(__CLASS__, $configKey), self::EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND);
212                 }
213
214                 // Unset it
215                 //* NOISY-DEBUG: */ printf('[%s:%d]: Unsetting configKey=%s ...' . PHP_EOL, __METHOD__, __LINE__, $configKey);
216                 unset(self::$configData[$configKey]);
217
218                 // Debug message
219                 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
220         }
221
222         /**
223          * Generates a code for hashes from this class
224          *
225          * @return      $hashCode       The hash code respresenting this class
226          */
227         public function hashCode () {
228                 return crc32($this->__toString());
229         }
230
231         /**
232          * Checks whether an object equals this object. You should overwrite this
233          * method to implement own equality checks
234          *
235          * @param       $objectInstance         An instance of a FrameworkInterface object
236          * @return      $equals                         Whether both objects equals
237          */
238         public function equals (FrameworkInterface $objectInstance) {
239                 // Now test it
240                 $equals = ((
241                                 $this->__toString() === $objectInstance->__toString()
242                                 ) && (
243                                 $this->hashCode() === $objectInstance->hashCode()
244                                 ));
245
246                 // Return the result
247                 return $equals;
248         }
249
250         /**
251          * Setter for call-back instance
252          *
253          * @param       $callbackInstance       An instance of a FrameworkInterface class
254          * @return      void
255          */
256         public function setCallbackInstance (FrameworkInterface $callbackInstance) {
257                 $this->callbackInstance = $callbackInstance;
258         }
259
260         /**
261          * Getter for field name
262          *
263          * @param       $fieldName              Field name which we shall get
264          * @return      $fieldValue             Field value from the user
265          * @throws      NullPointerException    If the result instance is null
266          */
267         public final function getField (string $fieldName) {
268                 // The super interface "FrameworkInterface" requires this
269                 throw new UnsupportedOperationException(array($this, __FUNCTION__), BaseFrameworkSystem::EXCEPTION_UNSPPORTED_OPERATION);
270         }
271
272         /**
273          * Checks if given field is set
274          *
275          * @param       $fieldName      Field name to check
276          * @return      $isSet          Whether the given field name is set
277          * @throws      NullPointerException    If the result instance is null
278          */
279         public function isFieldSet (string $fieldName) {
280                 // The super interface "FrameworkInterface" requires this
281                 throw new UnsupportedOperationException(array($this, __FUNCTION__), BaseFrameworkSystem::EXCEPTION_UNSPPORTED_OPERATION);
282         }
283
284 }