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
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 class FrameworkConfiguration {
29 * Include files which shall be included before the main loader.
31 private $moreIncPre = null;
34 * Include files which shall be included after the main loader.
36 private $moreIncPost = null;
39 * The framework's main configuration array which will be initialized with
40 * hard-coded configuration data and might be overwritten/extended by
41 * config data from the database.
43 private $config = array();
46 * The configuration instance itself
48 private static $cfgInstance = null;
50 // Some constants for the configuration system
51 const EXCEPTION_CONFIG_ENTRY_IS_EMPTY = 0xc00;
52 const EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND = 0xc01;
57 private function __construct () {
58 // Initialize both include lists
59 $this->moreIncPre = new ArrayObject();
60 $this->moreIncPost = new ArrayObject();
64 * "Create" a configuration instance
66 public final static function createFrameworkConfiguration ($enableDebug = false) {
68 * For singleton design pattern because we only need a one-time-run
69 * through the initial configuration.
71 if (is_null(self::$cfgInstance)) {
73 @error_reporting(E_ALL | E_STRICT);
76 * Shall we enable the debug mode?
79 define('DEBUG_MODE', true);
83 * Crate a config instance
85 self::$cfgInstance = new FrameworkConfiguration();
91 return self::$cfgInstance;
95 * Getter for an instance of this class
97 * @return $cfgInstance An instance of this class
99 public final static function getInstance () {
100 return self::$cfgInstance;
104 * Setter for default time zone (must be correct!)
106 * @param $zone The time-zone string (e.g. Europe/Berlin)
109 public final function setDefaultTimezone ($zone) {
110 // At least 5.1.0 is required for this!
111 if (version_compare(phpversion(), "5.1.0")) {
112 @date_default_timezone_set($zone);
117 * Setter for runtime magic quotes
119 public final function setMagicQuotesRuntime ($enableQuotes) {
120 // Cast it to boolean
121 $enableQuotes = (boolean) $enableQuotes;
124 @set_magic_quotes_runtime($enableQuotes);
128 * A private include loader
130 * @param $arrayObject The array object with all include files
133 private function loadIncludes (ArrayObject $arrayObject) {
134 // Load only if there are includes defined
135 if (!is_null($arrayObject)) {
136 for ($idx = $arrayObject->getIterator(); $idx->valid(); $idx->next()) {
138 $inc = $idx->current();
140 // Is the file name really set?
142 // Base path added? (Uni* / Windows)
143 if ((substr($inc, 0, 1) != "/") && (substr($inc, 1, 1) != ":")) {
145 $fqfn = sprintf("%s/inc/extra/%s", PATH, $inc);
147 // Base path is already added
152 // Include them all here
159 * Load all includes before main loader and clears the array after usage
163 public function loadPreIncludes () {
164 $this->loadIncludes($this->moreIncPre);
165 unset($this->moreIncPre);
169 * Load all includes after main loader and clears the array after usage
173 public function loadPostIncludes () {
174 $this->loadIncludes($this->moreIncPost);
175 unset($this->moreIncPost);
179 * Define the database type which must be valid and will not be verified.
181 * @param $type The database type. See path inc/database/.
184 public function defineDatabaseType ($type) {
185 // Is it defined or not?
186 if (!defined('_DB_TYPE')) {
188 $type = (string) $type;
191 define('_DB_TYPE', $type);
193 // Already defined! But we cannot throw an exception here... :(
194 ApplicationEntryPoint::app_die(sprintf("[%s:] Please define the database type only once in your application!",
201 * Define the local file path
203 * @param $path The database type. See path inc/database/.
206 public function definePath ($path) {
208 $path = (string) $path;
210 // Replace backslashes with slashes
211 $path = str_replace("\\", "/", $path);
213 // Is it defined or not?
214 if ((!is_dir($path)) || (!is_readable($path))) {
215 // Is not a valid path
216 ApplicationEntryPoint::app_die(sprintf("[%s:] Invalid path (not found) specified. Please make sure it is created.",
219 } elseif (!defined('PATH')) {
221 define('PATH', $path);
223 // Already defined! But we cannot throw an exception here... :(
224 ApplicationEntryPoint::app_die(sprintf("[%s:] Please define the local file path only once in your application.",
231 * Read a configuration element.
233 * @param $cfgEntry The configuration element
234 * @return $cfgValue The fetched configuration value
235 * @throws ConfigEntryIsEmptyException If $cfgEntry is empty
236 * @throws ConfigEntryNotFoundException If a configuration element
239 public function readConfig ($cfgEntry) {
241 $cfgEntry = (string) $cfgEntry;
243 // Is a valid configuration entry provided?
244 if (empty($cfgEntry)) {
246 throw new ConfigEntryIsEmptyException(__CLASS__, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY);
247 } elseif (!isset($this->config[$cfgEntry])) {
248 // Entry was not found!
249 throw new ConfigEntryNotFoundException(array(__CLASS__, $cfgEntry), self::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND);
253 if ((defined('DEBUG_CONFIG')) || (defined('DEBUG_ALL'))) {
254 echo "[".__METHOD__."] Configuration entry ".$cfgEntry." requested.<br />\n";
257 // Return the requested value
258 return $this->config[$cfgEntry];
262 * Set a configuration entry.
264 * @param $cfgEntry The configuration entry we want to add/change
265 * @param $cfgValue The configuration value we want to set
267 * @throws ConfigEntryIsEmptyException If $cfgEntry is empty
269 public final function setConfigEntry ($cfgEntry, $cfgValue) {
271 $cfgEntry = (string) $cfgEntry;
272 $cfgValue = (string) $cfgValue;
274 // Is a valid configuration entry provided?
275 if (empty($cfgEntry)) {
277 throw new ConfigEntryIsEmptyException(__CLASS__, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY);
280 // Set the configuration value
281 $this->config[$cfgEntry] = $cfgValue;
284 ksort($this->config);
288 * Compatiblity method to return this class' name
290 * @return __CLASS__ This class' name
292 public function __toString () {
293 return get_class($this);