More exceptions added, class loader can now load extra configs
[mailer.git] / inc / config / class_FrameworkConfiguration.php
1 <?php
2 /**
3  * A class for the configuration stuff implemented in a singleton design paddern
4  *
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
7  *
8  * @see                 ClassLoader
9  * @author              Roland Haeder <webmaster@mxchange.org>
10  * @version             0.3.0
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
14  *
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.
19  *
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.
24  *
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/>.
27  */
28 class FrameworkConfiguration {
29         /**
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.
33          */
34         private $config = array();
35
36         /**
37          * The configuration instance itself
38          */
39         private static $cfgInstance = null;
40
41         // Some constants for the configuration system
42         const EXCEPTION_CONFIG_ENTRY_IS_EMPTY      = 0xc00;
43         const EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND = 0xc01;
44
45         /**
46          * Private constructor
47          */
48         private function __construct () {
49                 // Empty for now
50         }
51
52         /**
53          * "Create" a configuration instance
54          */
55         public final static function createFrameworkConfiguration ($enableDebug = false) {
56                 /**
57                  * For singleton design pattern because we only need a one-time-run
58                  * through the initial configuration.
59                  */
60                 if (is_null(self::$cfgInstance))  {
61                         // CFG: DEBUG-LEVEL
62                         @error_reporting(E_ALL | E_STRICT);
63
64                         /**
65                          * Shall we enable the debug mode?
66                          */
67                         if ($enableDebug) {
68                                 define('DEBUG_MODE', true);
69                         }
70
71                         /**
72                          * Crate a config instance
73                          */
74                         self::$cfgInstance = new FrameworkConfiguration();
75                 }
76
77                 /**
78                  * Return the instance
79                  */
80                 return self::$cfgInstance;
81         }
82
83         /**
84          * Getter for an instance of this class
85          *
86          * @return      $cfgInstance    An instance of this class
87          */
88         public final static function getInstance () {
89                 return self::$cfgInstance;
90         }
91
92         /**
93          * Setter for default time zone (must be correct!)
94          *
95          * @param               $zone   The time-zone string (e.g. Europe/Berlin)
96          * @return      void
97          */
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);
102                 }
103         }
104
105         /**
106          * Setter for runtime magic quotes
107          */
108         public final function setMagicQuotesRuntime ($enableQuotes) {
109                 // Cast it to boolean
110                 $enableQuotes = (boolean) $enableQuotes;
111
112                 // Set it
113                 @set_magic_quotes_runtime($enableQuotes);
114         }
115
116         /**
117          * A private include loader
118          *
119          * @param               $arrayObject            The array object with all include files
120          * @return      void
121          */
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()) {
126                                 // Get include file
127                                 $inc = $idx->current();
128
129                                 // Is the file name really set?
130                                 if (!empty($inc)) {
131                                         // Base path added? (Uni* / Windows)
132                                         if ((substr($inc, 0, 1) != "/") && (substr($inc, 1, 1) != ":")) {
133                                                 // Generate FQFN
134                                                 $fqfn = sprintf("%s/inc/extra/%s", PATH, $inc);
135                                         } else {
136                                                 // Base path is already added
137                                                 $fqfn = $inc;
138                                         }
139                                 }
140
141                                 // Include them all here
142                                 require($fqfn);
143                         }
144                 }
145         }
146
147         /**
148          * Define the database type which must be valid and will not be verified.
149          *
150          * @param               $type   The database type. See path inc/database/.
151          * @return      void
152          */
153         public function defineDatabaseType ($type) {
154                 // Is it defined or not?
155                 if (!defined('_DB_TYPE')) {
156                         // Cast to string
157                         $type = (string) $type;
158
159                         // Set the constant
160                         define('_DB_TYPE', $type);
161                 } else {
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!",
164                                 __CLASS__
165                         ));
166                 }
167         }
168
169         /**
170          * Define the local file path
171          *
172          * @param               $path   The database type. See path inc/database/.
173          * @return      void
174          */
175         public function definePath ($path) {
176                 // Cast to string
177                 $path = (string) $path;
178
179                 // Replace backslashes with slashes
180                 $path = str_replace("\\", "/", $path);
181
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.",
186                                 __CLASS__
187                         ));
188                 } elseif (!defined('PATH')) {
189                         // Set the constant
190                         define('PATH', $path);
191                 } else {
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.",
194                                 __CLASS__
195                         ));
196                 }
197         }
198
199         /**
200          * Read a configuration element.
201          *
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
206          *                                                                      was not found
207          */
208         public function readConfig ($cfgEntry) {
209                 // Cast to string
210                 $cfgEntry = (string) $cfgEntry;
211
212                 // Is a valid configuration entry provided?
213                 if (empty($cfgEntry)) {
214                         // Entry is empty
215                         throw new ConfigEntryIsEmptyException(__CLASS__, 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);
219                 }
220
221                 // Debug message
222                 if ((defined('DEBUG_CONFIG')) || (defined('DEBUG_ALL'))) {
223                         echo "[".__METHOD__."] Configuration entry ".$cfgEntry." requested.<br />\n";
224                 }
225
226                 // Return the requested value
227                 return $this->config[$cfgEntry];
228         }
229
230         /**
231          * Set a configuration entry.
232          *
233          * @param               $cfgEntry       The configuration entry we want to add/change
234          * @param               $cfgValue       The configuration value we want to set
235          * @return      void
236          * @throws      ConfigEntryIsEmptyException     If $cfgEntry is empty
237          */
238         public final function setConfigEntry ($cfgEntry, $cfgValue) {
239                 // Cast to string
240                 $cfgEntry = (string) $cfgEntry;
241                 $cfgValue = (string) $cfgValue;
242
243                 // Is a valid configuration entry provided?
244                 if (empty($cfgEntry)) {
245                         // Entry is empty
246                         throw new ConfigEntryIsEmptyException(__CLASS__, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY);
247                 }
248
249                 // Set the configuration value
250                 $this->config[$cfgEntry] = $cfgValue;
251
252                 // Resort the array
253                 ksort($this->config);
254         }
255
256         /**
257          * Compatiblity method to return this class' name
258          *
259          * @return      __CLASS__               This class' name
260          */
261         public function __toString () {
262                 return get_class($this);
263         }
264 } // END - class
265
266 // [EOF]
267 ?>