Added debug information notices. Created iff() wrapper. Template system done.
[isso.git] / kernel.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # All parts of this file are ©2003-[#]year[#] Iris Studios, Inc. No # ||
7 || # part of this file may be reproduced in any way: part or whole. # ||
8 || # --------------------------------------------------------------- # ||
9 || # ©2003 - [#]year[#] Iris Studios, Inc. | http://www.iris-studios.com # ||
10 || ################################################################### ||
11 \*=====================================================================*/
12
13 define('ERR_FATAL', E_USER_ERROR);
14 define('ERR_ERROR', E_USER_WARNING);
15 define('ERR_WARNING', E_USER_NOTICE);
16
17 /**
18 * Iris Studios Shared Object Framework (ISSO)
19 *
20 * This framework allows a common backend to be used amongst all Iris
21 * Studios applications and can is built to be abstract and flexible.
22 * The base framework handles all loading and module management.
23 *
24 * @author Iris Studios, Inc.
25 * @copyright Copyright ©2003 - [#]year[#], Iris Studios, Inc.
26 * @version $Revision$
27 *
28 */
29 class Shared_Object_Framework
30 {
31 /**
32 * Global environment variables
33 *
34 * This is where we keep the global variables that are used within the shared framework
35 *
36 * @var version ISSO version
37 * @var sourcepath The location of the framework sources
38 * @var appath The path to the application's location
39 * @var application The name of the application that is using the framework
40 * @var debug Variable for debug mode
41 * @var debuginfo Listing of all debug notices
42 * @var modules An array of loaded framework modules
43 */
44 var $version = '[#]version[#]';
45 var $sourcepath = '';
46 var $apppath = '';
47 var $application = '';
48 var $debug = false;
49 var $debuginfo = array();
50 var $modules = array();
51
52 /**
53 * Constructor
54 */
55 function Shared_Object_Framework()
56 {
57 set_error_handler(array(&$this, '_error_handler'));
58 $this->modules['kernel'] = 'Shared Object Framework Core';
59 }
60
61 /**
62 * Prepares a path for being set as the sourcepath
63 *
64 * @param str Source path or URL
65 *
66 * @return str Prepared source path
67 */
68 function fetch_sourcepath($source)
69 {
70 if (substr($source, strlen($source) - 1) != '/')
71 {
72 $source .= '/';
73 }
74 return $source;
75 }
76
77 /**
78 * Loads a framework extension
79 *
80 * @param str Name of the framework
81 */
82 function load($framework)
83 {
84 if (!$this->is_loaded($framework))
85 {
86 $newobj = $this->locate($framework);
87 $this->$newobj['OBJ'] = new $newobj['CLASS']();
88 $GLOBALS["$newobj[OBJ]"] =& $this->$newobj['OBJ'];
89 $this->modules["$framework"] = $newobj['OBJECT'];
90 }
91 }
92
93 /**
94 * Includes a framework module. Module definitions need three variables:
95 * class, object, and obj. Class is the name of the class, object is
96 * the name human-readable name, and obj is the name that the module
97 * should be initialized as; this is used in class extensions.
98 *
99 * @param str Name of the framework
100 *
101 * @return array List of initialization variables
102 */
103 function locate($framework)
104 {
105 require_once($this->sourcepath . $framework . '.php');
106 return array('CLASS' => $CLASS, 'OBJECT' => $OBJECT, 'OBJ' => $OBJ);
107 }
108
109 /**
110 * Prints a list of all currently loaded framework modules
111 *
112 * @param bool Return the data as an array?
113 *
114 * @return mixed HTML output or an array of loaded modules
115 */
116 function show_modules($return = false)
117 {
118 if ($return)
119 {
120 return $this->modules;
121 }
122 else
123 {
124 $output = "\n\n<ul>\n\t<li>";
125 $output .= implode("</li>\n\t<li>", $this->modules);
126 $output .= "</li>\n</ul>\n\n";
127 $this->_message('Loaded Modules', $output, 1);
128 }
129 }
130
131 /**
132 * Verifies to see if a framework has been loaded
133 *
134 * @param str Framework name
135 *
136 * @return bool Whether or not the framework has been loaded
137 */
138 function is_loaded($framework)
139 {
140 if (isset($this->modules["$framework"]))
141 {
142 return true;
143 }
144 else
145 {
146 return false;
147 }
148 }
149
150 /**
151 * Prints an ISSO message
152 *
153 * @param str The title of the message
154 * @param str The content of the message
155 * @param int Type of message to be printed
156 * @param bool Return the output?
157 *
158 * @return mixed Output or null
159 */
160 function _message($title, $message, $type, $return = false)
161 {
162 switch ($type)
163 {
164 // Message
165 case 1:
166 $prefix = 'Message';
167 $color = '#669900';
168 $font = '#000000';
169 break;
170
171 // Warning
172 case 2:
173 $prefix = 'Warning';
174 $color = '#003399';
175 $font = '#FFFFFF';
176 break;
177
178 case 3:
179 $prefix = 'Error';
180 $color = '#990000';
181 $font = '#EFEFEF';
182 break;
183 }
184
185 $output = "\n<br />\n<table cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"500\" style=\"background-color: $color; font-family: Verdana, sans-serif; font-size: 12px;\">";
186 $output .= "\n<tr style=\"color: $font\">\n\t<td><strong>$prefix: $title</strong></td>\n</tr>";
187 $output .= "\n<tr style=\"background-color: #FFFFFF\">\n\t<td>$message</td>\n</tr>\n</table>\n<br />\n";
188
189 if ($return)
190 {
191 return $output;
192 }
193 else
194 {
195 print($output);
196 }
197 }
198
199 /**
200 * Custom error handler for ISSO
201 *
202 * @param int Error number
203 * @param str Error message string
204 * @param str File that contains the error
205 * @param str The line number of the error
206 * @param str The active symbol table at which point the error occurred
207 */
208 function _error_handler($errno, $errstr, $errfile, $errline)
209 {
210 switch ($errno)
211 {
212 // Fatal
213 case ERR_FATAL:
214 $title = 'Fatal';
215 break;
216
217 // Error
218 case ERR_ERROR:
219 $title = 'Alert';
220 break;
221
222 // Warning
223 case ERR_WARNING:
224 default:
225 $title = 'Warning';
226 break;
227 }
228
229 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
230
231 $this->_message($title, $errstr, 3);
232 }
233
234 /**
235 * Logs a debug message for verbose output
236 *
237 * @param str Message
238 */
239 function debug($message)
240 {
241 if ($this->debug)
242 {
243 $this->debuginfo[] = $message;
244 }
245 }
246 }
247
248 /**
249 * Global callback used for module calls back to the kernel
250 */
251 $_isso = new Shared_Object_Framework();
252
253 /**
254 * Wrapper for ternary operator that has to be in the global scope
255 *
256 * @param expr Expression
257 * @param mixed If the expression is true
258 * @param mixed If the expression is false
259 *
260 * @return mixed True or false data
261 */
262 function iff($condition, $iftrue, $iffalse = null)
263 {
264 return ($condition) ? ($iftrue) : ($iffalse);
265 }
266
267 /*=====================================================================*\
268 || ###################################################################
269 || # $HeadURL$
270 || # $Id$
271 || ###################################################################
272 \*=====================================================================*/
273 ?>