4 namespace Org\Mxchange\CoreFramework\Configuration;
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;
15 use \InvalidArgumentException;
16 use \UnexpectedValueException;
19 * A class for the configuration stuff implemented in a singleton design pattern
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
25 * @author Roland Haeder <webmaster@shipsimu.org>
27 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
28 * @license GNU GPL 3.0 or any newer version
29 * @link http://www.shipsimu.org
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.
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.
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/>.
44 class FrameworkConfiguration implements Registerable {
45 // Some constants for the configuration system
46 const EXCEPTION_CONFIG_KEY_IS_EMPTY = 0x130;
47 const EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND = 0x131;
48 const EXCEPTION_CONFIG_VALUE_TYPE_UNSUPPORTED = 0x132;
51 * The framework's main configuration array which will be initialized with
52 * hard-coded configuration data and might be overwritten/extended by
53 * config data from the database.
55 private static $configData = [];
58 * Call-back instance (unused)
60 private $callbackInstance = NULL;
63 * Default constructor, the configuration entries are static, not the
68 public function __construct () {
73 * Compatiblity method to return this class' name
75 * @return __CLASS__ This class' name
77 public function __toString () {
78 return get_class($this);
82 * Checks whether the given configuration key is set
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
88 public function isConfigurationEntrySet (string $configKey) {
90 //* NOISY-DEBUG: */ printf('[%s:%d]: configKey=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $configKey);
91 if (empty($configKey)) {
93 throw new InvalidArgumentException('Parameter "configKey" is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
97 $isset = ((isset(self::$configData[$configKey])) || (array_key_exists($configKey, self::$configData)));
100 //* NOISY-DEBUG: */ printf('[%s:%d]: isset=%s - EXIT!' . PHP_EOL, __METHOD__, __LINE__, intval($isset));
105 * Read a configuration element.
107 * @param $configKey The configuration element
108 * @return $configValue The fetched configuration value
109 * @throws InvalidArgumentException If $configKey is empty
110 * @throws NoConfigEntryException If a configuration element was not found
112 public function getConfigEntry (string $configKey) {
114 //* NOISY-DEBUG: */ printf('[%s:%d]: configKey=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $configKey);
115 if (empty($configKey)) {
117 throw new InvalidArgumentException('Parameter "configKey" is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
120 // Convert dashes to underscore
121 $configKey = StringUtils::convertDashesToUnderscores($configKey);
123 // Is a valid configuration key provided?
124 //* NOISY-DEBUG: */ printf('[%s:%d]: configKey=%s - AFTER!' . PHP_EOL, __METHOD__, __LINE__, $configKey);
125 if (!$this->isConfigurationEntrySet($configKey)) {
126 // Entry was not found!
127 throw new NoConfigEntryException(array(__CLASS__, $configKey), self::EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND);
130 // Return the requested value
131 //* NOISY-DEBUG: */ printf('[%s:%d]: Returning configData[%s]=[%s]:%s - EXIT!' . PHP_EOL, __METHOD__, __LINE__, $configKey, gettype(self::$configData[$configKey]), self::$configData[$configKey]);
132 return self::$configData[$configKey];
136 * Set a configuration key
138 * @param $configKey The configuration key we want to add/change
139 * @param $configValue The configuration value we want to set
141 * @throws InvalidArgumentException If $configKey is empty
142 * @throws InvalidArgumentException If $configValue has an unsupported variable type
144 public final function setConfigEntry (string $configKey, $configValue) {
145 // Is a valid configuration key key provided?
146 //* NOISY-DEBUG: */ printf('[%s:%d]: configKey=%s,configValue[]=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $configKey, gettype($configValue));
147 if (empty($configKey)) {
149 throw new InvalidArgumentException('Parameter "configKey" is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
150 } elseif ((is_array($configValue)) || (is_object($configValue)) || (is_resource($configValue))) {
151 // These cannot be set as this is not intended for configuration values, please use FrameworkArrayObject instead.
152 throw new InvalidArgumentException(sprintf('configValue[]=%s for configKey=%s is not supported.', gettype($configValue), $configKey), self::EXCEPTION_CONFIG_VALUE_TYPE_UNSUPPORTED);
156 $configKey = StringUtils::convertDashesToUnderscores($configKey);
158 // Set the configuration value
159 //* NOISY-DEBUG: */ printf('[%s:%d]: Setting configKey=%s,configValue[%s]=%s - EXIT!' . PHP_EOL, __METHOD__, __LINE__, $configKey, gettype($configValue), $configValue);
160 self::$configData[$configKey] = $configValue;
164 * Getter for whole configuration array
166 * @return $config Configuration array
168 public final function getConfigurationArray () {
170 //* NOISY-DEBUG: */ printf('[%s:%d]: self::configData()=%d - EXIT!' . PHP_EOL, __METHOD__, __LINE__, count(self::$configData));
171 return self::$configData;
175 * Sorts the configuration array, saves A LOT calls if done after all configuration files have been loaded. You should NOT
176 * set any configuration entries by your own, means outside any configuration file. If you still do so, you HAVE to call
177 * this method afterwards
181 public final function sortConfigurationArray () {
183 //* NOISY-DEBUG: */ printf('[%s:%d]: Sorting %d records - CALLED!' . PHP_EOL, __METHOD__, __LINE__, count(self::$configData));
184 ksort(self::$configData);
187 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
191 * Unset a configuration key, the entry must be there or else an
192 * exception is thrown.
194 * @param $configKey Configuration key to unset
196 * @throws InvalidArgumentException If $configKey is empty
197 * @throws NoConfigEntryException If a configuration element was not found
199 public final function unsetConfigEntry (string $configKey) {
200 // Validate parameters
201 //* NOISY-DEBUG: */ printf('[%s:%d]: configKey=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $configKey);
202 if (empty($configKey)) {
204 throw new InvalidArgumentException('Parameter "configKey" is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
207 // Convert dashes to underscore
208 $configKey = StringUtils::convertDashesToUnderscores($configKey);
210 // Is the configuration key there?
211 //* NOISY-DEBUG: */ printf('[%s:%d]: configKey=%s - AFTER!' . PHP_EOL, __METHOD__, __LINE__, $configKey);
212 if (!$this->isConfigurationEntrySet($configKey)) {
213 // Entry was not found!
214 throw new NoConfigEntryException(array(__CLASS__, $configKey), self::EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND);
218 //* NOISY-DEBUG: */ printf('[%s:%d]: Unsetting configKey=%s ...' . PHP_EOL, __METHOD__, __LINE__, $configKey);
219 unset(self::$configData[$configKey]);
222 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
226 * Checks if a configuration entry is_*_enabled set to 'Y'
228 * @param $keyPart Configuration to expand with is_$keyPart_enabled
229 * @return $enabled Whether it has been set to Y or N
230 * @throws InvalidArgumentException If a parameter is invalid
231 * @throws UnexpectedValueException If a returned value is of an unexpected type or value
233 public function isEnabled (string $keyPart) {
234 // Validate parameters
235 //* NOISY-DEBUG: */ printf('[%s:%d]: keyPart=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $keyPart);
236 if (empty($keyPart)) {
238 throw new InvalidArgumentException('Parameter "keyPart" is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
241 // Construct final config key
242 $configKey = sprintf('is_%s_enabled', $keyPart);
245 //* NOISY-DEBUG: */ printf('[%s:%d]: configKey=%s' . PHP_EOL, __METHOD__, __LINE__, $configKey);
246 $isEnabled = $this->getConfigEntry($configKey);
249 //* NOISY-DEBUG: */ printf('[%s:%d]: isEnabled[]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype($isEnabled));
250 if (!is_bool($isEnabled)) {
252 throw new UnexpectedValueException(sprintf('isEnabled[]=%s is unexpected', gettype($isEnabled)));
256 //* NOISY-DEBUG: */ printf('[%s:%d]: isEnabled=%d - EXIT!' . PHP_EOL, __METHOD__, __LINE__, intval($isEnabled));
261 * Generates a code for hashes from this class
263 * @return $hashCode The hash code respresenting this class
265 public function hashCode () {
266 return crc32($this->__toString());
270 * Checks whether an object equals this object. You should overwrite this
271 * method to implement own equality checks
273 * @param $objectInstance An instance of a FrameworkInterface object
274 * @return $equals Whether both objects equals
276 public function equals (FrameworkInterface $objectInstance) {
279 $this->__toString() === $objectInstance->__toString()
281 $this->hashCode() === $objectInstance->hashCode()
289 * Setter for call-back instance
291 * @param $callbackInstance An instance of a FrameworkInterface class
294 public function setCallbackInstance (FrameworkInterface $callbackInstance) {
295 $this->callbackInstance = $callbackInstance;
299 * Getter for field name
301 * @param $fieldName Field name which we shall get
302 * @return $fieldValue Field value from the user
303 * @throws NullPointerException If the result instance is null
305 public final function getField (string $fieldName) {
306 // The super interface "FrameworkInterface" requires this
307 throw new UnsupportedOperationException([$this, __FUNCTION__], BaseFrameworkSystem::EXCEPTION_UNSPPORTED_OPERATION);
311 * Checks if given field is set
313 * @param $fieldName Field name to check
314 * @return $isSet Whether the given field name is set
315 * @throws NullPointerException If the result instance is null
317 public function isFieldSet (string $fieldName) {
318 // The super interface "FrameworkInterface" requires this
319 throw new UnsupportedOperationException([$this, __FUNCTION__], BaseFrameworkSystem::EXCEPTION_UNSPPORTED_OPERATION);