BSRegister::GetType() will be responsible for adding the BS prefix to class names...
[isso.git] / Register.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 * ISSO Registry (Register.php)
24 *
25 * @package ISSO
26 */
27
28 // we need PHP5 to run
29 if (!function_exists('stripos'))
30 {
31 trigger_error('You need PHP version 5.0.0 or newer to run ISSO', E_USER_ERROR);
32 exit;
33 }
34
35 // get rid of register_globals
36 if ((bool)ini_get('register_globals') === true)
37 {
38 $superglobals = array('_GET', '_COOKIE', '_FILES', '_POST', '_SERVER', '_ENV');
39 foreach ($superglobals AS $global)
40 {
41 if (is_array(${$global}))
42 {
43 foreach (${$global} AS $_key => $_val)
44 {
45 if (isset(${$_key}))
46 {
47 unset(${$_key});
48 }
49 }
50 }
51 }
52 }
53
54 require_once('ISSO/Functions.php');
55
56 /**
57 * Register Class
58 *
59 * This is an ISSO registry class. It holds all of the ISSO system variables as well
60 * as serving as an object registry that is avaliable in the global scope to prevent
61 * globalization of variables. There can only be one instance of this existing
62 * at any given time.
63 *
64 * @author Blue Static
65 * @copyright Copyright ©2002 - [#]year[#], Blue Static
66 * @version $Revision$
67 * @package ISSO
68 *
69 */
70 class BSRegister
71 {
72 /**
73 * Instance of this class
74 * @var object
75 */
76 private static $instance;
77
78 /**
79 * Application name
80 * @var string
81 */
82 private $application = 'Unknown ISSO Project';
83
84 /**
85 * Application path
86 * @var string
87 */
88 private $appPath;
89
90 /**
91 * Application version
92 * @var string
93 */
94 private $appVersion;
95
96 /**
97 * Web path
98 * @var string
99 */
100 private $webPath;
101
102 /**
103 * Debug mode?
104 * @var bool
105 */
106 private $debug = false;
107
108 /**
109 * The master registry list
110 * @var array
111 */
112 private $registry = array();
113
114 /**
115 * An array of debug messages
116 * @var array
117 */
118 private $debugInfo = array();
119
120 // ###################################################################
121 /**
122 * Constructor
123 */
124 private function __construct() {}
125
126 // ###################################################################
127 /**
128 * Returns the single instance of the register
129 *
130 * @param string A string param
131 *
132 * @return object BSRegister instance
133 */
134 private static function Instance()
135 {
136 if (self::$instance == null)
137 {
138 self::$instance = new BSRegister();
139 set_error_handler(array(self::$instance, '_errorHandler'));
140 }
141 return self::$instance;
142 }
143
144 // ###################################################################
145 /**
146 * Sets the application name
147 *
148 * @param string Application name
149 */
150 public static function SetApplication($name)
151 {
152 self::Instance()->application = $name;
153 }
154
155 // ###################################################################
156 /**
157 * Gets the application name
158 *
159 * @return string Application name
160 */
161 public static function GetApplication()
162 {
163 return self::Instance()->application;
164 }
165
166 // ###################################################################
167 /**
168 * Sets the application's working path
169 *
170 * @param string Path
171 */
172 public static function SetAppPath($path)
173 {
174 self::Instance()->appPath = BSFunctions::FetchSourcePath($path);
175 }
176
177 // ###################################################################
178 /**
179 * Returns the path to the application
180 *
181 * @return string Application path
182 */
183 public static function GetAppPath()
184 {
185 return self::Instance()->appPath;
186 }
187
188 // ###################################################################
189 /**
190 * Sets the application version
191 *
192 * @param string Application version
193 */
194 public static function SetAppVersion($vers)
195 {
196 self::Instance()->appVersion = $vers;
197 }
198
199 // ###################################################################
200 /**
201 * Gets the application version
202 *
203 * @return string Application version
204 */
205 public static function GetAppVersion()
206 {
207 return self::Instance()->appVersion;
208 }
209
210 // ###################################################################
211 /**
212 * Sets the application's web path, which is the full path from the
213 * server's web root
214 *
215 * @param string Path
216 */
217 public static function SetWebPath($path)
218 {
219 self::Instance()->webPath = BSFunctions::FetchSourcePath($path);
220 }
221
222 // ###################################################################
223 /**
224 * Returns the web path to the application
225 *
226 * @return string Application's web path
227 */
228 public static function GetWebPath()
229 {
230 return self::Instance()->webPath;
231 }
232
233 // ###################################################################
234 /**
235 * Sets the debug state
236 *
237 * @param bool Debug mode on?
238 */
239 public static function SetDebug($debug)
240 {
241 self::Instance()->debug = $debug;
242 }
243
244 // ###################################################################
245 /**
246 * Gets the debug mode state
247 *
248 * @return bool Debug mode on?
249 */
250 public static function GetDebug()
251 {
252 return self::Instance()->debug;
253 }
254
255 // ###################################################################
256 /**
257 * Registers a value in the master registry. You cannot overwrite
258 * values. You must first unregister() them if you wish to do so.
259 *
260 * @param string Registry key
261 * @param mixed Value to register
262 */
263 public static function Register($key, $value)
264 {
265 if (isset(self::Instance()->registry["$key"]))
266 {
267 trigger_error('Cannot overwrite a key in the registry');
268 return;
269 }
270
271 self::Instance()->registry["$key"] = $value;
272 }
273
274 // ###################################################################
275 /**
276 * Unregisters a value from the registry. This removes all traces of
277 * it from this object.
278 *
279 * @param string Registry key
280 */
281 public static function Unregister($key)
282 {
283 if (!isset(self::Instance()->registry["$key"]))
284 {
285 trigger_error('You cannot unregister a key that does not exist');
286 return;
287 }
288
289 unset(self::Instance()->registry["$key"]);
290 }
291
292 // ###################################################################
293 /**
294 * This gets a value from the registry with a specific key
295 *
296 * @param string The key
297 *
298 * @return mixed Value in the registry for key
299 */
300 public static function Get($key)
301 {
302 if (!isset(self::Instance()->registry["$key"]))
303 {
304 trigger_error('Cannot access the registry with a non-existent key');
305 return;
306 }
307
308 return self::Instance()->registry["$key"];
309 }
310
311 // ###################################################################
312 /**
313 * Returns the first object of a specified class type
314 *
315 * @param string Class name
316 *
317 * @return object Object in the registry of that type
318 */
319 public function GetType($class)
320 {
321 $class = 'BS' . $class;
322 foreach (self::Instance()->registry AS $key => $object)
323 {
324 if ($object instanceof $class)
325 {
326 return $object;
327 }
328 }
329 }
330
331 // ###################################################################
332 /**
333 * Returns the entire registry stack
334 *
335 * @return array Complete registry
336 */
337 public static function GetAll()
338 {
339 return self::Instance()->registry;
340 }
341
342 // ###################################################################
343 /**
344 * Adds a debug message to the array. This only works when debug mode
345 * is enabled.
346 *
347 * @param string Debug message
348 */
349 public static function Debug($msg)
350 {
351 if (self::Instance()->debug)
352 {
353 self::Instance()->debugInfo[] = $msg;
354 }
355 }
356
357 // ###################################################################
358 /**
359 * Loads the specified module.
360 *
361 * @param string Module name
362 *
363 * @return object Instantiated module
364 */
365 public static function LoadModule($name)
366 {
367 @include_once("ISSO/$name.php");
368
369 $class = "BS$name";
370
371 if (!class_exists($class))
372 {
373 trigger_error('Specifed module does not conform to the ISSO specification, or the class is missing');
374 return;
375 }
376
377 return new $class;
378 }
379
380 // ###################################################################
381 /**
382 * Prints an ISSO message
383 *
384 * @param string The title of the message
385 * @param string The content of the message
386 * @param integer Type of message to be printed
387 * @param bool Return the output?
388 * @param bool Show the debug stack?
389 * @param integer Message width
390 *
391 * @return mixed Output or null
392 */
393 public static function Message($title, $message, $type, $return = false, $stack = true, $width = 500)
394 {
395 switch ($type)
396 {
397 // Message
398 case 1:
399 $prefix = 'Message';
400 $color = '#669900';
401 $font = '#000000';
402 break;
403
404 // Warning
405 case 2:
406 $prefix = 'Warning';
407 $color = '#003399';
408 $font = '#FFFFFF';
409 break;
410
411 case 3:
412 $prefix = 'Error';
413 $color = '#990000';
414 $font = '#EFEFEF';
415 break;
416 }
417
418 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;\">";
419 echo "\n<tr style=\"color: $font; text-align: left\">\n\t<td><strong>$prefix: $title</strong></td>\n</tr>";
420 echo "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>$message</td>\n</tr>";
421 if ($stack AND self::Instance()->getDebug())
422 {
423 echo "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td><strong>Debug Stack:</strong> <pre>";
424 debug_print_backtrace();
425 echo "</pre></td>\n</tr>";
426 }
427 echo "\n</table>\n<br />\n";
428 }
429
430 // ###################################################################
431 /**
432 * Custom error handler for ISSO; only handle E_WARNING, E_NOTICE,
433 * E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
434 *
435 * @param integer Error number
436 * @param string Error message string
437 * @param string File that contains the error
438 * @param string The line number of the error
439 * @param string The active symbol table at which point the error occurred
440 */
441 public function _errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
442 {
443 $level = ini_get('error_reporting');
444
445 switch ($errno)
446 {
447 // So we don't need to specify the error type in trigger_error(), all E_USER_NOTICEs
448 // are fatal and automatically kill the script
449 case E_USER_NOTICE:
450 $title = 'Fatal';
451 $mode = 3;
452 break;
453
454 // A non-fatal but important warning
455 case E_WARNING:
456 $title = 'Warning';
457 $mode = 2;
458 if (!($level & E_WARNING))
459 {
460 return;
461 }
462 break;
463
464 // A non-fatal notice that should all but be ignored unless in dev environments
465 case E_NOTICE:
466 case E_STRICT:
467 $title = 'Notice';
468 $mode = 1;
469 if (!($level & E_NOTICE))
470 {
471 return;
472 }
473 break;
474
475 case E_USER_WARNING:
476 case E_USER_NOTICE:
477 default:
478 trigger_error('The only error types supported are E_USER_NOTICE (fatal), E_WARNING, and E_NOTICE');
479 break;
480 }
481
482 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
483
484 $errstr = str_replace(array(getcwd(), dirname(getcwd())), '', $errstr);
485
486 self::Message($title, $errstr, $mode);
487
488 if ($errno == E_USER_NOTICE)
489 {
490 exit;
491 }
492 }
493
494 // ###################################################################
495 /**
496 * Creates a table that explains the error reporting levels and their
497 * state
498 */
499 public static function ExplainErrorReporting()
500 {
501 $levels = array(
502 'E_ERROR' => E_ERROR,
503 'E_WARNING' => E_WARNING,
504 'E_PARSE' => E_PARSE,
505 'E_NOTICE' => E_NOTICE,
506 'E_CORE_ERROR' => E_CORE_ERROR,
507 'E_CORE_WARNING' => E_CORE_WARNING,
508 'E_COMPILE_ERROR' => E_COMPILE_ERROR,
509 'E_COMPILE_WARNING' => E_COMPILE_WARNING,
510 'E_USER_ERROR' => E_USER_ERROR,
511 'E_USER_WARNING' => E_USER_WARNING,
512 'E_USER_NOTICE' => E_USER_NOTICE,
513 'E_ALL' => E_ALL,
514 'E_STRICT' => E_STRICT
515 );
516
517 $table = '<table cellspacing="0" cellpadding="2" border="0">';
518
519 foreach ($levels AS $name => $value)
520 {
521 $table .= '
522 <tr>
523 <td>' . $name . '</td>
524 <td>' . (ini_get('error_reporting') & $value) . '</td>
525 </tr>';
526 }
527
528 $table .= '
529 </table>';
530
531 self::Message('Error Reporting', $table, 1);
532 }
533
534 // ###################################################################
535 /**
536 * This function is used by other framework modules to check and see if
537 * the passed array of module names have been loaded. If not, this will
538 * throw an error informing the developer that the given modules are
539 * required in order for the framework to work.
540 *
541 * @param array Array of module names to check for loadedness
542 */
543 public function RequiredModules($modules)
544 {
545 foreach ($modules AS $module)
546 {
547 if (self::GetType($module) == null)
548 {
549 trigger_error('The ' . $module . ' is required in order to use this framework module');
550 }
551 }
552 }
553 }
554
555 /*=====================================================================*\
556 || ###################################################################
557 || # $HeadURL$
558 || # $Id$
559 || ###################################################################
560 \*=====================================================================*/
561 ?>