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@mxchange.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.mxchange.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 * Include files which shall be included before the main loader.
32 private $moreIncPre = null;
35 * Include files which shall be included after the main loader.
37 private $moreIncPost = null;
40 * The framework's main configuration array which will be initialized with
41 * hard-coded configuration data and might be overwritten/extended by
42 * config data from the database.
44 private $config = array();
47 * The configuration instance itself
49 private static $cfgInstance = null;
51 // Some constants for the configuration system
52 const EXCEPTION_CONFIG_ENTRY_IS_EMPTY = 0xc00;
53 const EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND = 0xc01;
58 private function __construct () {
59 // Initialize both include lists
60 $this->moreIncPre = new ArrayObject();
61 $this->moreIncPost = new ArrayObject();
65 * "Create" a configuration instance
67 public final static function createFrameworkConfiguration ($enableDebug = false) {
69 * For singleton design pattern because we only need a one-time-run
70 * through the initial configuration.
72 if (is_null(self::$cfgInstance)) {
74 @error_reporting(E_ALL | E_STRICT);
77 * Shall we enable the debug mode?
80 define('DEBUG_MODE', true);
84 * Crate a config instance
86 self::$cfgInstance = new FrameworkConfiguration();
92 return self::$cfgInstance;
96 * Getter for an instance of this class
98 * @return $cfgInstance An instance of this class
100 public final static function getInstance () {
101 return self::$cfgInstance;
105 * Setter for default time zone (must be correct!)
107 * @param $zone The time-zone string (e.g. Europe/Berlin)
110 public final function setDefaultTimezone ($zone) {
111 // At least 5.1.0 is required for this!
112 if (version_compare(phpversion(), "5.1.0")) {
113 @date_default_timezone_set($zone);
118 * Setter for runtime magic quotes
120 public final function setMagicQuotesRuntime ($enableQuotes) {
121 // Cast it to boolean
122 $enableQuotes = (boolean) $enableQuotes;
125 @set_magic_quotes_runtime($enableQuotes);
129 * A private include loader
131 * @param $arrayObject The array object with all include files
134 private function loadIncludes (ArrayObject $arrayObject) {
135 // Load only if there are includes defined
136 if (!is_null($arrayObject)) {
137 for ($idx = $arrayObject->getIterator(); $idx->valid(); $idx->next()) {
139 $inc = $idx->current();
141 // Is the file name really set?
143 // Base path added? (Uni* / Windows)
144 if ((substr($inc, 0, 1) != "/") && (substr($inc, 1, 1) != ":")) {
146 $fqfn = sprintf("%s/inc/extra/%s", PATH, $inc);
148 // Base path is already added
153 // Include them all here
160 * Load all includes before main loader and clears the array after usage
164 public function loadPreIncludes () {
165 $this->loadIncludes($this->moreIncPre);
166 unset($this->moreIncPre);
170 * Load all includes after main loader and clears the array after usage
174 public function loadPostIncludes () {
175 $this->loadIncludes($this->moreIncPost);
176 unset($this->moreIncPost);
180 * Define the database type which must be valid and will not be verified.
182 * @param $type The database type. See path inc/database/.
185 public function defineDatabaseType ($type) {
186 // Is it defined or not?
187 if (!defined('_DB_TYPE')) {
189 $type = (string) $type;
192 define('_DB_TYPE', $type);
194 // Already defined! But we cannot throw an exception here... :(
195 ApplicationEntryPoint::app_die(sprintf("[%s:] Please define the database type only once in your application!",
202 * Define the local file path
204 * @param $path The database type. See path inc/database/.
207 public function definePath ($path) {
209 $path = (string) $path;
211 // Is it defined or not?
212 if (!is_dir($path)) {
213 // Is not a valid path
214 ApplicationEntryPoint::app_die(sprintf("[%s:] Invalid path (not found) specified. Please make sure it is created.",
217 } elseif (!defined('PATH')) {
219 define('PATH', $path);
221 // Already defined! But we cannot throw an exception here... :(
222 ApplicationEntryPoint::app_die(sprintf("[%s:] Please define the local file path only once in your application.",
229 * Read a configuration element.
231 * @param $cfgEntry The configuration element
232 * @return $cfgValue The fetched configuration value
233 * @throws ConfigEntryIsEmptyException If $cfgEntry is empty
234 * @throws ConfigEntryNotFoundException If a configuration element
237 public function readConfig ($cfgEntry) {
239 $cfgEntry = (string) $cfgEntry;
241 // Is a valid configuration entry provided?
242 if (empty($cfgEntry)) {
244 throw new ConfigEntryIsEmptyException(__CLASS__, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY);
245 } elseif (!isset($this->config[$cfgEntry])) {
246 // Entry was not found!
247 throw new ConfigEntryNotFoundException(array(__CLASS__, $cfgEntry), self::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND);
251 if ((defined('DEBUG_CONFIG')) || (defined('DEBUG_ALL'))) {
252 echo "[".__METHOD__."] Configuration entry ".$cfgEntry." requested.<br />\n";
255 // Return the requested value
256 return $this->config[$cfgEntry];
260 * Set a configuration entry.
262 * @param $cfgEntry The configuration entry we want to add/change
263 * @param $cfgValue The configuration value we want to set
265 * @throws ConfigEntryIsEmptyException If $cfgEntry is empty
267 public final function setConfigEntry ($cfgEntry, $cfgValue) {
269 $cfgEntry = (string) $cfgEntry;
270 $cfgValue = (string) $cfgValue;
272 // Is a valid configuration entry provided?
273 if (empty($cfgEntry)) {
275 throw new ConfigEntryIsEmptyException(__CLASS__, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY);
278 // Set the configuration value
279 $this->config[$cfgEntry] = $cfgValue;
282 ksort($this->config);
286 * Compatiblity method to return this class' name
288 * @return __CLASS__ This class' name
290 public function __toString () {
291 return get_class($this);