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