4 namespace CoreFramework\Configuration;
6 // Import framework stuff
7 use CoreFramework\Console\Tools\ConsoleTools;
8 use CoreFramework\Dns\UnknownHostnameException;
9 use CoreFramework\Generic\FrameworkInterface;
10 use CoreFramework\Generic\NullPointerException;
11 use CoreFramework\Generic\UnsupportedOperationException;
12 use CoreFramework\Object\BaseFrameworkSystem;
13 use CoreFramework\Registry\Registerable;
16 use \InvalidArgumentException;
19 * A class for the configuration stuff implemented in a singleton design paddern
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 - 2017 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 {
47 * The framework's main configuration array which will be initialized with
48 * hard-coded configuration data and might be overwritten/extended by
49 * config data from the database.
51 private $config = array();
54 * The configuration instance itself
56 private static $configInstance = NULL;
59 * Call-back instance (unused)
61 private $callbackInstance = NULL;
63 // Some constants for the configuration system
64 const EXCEPTION_CONFIG_KEY_IS_EMPTY = 0x130;
65 const EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND = 0x131;
66 const EXCEPTION_CONFIG_VALUE_TYPE_UNSUPPORTED = 0x132;
73 private function __construct () {
78 * Compatiblity method to return this class' name
80 * @return __CLASS__ This class' name
82 public function __toString () {
83 return get_class($this);
87 * Getter for a singleton instance of this class
89 * @return $configInstance A singleton instance of this class
91 public static final function getSelfInstance () {
92 // is the instance there?
93 if (is_null(self::$configInstance)) {
94 // Create a config instance
95 self::$configInstance = new FrameworkConfiguration();
98 // Return singleton instance
99 return self::$configInstance;
103 * Checks whether the given configuration key is set
105 * @param $configKey The configuration key we shall check
106 * @return $isset Whether the given configuration key is set
107 * @throws NullPointerException If $configKey is NULL
108 * @throws InvalidArgumentException If $configKey is empty
110 public function isConfigurationEntrySet ($configKey) {
112 if (is_null($configKey)) {
114 throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER);
115 } elseif (!is_string($configKey)) {
117 throw new InvalidArgumentException(sprintf('configKey[]=%s is not a string', gettype($configKey)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
118 } elseif ((is_string($configKey)) && (empty($configKey))) {
120 throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
124 $isset = ((isset($this->config[$configKey])) || (array_key_exists($configKey, $this->config)));
131 * Read a configuration element.
133 * @param $configKey The configuration element
134 * @return $configValue The fetched configuration value
135 * @throws NullPointerException If $configKey is NULL
136 * @throws InvalidArgumentException If $configKey is empty
137 * @throws NoConfigEntryException If a configuration element was not found
139 public function getConfigEntry ($configKey) {
141 if (is_null($configKey)) {
143 throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER);
144 } elseif (!is_string($configKey)) {
146 throw new InvalidArgumentException(sprintf('configKey[]=%s is not a string', gettype($configKey)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
147 } elseif ((is_string($configKey)) && (empty($configKey))) {
149 throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
152 // Convert dashes to underscore
153 $configKey = BaseFrameworkSystem::convertDashesToUnderscores($configKey);
155 // Is a valid configuration key provided?
156 if (!$this->isConfigurationEntrySet($configKey)) {
157 // Entry was not found!
158 throw new NoConfigEntryException(array(__CLASS__, $configKey), self::EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND);
161 // Return the requested value
162 return $this->config[$configKey];
166 * Set a configuration key
168 * @param $configKey The configuration key we want to add/change
169 * @param $configValue The configuration value we want to set
171 * @throws NullPointerException If $configKey is NULL
172 * @throws InvalidArgumentException If $configKey is empty
173 * @throws InvalidArgumentException If $configValue has an unsupported variable type
175 public final function setConfigEntry ($configKey, $configValue) {
176 // Is a valid configuration key key provided?
177 if (is_null($configKey)) {
178 // Configuration key is null
179 throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER);
180 } elseif (!is_string($configKey)) {
182 throw new InvalidArgumentException(sprintf('configKey[]=%s is not a string', gettype($configKey)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
183 } elseif ((is_string($configKey)) && (empty($configKey))) {
185 throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
186 } elseif ((is_array($configValue)) || (is_object($configValue)) || (is_resource($configValue))) {
187 // These cannot be set as this is not intended for configuration values, please use FrameworkArrayObject instead.
188 throw new InvalidArgumentException(sprintf('configValue[]=%s for configKey=%s is not supported.', gettype($configValue), $configKey), self::EXCEPTION_CONFIG_VALUE_TYPE_UNSUPPORTED);
192 $configKey = BaseFrameworkSystem::convertDashesToUnderscores($configKey);
194 // Set the configuration value
195 //* NOISY-DEBUG: */ print(__METHOD__ . ':configEntry=' . $configKey . ',configValue[' . gettype($configValue) . ']=' . $configValue . PHP_EOL);
196 $this->config[$configKey] = $configValue;
199 ksort($this->config);
203 * Getter for whole configuration array
205 * @return $config Configuration array
207 public final function getConfigurationArray () {
209 return $this->config;
213 * Unset a configuration key, the entry must be there or else an
214 * exception is thrown.
216 * @param $configKey Configuration key to unset
218 * @throws NullPointerException If $configKey is NULL
219 * @throws InvalidArgumentException If $configKey is empty
220 * @throws NoConfigEntryException If a configuration element was not found
222 public final function unsetConfigEntry ($configKey) {
223 // Validate parameters
224 if (is_null($configKey)) {
225 // Configuration key is null
226 throw new NullPointerException($this, BaseFrameworkSystem::EXCEPTION_IS_NULL_POINTER);
227 } elseif (!is_string($configKey)) {
229 throw new InvalidArgumentException(sprintf('configKey[]=%s is not a string', gettype($configKey)), self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
230 } elseif ((is_string($configKey)) && (empty($configKey))) {
232 throw new InvalidArgumentException('configKey is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY);
235 // Convert dashes to underscore
236 $configKey = BaseFrameworkSystem::convertDashesToUnderscores($configKey);
238 // Is the configuration key there?
239 if (!$this->isConfigurationEntrySet($configKey)) {
240 // Entry was not found!
241 throw new NoConfigEntryException(array(__CLASS__, $configKey), self::EXCEPTION_CONFIG_KEY_WAS_NOT_FOUND);
245 unset($this->config[$configKey]);
249 * Getter for field name
251 * @param $fieldName Field name which we shall get
252 * @return $fieldValue Field value from the user
253 * @throws NullPointerException If the result instance is null
255 public final function getField ($fieldName) {
256 // The super interface "FrameworkInterface" requires this
257 throw new UnsupportedOperationException(array($this, __FUNCTION__), BaseFrameworkSystem::EXCEPTION_UNSPPORTED_OPERATION);
261 * Checks if given field is set
263 * @param $fieldName Field name to check
264 * @return $isSet Whether the given field name is set
265 * @throws NullPointerException If the result instance is null
267 public function isFieldSet ($fieldName) {
268 // The super interface "FrameworkInterface" requires this
269 throw new UnsupportedOperationException(array($this, __FUNCTION__), BaseFrameworkSystem::EXCEPTION_UNSPPORTED_OPERATION);
273 * Generates a code for hashes from this class
275 * @return $hashCode The hash code respresenting this class
277 public function hashCode () {
278 return crc32($this->__toString());
282 * Checks whether an object equals this object. You should overwrite this
283 * method to implement own equality checks
285 * @param $objectInstance An instance of a FrameworkInterface object
286 * @return $equals Whether both objects equals
288 public function equals (FrameworkInterface $objectInstance) {
291 $this->__toString() === $objectInstance->__toString()
293 $this->hashCode() === $objectInstance->hashCode()
301 * Setter for call-back instance
303 * @param $callbackInstance An instance of a FrameworkInterface class
306 public function setCallbackInstance (FrameworkInterface $callbackInstance) {
307 $this->callbackInstance = $callbackInstance;