Moving error handling stuff into Loader and Functions
[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 * Returns the shared instance of the BSLoader singleton class.
69 *
70 * @return object The BSLoader shared instance
71 */
72 private function SharedInstance()
73 {
74 if (!self::$instance)
75 {
76 self::$instance = new BSLoader;
77 set_error_handler(array(self::$instance, '_error_handler'));
78 }
79 return self::$instance;
80 }
81
82 // ###################################################################
83 /**
84 * Creates a new BSRegister instance and returns it.
85 *
86 * @return object New register
87 */
88 public static function NewRegister()
89 {
90 require_once('ISSO/Register.php');
91
92 self::SharedInstance()->registers[] = new BSRegister();
93
94 return end(self::SharedInstance()->registers);
95 }
96
97 // ###################################################################
98 /**
99 * Returns the array of all the registers
100 *
101 * @return array The array of all the registers
102 */
103 public static function GetAllRegisters()
104 {
105 return self::SharedInstance()->registers;
106 }
107
108 // ###################################################################
109 /**
110 * Sets the main register
111 *
112 * @param object New main register
113 */
114 public static function SetRegister($register)
115 {
116 if (get_class($register) != 'BSRegister')
117 {
118 trigger_error('BSLoader::SetRegister() was not passed a BSRegister object');
119 return;
120 }
121
122 self::SharedInstance()->main = $register;
123 }
124
125 // ###################################################################
126 /**
127 * Gets the main register if no argument is passed, or an arbitrary
128 * one if the index of a register is passed.
129 *
130 * @param integer Register index
131 *
132 * @return object Specified register
133 */
134 public static function &GetRegister($index = null)
135 {
136 if (is_null($index))
137 {
138 if (!isset(self::SharedInstance()->main))
139 {
140 trigger_error('Cannot fetch the main register because it has not been set with BSLoader::SetRegister()');
141 return;
142 }
143 return self::SharedInstance()->main;
144 }
145 else
146 {
147 if (!isset(self::SharedInstance()->registers["$index"]))
148 {
149 trigger_error('Invalid register index passed to BSLoader::GetRegister()');
150 return;
151 }
152 return self::SharedInstance()->registers["$index"];
153 }
154 }
155
156 // ###################################################################
157 /**
158 * Loads the specified module.
159 *
160 * @param string Module name
161 *
162 * @return object Instantiated module
163 */
164 public static function LoadModule($name)
165 {
166 @include_once("ISSO/$name.php");
167
168 $class = "BS$name";
169
170 if (!class_exists($class))
171 {
172 trigger_error('Specifed module does not conform to the ISSO specification, or the class is missing');
173 return;
174 }
175
176 return new $class;
177 }
178
179 // ###################################################################
180 /**
181 * Prints an ISSO message
182 *
183 * @param string The title of the message
184 * @param string The content of the message
185 * @param integer Type of message to be printed
186 * @param bool Return the output?
187 * @param bool Show the debug stack?
188 * @param integer Message width
189 *
190 * @return mixed Output or null
191 */
192 public static function Message($title, $message, $type, $return = false, $stack = true, $width = 500)
193 {
194 switch ($type)
195 {
196 // Message
197 case 1:
198 $prefix = 'Message';
199 $color = '#669900';
200 $font = '#000000';
201 break;
202
203 // Warning
204 case 2:
205 $prefix = 'Warning';
206 $color = '#003399';
207 $font = '#FFFFFF';
208 break;
209
210 case 3:
211 $prefix = 'Error';
212 $color = '#990000';
213 $font = '#EFEFEF';
214 break;
215 }
216
217 $backtrace = debug_backtrace();
218 array_shift($backtrace);
219
220 if (isset($backtrace[0]) AND $backtrace[0]['function'] == '_error_handler')
221 {
222 array_shift($backtrace);
223 }
224
225 $trace = BSFunctions::FormatDebugTrace($backtrace);
226
227 $output = "\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;\">";
228 $output .= "\n<tr style=\"color: $font; text-align: left\">\n\t<td><strong>$prefix: $title</strong></td>\n</tr>";
229 $output .= "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>$message</td>\n</tr>";
230 $output .= (($stack AND self::GetRegister()->getDebug()) ? "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td><strong>Debug Stack:</strong> <pre>" . implode("\n", $trace) . "</pre></td>\n</tr>" : '');
231 $output .= "\n</table>\n<br />\n";
232
233 if ($return)
234 {
235 return $output;
236 }
237 else
238 {
239 print($output);
240 }
241 }
242
243 // ###################################################################
244 /**
245 * Custom error handler for ISSO; only handle E_WARNING, E_NOTICE,
246 * E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
247 *
248 * @param integer Error number
249 * @param string Error message string
250 * @param string File that contains the error
251 * @param string The line number of the error
252 * @param string The active symbol table at which point the error occurred
253 */
254 private function _error_handler($errno, $errstr, $errfile, $errline, $errcontext)
255 {
256 $level = ini_get('error_reporting');
257
258 switch ($errno)
259 {
260 // So we don't need to specify the error type in trigger_error(), all E_USER_NOTICEs
261 // are fatal and automatically kill the script
262 case E_USER_NOTICE:
263 $title = 'Fatal';
264 $mode = 3;
265 break;
266
267 // A non-fatal but important warning
268 case E_WARNING:
269 $title = 'Warning';
270 $mode = 2;
271 if (!($level & E_WARNING))
272 {
273 return;
274 }
275 break;
276
277 // A non-fatal notice that should all but be ignored unless in dev environments
278 case E_NOTICE:
279 case E_STRICT:
280 $title = 'Notice';
281 $mode = 1;
282 if (!($level & E_NOTICE))
283 {
284 return;
285 }
286 break;
287
288 case E_USER_WARNING:
289 case E_USER_NOTICE:
290 default:
291 trigger_error('The only error types supported are E_USER_NOTICE (fatal), E_WARNING, and E_NOTICE ' . $errno);
292 break;
293 }
294
295 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
296
297 $errstr = str_replace(array(getcwd(), dirname(getcwd())), '', $errstr);
298
299 self::Message($title, $errstr, $mode);
300
301 if ($errno == E_USER_NOTICE)
302 {
303 exit;
304 }
305 }
306
307 // ###################################################################
308 /**
309 * Creates a table that explains the error reporting levels and their
310 * state
311 */
312 public function explain_error_reporting()
313 {
314 $levels = array(
315 'E_ERROR' => E_ERROR,
316 'E_WARNING' => E_WARNING,
317 'E_PARSE' => E_PARSE,
318 'E_NOTICE' => E_NOTICE,
319 'E_CORE_ERROR' => E_CORE_ERROR,
320 'E_CORE_WARNING' => E_CORE_WARNING,
321 'E_COMPILE_ERROR' => E_COMPILE_ERROR,
322 'E_COMPILE_WARNING' => E_COMPILE_WARNING,
323 'E_USER_ERROR' => E_USER_ERROR,
324 'E_USER_WARNING' => E_USER_WARNING,
325 'E_USER_NOTICE' => E_USER_NOTICE,
326 'E_ALL' => E_ALL,
327 'E_STRICT' => E_STRICT
328 );
329
330 $table = '<table cellspacing="0" cellpadding="2" border="0">';
331
332 foreach ($levels AS $name => $value)
333 {
334 $table .= '
335 <tr>
336 <td>' . $name . '</td>
337 <td>' . (ini_get('error_reporting') & $value) . '</td>
338 </tr>';
339 }
340
341 $table .= '
342 </table>';
343
344 $this->message('Error Reporting', $table, 1);
345 }
346 }
347
348 /*=====================================================================*\
349 || ###################################################################
350 || # $HeadURL$
351 || # $Id$
352 || ###################################################################
353 \*=====================================================================*/
354 ?>