4 namespace Org\Mxchange\CoreFramework\Configuration;
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;
14 use \InvalidArgumentException;
17 * A class for the configuration stuff implemented in a singleton design paddern
19 * NOTE: We cannot put this in framework/main/ because it would be loaded (again) in
20 * class loader. See framework/loader/class_ClassLoader.php for instance
23 * @author Roland Haeder <webmaster@shipsimu.org>
25 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
26 * @license GNU GPL 3.0 or any newer version
27 * @link http://www.shipsimu.org
29 * This program is free software: you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation, either version 3 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program. If not, see <http://www.gnu.org/licenses/>.
42 class FrameworkConfiguration implements Registerable {
45 * The framework's main configuration array which will be initialized with
46 * hard-coded configuration data and might be overwritten/extended by
47 * config data from the database.
49 private static $config = array();
52 * Call-back instance (unused)
54 private $callbackInstance = NULL;
56 // Some constants for the configuration system
57 const EXCEPTION_CONFIG_KEY_IS_EMPTY = 0x130;
58 const EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND = 0x131;
59 const EXCEPTION_CONFIG_VALUE_TYPE_UNSUPPORTED = 0x132;
62 * Default constructor, the configuration entries are static, not the
67 public function __construct () {
72 * Compatiblity method to return this class' name
74 * @return __CLASS__ This class' name
76 public function __toString () {
77 return get_class($this);
81 * Checks whether the given configuration key is set
83 * @param $configKey The configuration key we shall check
84 * @return $isset Whether the given configuration key is set
85 * @throws NullPointerException If $configKey is NULL
86 * @throws InvalidArgumentException If $configKey is empty
88 public function isConfigurationEntrySet ($configKey) {
90 if (is_null($configKey)) {
92 throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER);
93 } elseif (!is_string($configKey)) {
95 throw new InvalidArgumentException(sprintf('configKey[]=%s is not a string', gettype($configKey)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
96 } elseif ((is_string($configKey)) && (empty($configKey))) {
98 throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
102 $isset = ((isset(self::$config[$configKey])) || (array_key_exists($configKey, self::$config)));
109 * Read a configuration element.
111 * @param $configKey The configuration element
112 * @return $configValue The fetched configuration value
113 * @throws NullPointerException If $configKey is NULL
114 * @throws InvalidArgumentException If $configKey is empty
115 * @throws NoConfigEntryException If a configuration element was not found
117 public function getConfigEntry ($configKey) {
119 if (is_null($configKey)) {
121 throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER);
122 } elseif (!is_string($configKey)) {
124 throw new InvalidArgumentException(sprintf('configKey[]=%s is not a string', gettype($configKey)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
125 } elseif ((is_string($configKey)) && (empty($configKey))) {
127 throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
130 // Convert dashes to underscore
131 $configKey = BaseFrameworkSystem::convertDashesToUnderscores($configKey);
133 // Is a valid configuration key provided?
134 if (!$this->isConfigurationEntrySet($configKey)) {
135 // Entry was not found!
136 throw new NoConfigEntryException(array(__CLASS__, $configKey), self::EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND);
139 // Return the requested value
140 return self::$config[$configKey];
144 * Set a configuration key
146 * @param $configKey The configuration key we want to add/change
147 * @param $configValue The configuration value we want to set
149 * @throws NullPointerException If $configKey is NULL
150 * @throws InvalidArgumentException If $configKey is empty
151 * @throws InvalidArgumentException If $configValue has an unsupported variable type
153 public final function setConfigEntry ($configKey, $configValue) {
154 // Is a valid configuration key key provided?
155 if (is_null($configKey)) {
156 // Configuration key is null
157 throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER);
158 } elseif (!is_string($configKey)) {
160 throw new InvalidArgumentException(sprintf('configKey[]=%s is not a string', gettype($configKey)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
161 } elseif ((is_string($configKey)) && (empty($configKey))) {
163 throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
164 } elseif ((is_array($configValue)) || (is_object($configValue)) || (is_resource($configValue))) {
165 // These cannot be set as this is not intended for configuration values, please use FrameworkArrayObject instead.
166 throw new InvalidArgumentException(sprintf('configValue[]=%s for configKey=%s is not supported.', gettype($configValue), $configKey), self::EXCEPTION_CONFIG_VALUE_TYPE_UNSUPPORTED);
170 $configKey = BaseFrameworkSystem::convertDashesToUnderscores($configKey);
172 // Set the configuration value
173 //* NOISY-DEBUG: */ print(__METHOD__ . ':configEntry=' . $configKey . ',configValue[' . gettype($configValue) . ']=' . $configValue . PHP_EOL);
174 self::$config[$configKey] = $configValue;
177 ksort(self::$config);
181 * Getter for whole configuration array
183 * @return $config Configuration array
185 public final function getConfigurationArray () {
187 return self::$config;
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 NullPointerException If $configKey is NULL
197 * @throws InvalidArgumentException If $configKey is empty
198 * @throws NoConfigEntryException If a configuration element was not found
200 public final function unsetConfigEntry ($configKey) {
201 // Validate parameters
202 if (is_null($configKey)) {
203 // Configuration key is null
204 throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER);
205 } elseif (!is_string($configKey)) {
207 throw new InvalidArgumentException(sprintf('configKey[]=%s is not a string', gettype($configKey)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
208 } elseif ((is_string($configKey)) && (empty($configKey))) {
210 throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
213 // Convert dashes to underscore
214 $configKey = BaseFrameworkSystem::convertDashesToUnderscores($configKey);
216 // Is the configuration key there?
217 if (!$this->isConfigurationEntrySet($configKey)) {
218 // Entry was not found!
219 throw new NoConfigEntryException(array(__CLASS__, $configKey), self::EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND);
223 unset(self::$config[$configKey]);
227 * Getter for field name
229 * @param $fieldName Field name which we shall get
230 * @return $fieldValue Field value from the user
231 * @throws NullPointerException If the result instance is null
233 public final function getField ($fieldName) {
234 // The super interface "FrameworkInterface" requires this
235 throw new UnsupportedOperationException(array($this, __FUNCTION__), BaseFrameworkSystem::EXCEPTION_UNSPPORTED_OPERATION);
239 * Checks if given field is set
241 * @param $fieldName Field name to check
242 * @return $isSet Whether the given field name is set
243 * @throws NullPointerException If the result instance is null
245 public function isFieldSet ($fieldName) {
246 // The super interface "FrameworkInterface" requires this
247 throw new UnsupportedOperationException(array($this, __FUNCTION__), BaseFrameworkSystem::EXCEPTION_UNSPPORTED_OPERATION);
251 * Generates a code for hashes from this class
253 * @return $hashCode The hash code respresenting this class
255 public function hashCode () {
256 return crc32($this->__toString());
260 * Checks whether an object equals this object. You should overwrite this
261 * method to implement own equality checks
263 * @param $objectInstance An instance of a FrameworkInterface object
264 * @return $equals Whether both objects equals
266 public function equals (FrameworkInterface $objectInstance) {
269 $this->__toString() === $objectInstance->__toString()
271 $this->hashCode() === $objectInstance->hashCode()
279 * Setter for call-back instance
281 * @param $callbackInstance An instance of a FrameworkInterface class
284 public function setCallbackInstance (FrameworkInterface $callbackInstance) {
285 $this->callbackInstance = $callbackInstance;