Added RequiredModules() to check and see if a set of framework modules is loaded
[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 foreach (self::Instance()->registry AS $key => $object)
322 {
323 if ($object instanceof $class)
324 {
325 return $object;
326 }
327 }
328 }
329
330 // ###################################################################
331 /**
332 * Returns the entire registry stack
333 *
334 * @return array Complete registry
335 */
336 public static function GetAll()
337 {
338 return self::Instance()->registry;
339 }
340
341 // ###################################################################
342 /**
343 * Adds a debug message to the array. This only works when debug mode
344 * is enabled.
345 *
346 * @param string Debug message
347 */
348 public static function Debug($msg)
349 {
350 if (self::Instance()->debug)
351 {
352 self::Instance()->debugInfo[] = $msg;
353 }
354 }
355
356 // ###################################################################
357 /**
358 * Loads the specified module.
359 *
360 * @param string Module name
361 *
362 * @return object Instantiated module
363 */
364 public static function LoadModule($name)
365 {
366 @include_once("ISSO/$name.php");
367
368 $class = "BS$name";
369
370 if (!class_exists($class))
371 {
372 trigger_error('Specifed module does not conform to the ISSO specification, or the class is missing');
373 return;
374 }
375
376 return new $class;
377 }
378
379 // ###################################################################
380 /**
381 * Prints an ISSO message
382 *
383 * @param string The title of the message
384 * @param string The content of the message
385 * @param integer Type of message to be printed
386 * @param bool Return the output?
387 * @param bool Show the debug stack?
388 * @param integer Message width
389 *
390 * @return mixed Output or null
391 */
392 public static function Message($title, $message, $type, $return = false, $stack = true, $width = 500)
393 {
394 switch ($type)
395 {
396 // Message
397 case 1:
398 $prefix = 'Message';
399 $color = '#669900';
400 $font = '#000000';
401 break;
402
403 // Warning
404 case 2:
405 $prefix = 'Warning';
406 $color = '#003399';
407 $font = '#FFFFFF';
408 break;
409
410 case 3:
411 $prefix = 'Error';
412 $color = '#990000';
413 $font = '#EFEFEF';
414 break;
415 }
416
417 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;\">";
418 echo "\n<tr style=\"color: $font; text-align: left\">\n\t<td><strong>$prefix: $title</strong></td>\n</tr>";
419 echo "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>$message</td>\n</tr>";
420 if ($stack AND self::Instance()->getDebug())
421 {
422 echo "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td><strong>Debug Stack:</strong> <pre>";
423 debug_print_backtrace();
424 echo "</pre></td>\n</tr>";
425 }
426 echo "\n</table>\n<br />\n";
427 }
428
429 // ###################################################################
430 /**
431 * Custom error handler for ISSO; only handle E_WARNING, E_NOTICE,
432 * E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
433 *
434 * @param integer Error number
435 * @param string Error message string
436 * @param string File that contains the error
437 * @param string The line number of the error
438 * @param string The active symbol table at which point the error occurred
439 */
440 public function _errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
441 {
442 $level = ini_get('error_reporting');
443
444 switch ($errno)
445 {
446 // So we don't need to specify the error type in trigger_error(), all E_USER_NOTICEs
447 // are fatal and automatically kill the script
448 case E_USER_NOTICE:
449 $title = 'Fatal';
450 $mode = 3;
451 break;
452
453 // A non-fatal but important warning
454 case E_WARNING:
455 $title = 'Warning';
456 $mode = 2;
457 if (!($level & E_WARNING))
458 {
459 return;
460 }
461 break;
462
463 // A non-fatal notice that should all but be ignored unless in dev environments
464 case E_NOTICE:
465 case E_STRICT:
466 $title = 'Notice';
467 $mode = 1;
468 if (!($level & E_NOTICE))
469 {
470 return;
471 }
472 break;
473
474 case E_USER_WARNING:
475 case E_USER_NOTICE:
476 default:
477 trigger_error('The only error types supported are E_USER_NOTICE (fatal), E_WARNING, and E_NOTICE');
478 break;
479 }
480
481 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
482
483 $errstr = str_replace(array(getcwd(), dirname(getcwd())), '', $errstr);
484
485 self::Message($title, $errstr, $mode);
486
487 if ($errno == E_USER_NOTICE)
488 {
489 exit;
490 }
491 }
492
493 // ###################################################################
494 /**
495 * Creates a table that explains the error reporting levels and their
496 * state
497 */
498 public static function ExplainErrorReporting()
499 {
500 $levels = array(
501 'E_ERROR' => E_ERROR,
502 'E_WARNING' => E_WARNING,
503 'E_PARSE' => E_PARSE,
504 'E_NOTICE' => E_NOTICE,
505 'E_CORE_ERROR' => E_CORE_ERROR,
506 'E_CORE_WARNING' => E_CORE_WARNING,
507 'E_COMPILE_ERROR' => E_COMPILE_ERROR,
508 'E_COMPILE_WARNING' => E_COMPILE_WARNING,
509 'E_USER_ERROR' => E_USER_ERROR,
510 'E_USER_WARNING' => E_USER_WARNING,
511 'E_USER_NOTICE' => E_USER_NOTICE,
512 'E_ALL' => E_ALL,
513 'E_STRICT' => E_STRICT
514 );
515
516 $table = '<table cellspacing="0" cellpadding="2" border="0">';
517
518 foreach ($levels AS $name => $value)
519 {
520 $table .= '
521 <tr>
522 <td>' . $name . '</td>
523 <td>' . (ini_get('error_reporting') & $value) . '</td>
524 </tr>';
525 }
526
527 $table .= '
528 </table>';
529
530 self::Message('Error Reporting', $table, 1);
531 }
532
533 // ###################################################################
534 /**
535 * This function is used by other framework modules to check and see if
536 * the passed array of module names have been loaded. If not, this will
537 * throw an error informing the developer that the given modules are
538 * required in order for the framework to work.
539 *
540 * @param array Array of module names to check for loadedness
541 */
542 public function RequiredModules($modules)
543 {
544 foreach ($modules AS $module)
545 {
546 if (self::GetType($module) == null)
547 {
548 trigger_error('The ' . $module . ' is required in order to use this framework module');
549 }
550 }
551 }
552 }
553
554 /*=====================================================================*\
555 || ###################################################################
556 || # $HeadURL$
557 || # $Id$
558 || ###################################################################
559 \*=====================================================================*/
560 ?>