Change the formatting so we don't get weird problems in BBEdit folding
[isso.git] / Loader.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /**
23 * System loader (Loader.php)
24 *
25 * @package ISSO
26 */
27
28 /**
29 * System Loader
30 *
31 * This class contains static methods that are used to create new registers and
32 * load modules.
33 *
34 * @author Blue Static
35 * @copyright Copyright ©2002 - [#]year[#], Blue Static
36 * @version $Revision$
37 * @package ISSO
38 *
39 */
40 class BSLoader
41 {
42 /**
43 * Singleton instance
44 * @var object
45 */
46 private static $instance;
47
48 /**
49 * Array of all the registers
50 * @var array
51 */
52 private $registers = array();
53
54 /**
55 * The main register
56 * @var object
57 */
58 private $main;
59
60 // ###################################################################
61 /**
62 * Constructor
63 */
64 private function __construct()
65 { }
66
67 // ###################################################################
68 /**
69 * Returns the shared instance of the BSLoader singleton class.
70 *
71 * @return object The BSLoader shared instance
72 */
73 private static function SharedInstance()
74 {
75 if (!self::$instance)
76 {
77 self::$instance = new BSLoader;
78 set_error_handler(array(self::$instance, '_error_handler'));
79 }
80 return self::$instance;
81 }
82
83 // ###################################################################
84 /**
85 * Creates a new BSRegister instance and returns it.
86 *
87 * @return object New register
88 */
89 public static function NewRegister()
90 {
91 require_once('ISSO/Register.php');
92
93 self::SharedInstance()->registers[] = $return = new BSRegister();
94
95 return $return;
96 }
97
98 // ###################################################################
99 /**
100 * Returns the array of all the registers
101 *
102 * @return array The array of all the registers
103 */
104 public static function GetAllRegisters()
105 {
106 return self::SharedInstance()->registers;
107 }
108
109 // ###################################################################
110 /**
111 * Sets the main register
112 *
113 * @param object New main register
114 */
115 public static function SetRegister($register)
116 {
117 if (get_class($register) != 'BSRegister')
118 {
119 trigger_error('BSLoader::SetRegister() was not passed a BSRegister object');
120 return;
121 }
122
123 self::SharedInstance()->main = $register;
124 }
125
126 // ###################################################################
127 /**
128 * Gets the main register if no argument is passed, or an arbitrary
129 * one if the index of a register is passed.
130 *
131 * @param integer Register index
132 *
133 * @return object Specified register
134 */
135 public static function &GetRegister($index = null)
136 {
137 if (is_null($index))
138 {
139 if (!isset(self::SharedInstance()->main))
140 {
141 trigger_error('Cannot fetch the main register because it has not been set with BSLoader::SetRegister()');
142 return;
143 }
144 return self::SharedInstance()->main;
145 }
146 else
147 {
148 if (!isset(self::SharedInstance()->registers["$index"]))
149 {
150 trigger_error('Invalid register index passed to BSLoader::GetRegister()');
151 return;
152 }
153 return self::SharedInstance()->registers["$index"];
154 }
155 }
156
157 // ###################################################################
158 /**
159 * Determines if there's a main register set. If not, returns FALSE.
160 *
161 * @return bool Is there a main register set?
162 */
163 public static function HasRegister()
164 {
165 return (isset(self::SharedInstance()->main) AND self::SharedInstance()->main instanceof BSRegister);
166 }
167
168 // ###################################################################
169 /**
170 * Loads the specified module.
171 *
172 * @param string Module name
173 *
174 * @return object Instantiated module
175 */
176 public static function LoadModule($name)
177 {
178 @include_once("ISSO/$name.php");
179
180 $class = "BS$name";
181
182 if (!class_exists($class))
183 {
184 trigger_error('Specifed module does not conform to the ISSO specification, or the class is missing');
185 return;
186 }
187
188 return new $class;
189 }
190
191 // ###################################################################
192 /**
193 * Prints an ISSO message
194 *
195 * @param string The title of the message
196 * @param string The content of the message
197 * @param integer Type of message to be printed
198 * @param bool Return the output?
199 * @param bool Show the debug stack?
200 * @param integer Message width
201 *
202 * @return mixed Output or null
203 */
204 public static function Message($title, $message, $type, $return = false, $stack = true, $width = 500)
205 {
206 switch ($type)
207 {
208 // Message
209 case 1:
210 $prefix = 'Message';
211 $color = '#669900';
212 $font = '#000000';
213 break;
214
215 // Warning
216 case 2:
217 $prefix = 'Warning';
218 $color = '#003399';
219 $font = '#FFFFFF';
220 break;
221
222 case 3:
223 $prefix = 'Error';
224 $color = '#990000';
225 $font = '#EFEFEF';
226 break;
227 }
228
229 echo "\n<br />\n<table cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"$width\" style=\"background-color: $color; color: black; font-family: Verdana, sans-serif; font-size: 12px;\">";
230 echo "\n<tr style=\"color: $font; text-align: left\">\n\t<td><strong>$prefix: $title</strong></td>\n</tr>";
231 echo "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>$message</td>\n</tr>";
232 if ($stack AND self::HasRegister() AND self::GetRegister()->getDebug())
233 {
234 echo "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td><strong>Debug Stack:</strong> <pre>";
235 debug_print_backtrace();
236 echo "</pre></td>\n</tr>";
237 }
238 echo "\n</table>\n<br />\n";
239 }
240
241 // ###################################################################
242 /**
243 * Custom error handler for ISSO; only handle E_WARNING, E_NOTICE,
244 * E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
245 *
246 * @param integer Error number
247 * @param string Error message string
248 * @param string File that contains the error
249 * @param string The line number of the error
250 * @param string The active symbol table at which point the error occurred
251 */
252 private function _error_handler($errno, $errstr, $errfile, $errline, $errcontext)
253 {
254 $level = ini_get('error_reporting');
255
256 switch ($errno)
257 {
258 // So we don't need to specify the error type in trigger_error(), all E_USER_NOTICEs
259 // are fatal and automatically kill the script
260 case E_USER_NOTICE:
261 $title = 'Fatal';
262 $mode = 3;
263 break;
264
265 // A non-fatal but important warning
266 case E_WARNING:
267 $title = 'Warning';
268 $mode = 2;
269 if (!($level & E_WARNING))
270 {
271 return;
272 }
273 break;
274
275 // A non-fatal notice that should all but be ignored unless in dev environments
276 case E_NOTICE:
277 case E_STRICT:
278 $title = 'Notice';
279 $mode = 1;
280 if (!($level & E_NOTICE))
281 {
282 return;
283 }
284 break;
285
286 case E_USER_WARNING:
287 case E_USER_NOTICE:
288 default:
289 trigger_error('The only error types supported are E_USER_NOTICE (fatal), E_WARNING, and E_NOTICE');
290 break;
291 }
292
293 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
294
295 $errstr = str_replace(array(getcwd(), dirname(getcwd())), '', $errstr);
296
297 self::Message($title, $errstr, $mode);
298
299 if ($errno == E_USER_NOTICE)
300 {
301 exit;
302 }
303 }
304
305 // ###################################################################
306 /**
307 * Creates a table that explains the error reporting levels and their
308 * state
309 */
310 public static function ExplainErrorReporting()
311 {
312 $levels = array(
313 'E_ERROR' => E_ERROR,
314 'E_WARNING' => E_WARNING,
315 'E_PARSE' => E_PARSE,
316 'E_NOTICE' => E_NOTICE,
317 'E_CORE_ERROR' => E_CORE_ERROR,
318 'E_CORE_WARNING' => E_CORE_WARNING,
319 'E_COMPILE_ERROR' => E_COMPILE_ERROR,
320 'E_COMPILE_WARNING' => E_COMPILE_WARNING,
321 'E_USER_ERROR' => E_USER_ERROR,
322 'E_USER_WARNING' => E_USER_WARNING,
323 'E_USER_NOTICE' => E_USER_NOTICE,
324 'E_ALL' => E_ALL,
325 'E_STRICT' => E_STRICT
326 );
327
328 $table = '<table cellspacing="0" cellpadding="2" border="0">';
329
330 foreach ($levels AS $name => $value)
331 {
332 $table .= '
333 <tr>
334 <td>' . $name . '</td>
335 <td>' . (ini_get('error_reporting') & $value) . '</td>
336 </tr>';
337 }
338
339 $table .= '
340 </table>';
341
342 self::Message('Error Reporting', $table, 1);
343 }
344 }
345
346 /*=====================================================================*\
347 || ###################################################################
348 || # $HeadURL$
349 || # $Id$
350 || ###################################################################
351 \*=====================================================================*/
352 ?>