3 * A class for the configuration stuff implemented in a singleton design paddern
5 * NOTE: We cannot put this in inc/classes/ because it would be loaded (again)
6 * in the class loader. See inc/loader/class_ClassLoader.php for instance
9 * @author Roland Haeder <webmaster@ship-simu.org>
11 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
12 * @license GNU GPL 3.0 or any newer version
13 * @link http://www.ship-simu.org
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 class FrameworkConfiguration {
30 * The framework's main configuration array which will be initialized with
31 * hard-coded configuration data and might be overwritten/extended by
32 * config data from the database.
34 private $config = array();
37 * The configuration instance itself
39 private static $cfgInstance = null;
41 // Some constants for the configuration system
42 const EXCEPTION_CONFIG_ENTRY_IS_EMPTY = 0xc00;
43 const EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND = 0xc01;
46 * Protected constructor
48 protected function __construct () {
53 * "Create" a configuration instance
55 public final static function createFrameworkConfiguration ($enableDebug = false) {
57 * For singleton design pattern because we only need a one-time-run
58 * through the initial configuration.
60 if (is_null(self::$cfgInstance)) {
62 @error_reporting(E_ALL | E_STRICT);
65 * Shall we enable the debug mode?
68 define('DEBUG_MODE', true);
72 * Crate a config instance
74 self::$cfgInstance = new FrameworkConfiguration();
80 return self::$cfgInstance;
84 * Getter for an instance of this class
86 * @return $cfgInstance An instance of this class
88 public final static function getInstance () {
89 return self::$cfgInstance;
93 * Setter for default time zone (must be correct!)
95 * @param $zone The time-zone string (e.g. Europe/Berlin)
98 public final function setDefaultTimezone ($zone) {
99 // At least 5.1.0 is required for this!
100 if (version_compare(phpversion(), "5.1.0")) {
101 @date_default_timezone_set($zone);
106 * Setter for runtime magic quotes
108 public final function setMagicQuotesRuntime ($enableQuotes) {
109 // Cast it to boolean
110 $enableQuotes = (boolean) $enableQuotes;
113 @set_magic_quotes_runtime($enableQuotes);
117 * A private include loader
119 * @param $arrayObject The array object with all include files
122 private function loadIncludes (ArrayObject $arrayObject) {
123 // Load only if there are includes defined
124 if (!is_null($arrayObject)) {
125 for ($idx = $arrayObject->getIterator(); $idx->valid(); $idx->next()) {
127 $inc = $idx->current();
129 // Is the file name really set?
131 // Base path added? (Uni* / Windows)
132 if ((substr($inc, 0, 1) != "/") && (substr($inc, 1, 1) != ":")) {
134 $fqfn = sprintf("%s/inc/extra/%s", PATH, $inc);
136 // Base path is already added
141 // Include them all here
148 * Define the database type which must be valid and will not be verified.
150 * @param $type The database type. See path inc/database/.
153 public function defineDatabaseType ($type) {
154 // Is it defined or not?
155 if (!defined('_DB_TYPE')) {
157 $type = (string) $type;
160 define('_DB_TYPE', $type);
162 // Already defined! But we cannot throw an exception here... :(
163 ApplicationEntryPoint::app_die(sprintf("[%s:] Please define the database type only once in your application!",
170 * Define the local file path
172 * @param $path The database type. See path inc/database/.
175 public function definePath ($path) {
177 $path = (string) $path;
179 // Replace backslashes with slashes
180 $path = str_replace("\\", "/", $path);
182 // Is it defined or not?
183 if ((!is_dir($path)) || (!is_readable($path))) {
184 // Is not a valid path
185 ApplicationEntryPoint::app_die(sprintf("[%s:] Invalid path (not found) specified. Please make sure it is created.",
188 } elseif (!defined('PATH')) {
190 define('PATH', $path);
192 // Already defined! But we cannot throw an exception here... :(
193 ApplicationEntryPoint::app_die(sprintf("[%s:] Please define the local file path only once in your application.",
200 * Read a configuration element.
202 * @param $cfgEntry The configuration element
203 * @return $cfgValue The fetched configuration value
204 * @throws ConfigEntryIsEmptyException If $cfgEntry is empty
205 * @throws ConfigEntryNotFoundException If a configuration element
208 public function readConfig ($cfgEntry) {
210 $cfgEntry = (string) $cfgEntry;
212 // Is a valid configuration entry provided?
213 if (empty($cfgEntry)) {
215 throw new ConfigEntryIsEmptyException($this, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY);
216 } elseif (!isset($this->config[$cfgEntry])) {
217 // Entry was not found!
218 throw new ConfigEntryNotFoundException(array(__CLASS__, $cfgEntry), self::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND);
222 if ((defined('DEBUG_CONFIG')) || (defined('DEBUG_ALL'))) {
223 echo "[".__METHOD__."] Configuration entry ".$cfgEntry." requested.<br />\n";
226 // Return the requested value
227 return $this->config[$cfgEntry];
231 * Set a configuration entry.
233 * @param $cfgEntry The configuration entry we want to add/change
234 * @param $cfgValue The configuration value we want to set
236 * @throws ConfigEntryIsEmptyException If $cfgEntry is empty
238 public final function setConfigEntry ($cfgEntry, $cfgValue) {
240 $cfgEntry = (string) $cfgEntry;
241 $cfgValue = (string) $cfgValue;
243 // Is a valid configuration entry provided?
244 if (empty($cfgEntry)) {
246 throw new ConfigEntryIsEmptyException($this, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY);
249 // Set the configuration value
250 $this->config[$cfgEntry] = $cfgValue;
253 ksort($this->config);
257 * Compatiblity method to return this class' name
259 * @return __CLASS__ This class' name
261 public function __toString () {
262 return get_class($this);