Copyrights added
[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          * Include files which shall be included before the main loader.
31          */
32         private $moreIncPre = null;
33
34         /**
35          * Include files which shall be included after the main loader.
36          */
37         private $moreIncPost = null;
38
39         /**
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.
43          */
44         private $config = array();
45
46         /**
47          * The configuration instance itself
48          */
49         private static $cfgInstance = null;
50
51         // Some constants for the configuration system
52         const EXCEPTION_CONFIG_ENTRY_IS_EMPTY      = 0xc00;
53         const EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND = 0xc01;
54
55         /**
56          * Private constructor
57          */
58         private function __construct () {
59                 // Initialize both include lists
60                 $this->moreIncPre  = new ArrayObject();
61                 $this->moreIncPost = new ArrayObject();
62         }
63
64         /**
65          * "Create" a configuration instance
66          */
67         public final static function createFrameworkConfiguration ($enableDebug = false) {
68                 /**
69                  * For singleton design pattern because we only need a one-time-run
70                  * through the initial configuration.
71                  */
72                 if (is_null(self::$cfgInstance))  {
73                         // CFG: DEBUG-LEVEL
74                         @error_reporting(E_ALL | E_STRICT);
75
76                         /**
77                          * Shall we enable the debug mode?
78                          */
79                         if ($enableDebug) {
80                                 define('DEBUG_MODE', true);
81                         }
82
83                         /**
84                          * Crate a config instance
85                          */
86                         self::$cfgInstance = new FrameworkConfiguration();
87                 }
88
89                 /**
90                  * Return the instance
91                  */
92                 return self::$cfgInstance;
93         }
94
95         /**
96          * Getter for an instance of this class
97          *
98          * @return      $cfgInstance    An instance of this class
99          */
100         public final static function getInstance () {
101                 return self::$cfgInstance;
102         }
103
104         /**
105          * Setter for default time zone (must be correct!)
106          *
107          * @param               $zone   The time-zone string (e.g. Europe/Berlin)
108          * @return      void
109          */
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);
114                 }
115         }
116
117         /**
118          * Setter for runtime magic quotes
119          */
120         public final function setMagicQuotesRuntime ($enableQuotes) {
121                 // Cast it to boolean
122                 $enableQuotes = (boolean) $enableQuotes;
123
124                 // Set it
125                 @set_magic_quotes_runtime($enableQuotes);
126         }
127
128         /**
129          * A private include loader
130          *
131          * @param               $arrayObject            The array object with all include files
132          * @return      void
133          */
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()) {
138                                 // Get include file
139                                 $inc = $idx->current();
140
141                                 // Is the file name really set?
142                                 if (!empty($inc)) {
143                                         // Base path added? (Uni* / Windows)
144                                         if ((substr($inc, 0, 1) != "/") && (substr($inc, 1, 1) != ":")) {
145                                                 // Generate FQFN
146                                                 $fqfn = sprintf("%s/inc/extra/%s", PATH, $inc);
147                                         } else {
148                                                 // Base path is already added
149                                                 $fqfn = $inc;
150                                         }
151                                 }
152
153                                 // Include them all here
154                                 require($fqfn);
155                         }
156                 }
157         }
158
159         /**
160          * Load all includes before main loader and clears the array after usage
161          *
162          * @return      void
163          */
164         public function loadPreIncludes () {
165                 $this->loadIncludes($this->moreIncPre);
166                 unset($this->moreIncPre);
167         }
168
169         /**
170          * Load all includes after main loader and clears the array after usage
171          *
172          * @return      void
173          */
174         public function loadPostIncludes () {
175                 $this->loadIncludes($this->moreIncPost);
176                 unset($this->moreIncPost);
177         }
178
179         /**
180          * Define the database type which must be valid and will not be verified.
181          *
182          * @param               $type   The database type. See path inc/database/.
183          * @return      void
184          */
185         public function defineDatabaseType ($type) {
186                 // Is it defined or not?
187                 if (!defined('_DB_TYPE')) {
188                         // Cast to string
189                         $type = (string) $type;
190
191                         // Set the constant
192                         define('_DB_TYPE', $type);
193                 } else {
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!",
196                                 __CLASS__
197                         ));
198                 }
199         }
200
201         /**
202          * Define the local file path
203          *
204          * @param               $path   The database type. See path inc/database/.
205          * @return      void
206          */
207         public function definePath ($path) {
208                 // Cast to string
209                 $path = (string) $path;
210
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.",
215                                 __CLASS__
216                         ));
217                 } elseif (!defined('PATH')) {
218                         // Set the constant
219                         define('PATH', $path);
220                 } else {
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.",
223                                 __CLASS__
224                         ));
225                 }
226         }
227
228         /**
229          * Read a configuration element.
230          *
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
235          *                                                                      was not found
236          */
237         public function readConfig ($cfgEntry) {
238                 // Cast to string
239                 $cfgEntry = (string) $cfgEntry;
240
241                 // Is a valid configuration entry provided?
242                 if (empty($cfgEntry)) {
243                         // Entry is empty
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);
248                 }
249
250                 // Debug message
251                 if ((defined('DEBUG_CONFIG')) || (defined('DEBUG_ALL'))) {
252                         echo "[".__METHOD__."] Configuration entry ".$cfgEntry." requested.<br />\n";
253                 }
254
255                 // Return the requested value
256                 return $this->config[$cfgEntry];
257         }
258
259         /**
260          * Set a configuration entry.
261          *
262          * @param               $cfgEntry       The configuration entry we want to add/change
263          * @param               $cfgValue       The configuration value we want to set
264          * @return      void
265          * @throws      ConfigEntryIsEmptyException     If $cfgEntry is empty
266          */
267         public final function setConfigEntry ($cfgEntry, $cfgValue) {
268                 // Cast to string
269                 $cfgEntry = (string) $cfgEntry;
270                 $cfgValue = (string) $cfgValue;
271
272                 // Is a valid configuration entry provided?
273                 if (empty($cfgEntry)) {
274                         // Entry is empty
275                         throw new ConfigEntryIsEmptyException(__CLASS__, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY);
276                 }
277
278                 // Set the configuration value
279                 $this->config[$cfgEntry] = $cfgValue;
280
281                 // Resort the array
282                 ksort($this->config);
283         }
284
285         /**
286          * Compatiblity method to return this class' name
287          *
288          * @return      __CLASS__               This class' name
289          */
290         public function __toString () {
291                 return get_class($this);
292         }
293 } // END - class
294
295 // [EOF]
296 ?>