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