]> git.mxchange.org Git - core.git/blob - framework/config/class_FrameworkConfiguration.php
e43a3b86eb773f80f96050d71df88d0d52fe6a3b
[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 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 - 2021 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('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                 if (empty($configKey)) {
112                         // Entry is empty
113                         throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
114                 }
115
116                 // Convert dashes to underscore
117                 $configKey = StringUtils::convertDashesToUnderscores($configKey);
118
119                 // Is a valid configuration key provided?
120                 if (!$this->isConfigurationEntrySet($configKey)) {
121                         // Entry was not found!
122                         throw new NoConfigEntryException(array(__CLASS__, $configKey), self::EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND);
123                 }
124
125                 // Return the requested value
126                 return self::$configData[$configKey];
127         }
128
129         /**
130          * Set a configuration key
131          *
132          * @param       $configKey      The configuration key we want to add/change
133          * @param       $configValue    The configuration value we want to set
134          * @return      void
135          * @throws      InvalidArgumentException        If $configKey is empty
136          * @throws      InvalidArgumentException        If $configValue has an unsupported variable type
137          */
138         public final function setConfigEntry (string $configKey, $configValue) {
139                 // Is a valid configuration key key provided?
140                 if (empty($configKey)) {
141                         // Entry is empty
142                         throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
143                 } elseif ((is_array($configValue)) || (is_object($configValue)) || (is_resource($configValue))) {
144                         // These cannot be set as this is not intended for configuration values, please use FrameworkArrayObject instead.
145                         throw new InvalidArgumentException(sprintf('configValue[]=%s for configKey=%s is not supported.', gettype($configValue), $configKey), self::EXCEPTION_CONFIG_VALUE_TYPE_UNSUPPORTED);
146                 }
147
148                 // Cast to string
149                 $configKey = StringUtils::convertDashesToUnderscores($configKey);
150
151                 // Set the configuration value
152                 //* NOISY-DEBUG: */ print(__METHOD__ . ':configEntry=' . $configKey . ',configValue[' . gettype($configValue) . ']=' . $configValue . PHP_EOL);
153                 self::$configData[$configKey] = $configValue;
154
155                 // Resort the array
156                 ksort(self::$configData);
157         }
158
159         /**
160          * Getter for whole configuration array
161          *
162          * @return      $config         Configuration array
163          */
164         public final function getConfigurationArray () {
165                 // Return it
166                 return self::$configData;
167         }
168
169         /**
170          * Unset a configuration key, the entry must be there or else an
171          * exception is thrown.
172          *
173          * @param       $configKey      Configuration key to unset
174          * @return      void
175          * @throws      InvalidArgumentException        If $configKey is empty
176          * @throws      NoConfigEntryException  If a configuration element was not found
177          */
178         public final function unsetConfigEntry (string $configKey) {
179                 // Validate parameters
180                 if (empty($configKey)) {
181                         // Entry is empty
182                         throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
183                 }
184
185                 // Convert dashes to underscore
186                 $configKey = StringUtils::convertDashesToUnderscores($configKey);
187
188                 // Is the configuration key there?
189                 if (!$this->isConfigurationEntrySet($configKey)) {
190                         // Entry was not found!
191                         throw new NoConfigEntryException(array(__CLASS__, $configKey), self::EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND);
192                 }
193
194                 // Unset it
195                 unset(self::$configData[$configKey]);
196         }
197
198         /**
199          * Generates a code for hashes from this class
200          *
201          * @return      $hashCode       The hash code respresenting this class
202          */
203         public function hashCode () {
204                 return crc32($this->__toString());
205         }
206
207         /**
208          * Checks whether an object equals this object. You should overwrite this
209          * method to implement own equality checks
210          *
211          * @param       $objectInstance         An instance of a FrameworkInterface object
212          * @return      $equals                         Whether both objects equals
213          */
214         public function equals (FrameworkInterface $objectInstance) {
215                 // Now test it
216                 $equals = ((
217                                 $this->__toString() === $objectInstance->__toString()
218                                 ) && (
219                                 $this->hashCode() === $objectInstance->hashCode()
220                                 ));
221
222                 // Return the result
223                 return $equals;
224         }
225
226         /**
227          * Setter for call-back instance
228          *
229          * @param       $callbackInstance       An instance of a FrameworkInterface class
230          * @return      void
231          */
232         public function setCallbackInstance (FrameworkInterface $callbackInstance) {
233                 $this->callbackInstance = $callbackInstance;
234         }
235
236         /**
237          * Getter for field name
238          *
239          * @param       $fieldName              Field name which we shall get
240          * @return      $fieldValue             Field value from the user
241          * @throws      NullPointerException    If the result instance is null
242          */
243         public final function getField (string $fieldName) {
244                 // The super interface "FrameworkInterface" requires this
245                 throw new UnsupportedOperationException(array($this, __FUNCTION__), BaseFrameworkSystem::EXCEPTION_UNSPPORTED_OPERATION);
246         }
247
248         /**
249          * Checks if given field is set
250          *
251          * @param       $fieldName      Field name to check
252          * @return      $isSet          Whether the given field name is set
253          * @throws      NullPointerException    If the result instance is null
254          */
255         public function isFieldSet (string $fieldName) {
256                 // The super interface "FrameworkInterface" requires this
257                 throw new UnsupportedOperationException(array($this, __FUNCTION__), BaseFrameworkSystem::EXCEPTION_UNSPPORTED_OPERATION);
258         }
259
260 }