]> src.bluestatic.org Git - isso.git/blob - Loader.php
Make SharedInstance() static to remove notices
[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 static 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 * Determines if there's a main register set. If not, returns FALSE.
159 *
160 * @return bool Is there a main register set?
161 */
162 public static function HasRegister()
163 {
164 return (isset(self::SharedInstance()->main) AND self::SharedInstance()->main instanceof BSRegister);
165 }
166
167 // ###################################################################
168 /**
169 * Loads the specified module.
170 *
171 * @param string Module name
172 *
173 * @return object Instantiated module
174 */
175 public static function LoadModule($name)
176 {
177 @include_once("ISSO/$name.php");
178
179 $class = "BS$name";
180
181 if (!class_exists($class))
182 {
183 trigger_error('Specifed module does not conform to the ISSO specification, or the class is missing');
184 return;
185 }
186
187 return new $class;
188 }
189
190 // ###################################################################
191 /**
192 * Prints an ISSO message
193 *
194 * @param string The title of the message
195 * @param string The content of the message
196 * @param integer Type of message to be printed
197 * @param bool Return the output?
198 * @param bool Show the debug stack?
199 * @param integer Message width
200 *
201 * @return mixed Output or null
202 */
203 public static function Message($title, $message, $type, $return = false, $stack = true, $width = 500)
204 {
205 switch ($type)
206 {
207 // Message
208 case 1:
209 $prefix = 'Message';
210 $color = '#669900';
211 $font = '#000000';
212 break;
213
214 // Warning
215 case 2:
216 $prefix = 'Warning';
217 $color = '#003399';
218 $font = '#FFFFFF';
219 break;
220
221 case 3:
222 $prefix = 'Error';
223 $color = '#990000';
224 $font = '#EFEFEF';
225 break;
226 }
227
228 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;\">";
229 echo "\n<tr style=\"color: $font; text-align: left\">\n\t<td><strong>$prefix: $title</strong></td>\n</tr>";
230 echo "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>$message</td>\n</tr>";
231 if ($stack AND self::HasRegister() AND self::GetRegister()->getDebug())
232 {
233 echo "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td><strong>Debug Stack:</strong> <pre>";
234 debug_print_backtrace();
235 echo "</pre></td>\n</tr>";
236 }
237 echo "\n</table>\n<br />\n";
238 }
239
240 // ###################################################################
241 /**
242 * Custom error handler for ISSO; only handle E_WARNING, E_NOTICE,
243 * E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
244 *
245 * @param integer Error number
246 * @param string Error message string
247 * @param string File that contains the error
248 * @param string The line number of the error
249 * @param string The active symbol table at which point the error occurred
250 */
251 private function _error_handler($errno, $errstr, $errfile, $errline, $errcontext)
252 {
253 $level = ini_get('error_reporting');
254
255 switch ($errno)
256 {
257 // So we don't need to specify the error type in trigger_error(), all E_USER_NOTICEs
258 // are fatal and automatically kill the script
259 case E_USER_NOTICE:
260 $title = 'Fatal';
261 $mode = 3;
262 break;
263
264 // A non-fatal but important warning
265 case E_WARNING:
266 $title = 'Warning';
267 $mode = 2;
268 if (!($level & E_WARNING))
269 {
270 return;
271 }
272 break;
273
274 // A non-fatal notice that should all but be ignored unless in dev environments
275 case E_NOTICE:
276 case E_STRICT:
277 $title = 'Notice';
278 $mode = 1;
279 if (!($level & E_NOTICE))
280 {
281 return;
282 }
283 break;
284
285 case E_USER_WARNING:
286 case E_USER_NOTICE:
287 default:
288 trigger_error('The only error types supported are E_USER_NOTICE (fatal), E_WARNING, and E_NOTICE ' . $errno);
289 break;
290 }
291
292 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
293
294 $errstr = str_replace(array(getcwd(), dirname(getcwd())), '', $errstr);
295
296 self::Message($title, $errstr, $mode);
297
298 if ($errno == E_USER_NOTICE)
299 {
300 exit;
301 }
302 }
303
304 // ###################################################################
305 /**
306 * Creates a table that explains the error reporting levels and their
307 * state
308 */
309 public function explain_error_reporting()
310 {
311 $levels = array(
312 'E_ERROR' => E_ERROR,
313 'E_WARNING' => E_WARNING,
314 'E_PARSE' => E_PARSE,
315 'E_NOTICE' => E_NOTICE,
316 'E_CORE_ERROR' => E_CORE_ERROR,
317 'E_CORE_WARNING' => E_CORE_WARNING,
318 'E_COMPILE_ERROR' => E_COMPILE_ERROR,
319 'E_COMPILE_WARNING' => E_COMPILE_WARNING,
320 'E_USER_ERROR' => E_USER_ERROR,
321 'E_USER_WARNING' => E_USER_WARNING,
322 'E_USER_NOTICE' => E_USER_NOTICE,
323 'E_ALL' => E_ALL,
324 'E_STRICT' => E_STRICT
325 );
326
327 $table = '<table cellspacing="0" cellpadding="2" border="0">';
328
329 foreach ($levels AS $name => $value)
330 {
331 $table .= '
332 <tr>
333 <td>' . $name . '</td>
334 <td>' . (ini_get('error_reporting') & $value) . '</td>
335 </tr>';
336 }
337
338 $table .= '
339 </table>';
340
341 $this->message('Error Reporting', $table, 1);
342 }
343 }
344
345 /*=====================================================================*\
346 || ###################################################################
347 || # $HeadURL$
348 || # $Id$
349 || ###################################################################
350 \*=====================================================================*/
351 ?>