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
10 class FrameworkConfiguration {
12 * Include files which shall be included before the main loader.
14 private $moreIncPre = null;
17 * Include files which shall be included after the main loader.
19 private $moreIncPost = null;
22 * The framework's main configuration array which will be initialized with
23 * hard-coded configuration data and might be overwritten/extended by
24 * config data from the database.
26 private $config = array();
29 * The configuration instance itself
31 private static $cfgInstance = null;
33 // Some constants for the configuration system
34 const EXCEPTION_CONFIG_ENTRY_IS_EMPTY = 0xc00;
35 const EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND = 0xc01;
40 private function __construct () {
41 // Initialize both include lists
42 $this->moreIncPre = new ArrayObject();
43 $this->moreIncPost = new ArrayObject();
47 * "Create" a configuration instance
49 public final static function createFrameworkConfiguration ($enableDebug = false) {
51 * For singleton design pattern because we only need a one-time-run
52 * through the initial configuration.
54 if (is_null(self::$cfgInstance)) {
56 @error_reporting(E_ALL | E_STRICT);
59 * Shall we enable the debug mode?
62 define('DEBUG_MODE', true);
66 * Crate a config instance
68 self::$cfgInstance = new FrameworkConfiguration();
74 return self::$cfgInstance;
78 * Getter for an instance of this class
80 * @return $cfgInstance An instance of this class
82 public final static function getInstance () {
83 return self::$cfgInstance;
87 * Setter for default time zone (must be correct!)
89 * @param $zone The time-zone string (e.g. Europe/Berlin)
92 public final function setDefaultTimezone ($zone) {
93 // At least 5.1.0 is required for this!
94 if (version_compare(phpversion(), "5.1.0")) {
95 @date_default_timezone_set($zone);
100 * Setter for runtime magic quotes
102 public final function setMagicQuotesRuntime ($enableQuotes) {
103 // Cast it to boolean
104 $enableQuotes = (boolean) $enableQuotes;
107 @set_magic_quotes_runtime($enableQuotes);
111 * A private include loader
113 * @param $arrayObject The array object with all include files
116 private function loadIncludes (ArrayObject $arrayObject) {
117 // Load only if there are includes defined
118 if (!is_null($arrayObject)) {
119 for ($idx = $arrayObject->getIterator(); $idx->valid(); $idx->next()) {
121 $inc = $idx->current();
123 // Is the file name really set?
125 // Base path added? (Uni* / Windows)
126 if ((substr($inc, 0, 1) != "/") && (substr($inc, 1, 1) != ":")) {
128 $fqfn = sprintf("%s/inc/extra/%s", PATH, $inc);
130 // Base path is already added
135 // Include them all here
142 * Load all includes before main loader and clears the array after usage
146 public function loadPreIncludes () {
147 $this->loadIncludes($this->moreIncPre);
148 unset($this->moreIncPre);
152 * Load all includes after main loader and clears the array after usage
156 public function loadPostIncludes () {
157 $this->loadIncludes($this->moreIncPost);
158 unset($this->moreIncPost);
162 * Define the database type which must be valid and will not be verified.
164 * @param $type The database type. See path inc/database/.
167 public function defineDatabaseType ($type) {
168 // Is it defined or not?
169 if (!defined('_DB_TYPE')) {
171 $type = (string) $type;
174 define('_DB_TYPE', $type);
176 // Already defined! But we cannot throw an exception here... :(
177 ApplicationEntryPoint::app_die(sprintf("[%s:] Please define the database type only once in your application!",
184 * Define the local file path
186 * @param $path The database type. See path inc/database/.
189 public function definePath ($path) {
191 $path = (string) $path;
193 // Is it defined or not?
194 if (!is_dir($path)) {
195 // Is not a valid path
196 ApplicationEntryPoint::app_die(sprintf("[%s:] Invalid path (not found) specified. Please make sure it is created.",
199 } elseif (!defined('PATH')) {
201 define('PATH', $path);
203 // Already defined! But we cannot throw an exception here... :(
204 ApplicationEntryPoint::app_die(sprintf("[%s:] Please define the local file path only once in your application.",
211 * Read a configuration element.
213 * @param $cfgEntry The configuration element
214 * @return $cfgValue The fetched configuration value
215 * @throws ConfigEntryIsEmptyException If $cfgEntry is empty
216 * @throws ConfigEntryNotFoundException If a configuration element
219 public function readConfig ($cfgEntry) {
221 $cfgEntry = (string) $cfgEntry;
223 // Is a valid configuration entry provided?
224 if (empty($cfgEntry)) {
226 throw new ConfigEntryIsEmptyException(__CLASS__, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY);
227 } elseif (!isset($this->config[$cfgEntry])) {
228 // Entry was not found!
229 throw new ConfigEntryNotFoundException(array(__CLASS__, $cfgEntry), self::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND);
233 if ((defined('DEBUG_CONFIG')) || (defined('DEBUG_ALL'))) {
234 echo "[".__METHOD__."] Configuration entry ".$cfgEntry." requested.<br />\n";
237 // Return the requested value
238 return $this->config[$cfgEntry];
242 * Set a configuration entry.
244 * @param $cfgEntry The configuration entry we want to add/change
245 * @param $cfgValue The configuration value we want to set
247 * @throws ConfigEntryIsEmptyException If $cfgEntry is empty
249 public final function setConfigEntry ($cfgEntry, $cfgValue) {
251 $cfgEntry = (string) $cfgEntry;
252 $cfgValue = (string) $cfgValue;
254 // Is a valid configuration entry provided?
255 if (empty($cfgEntry)) {
257 throw new ConfigEntryIsEmptyException(__CLASS__, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY);
260 // Set the configuration value
261 $this->config[$cfgEntry] = $cfgValue;
264 ksort($this->config);
268 * Compatiblity method to return this class' name
270 * @return __CLASS__ This class' name
272 public function __toString () {
273 return get_class($this);