Fixing a call to BSApp::GetType() that no longer exists
[isso.git] / App.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
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 Application Root (App.php)
24 *
25 * Constants:
26 * ISSO_MT_START - Define the microtime() value at the top of your
27 * script and this will calculate the total execution
28 * time
29 * SVN - Place SVN $Id keyword to get SVN revision information on output
30 *
31 * @package ISSO
32 */
33
34 // we need PHP 5.2.0 to run
35 if (!function_exists('array_fill_keys'))
36 {
37 print('You need PHP version 5.2.0 or newer to run ISSO');
38 exit;
39 }
40
41 // get rid of register_globals
42 if ((bool)ini_get('register_globals') === true)
43 {
44 $superglobals = array('_GET', '_COOKIE', '_FILES', '_POST', '_SERVER', '_ENV');
45 foreach ($superglobals AS $global)
46 {
47 if (is_array(${$global}))
48 {
49 foreach (${$global} AS $_key => $_val)
50 {
51 if (isset(${$_key}))
52 {
53 unset(${$_key});
54 }
55 }
56 }
57 }
58 }
59
60 require_once('ISSO/Functions.php');
61
62 /**
63 * Application Class
64 *
65 * This is an ISSO application class. It holds all of the ISSO system variables as well
66 * as serving as an object registry that is avaliable in the global scope to prevent
67 * globalization of variables. There can only be one instance of this existing
68 * at any given time.
69 *
70 * @author Blue Static
71 * @copyright Copyright ©2002 - [#]year[#], Blue Static
72 * @version $Revision$
73 * @package ISSO
74 *
75 */
76 class BSApp
77 {
78 /**
79 * Instance of this class
80 * @var object
81 */
82 private static $instance;
83
84 /**
85 * Application name
86 * @var string
87 */
88 private $application = 'Unknown ISSO Project';
89
90 /**
91 * Application path
92 * @var string
93 */
94 private $appPath;
95
96 /**
97 * Application version
98 * @var string
99 */
100 private $appVersion;
101
102 /**
103 * Web path
104 * @var string
105 */
106 private $webPath;
107
108 /**
109 * Debug mode?
110 * @var bool
111 */
112 private $debug = false;
113
114 /**
115 * The master registry list
116 * @var array
117 */
118 private $registry = null;
119
120 /**
121 * An array of debug messages
122 * @var array
123 */
124 private $debugInfo = array();
125
126 // ###################################################################
127 /**
128 * Constructor
129 */
130 private function __construct()
131 {
132 $this->registry = new BSVariableRegistry();
133 }
134
135 // ###################################################################
136 /**
137 * Returns the single instance of the register
138 *
139 * @param string A string param
140 *
141 * @return object BSApp instance
142 */
143 private static function _Instance()
144 {
145 if (!self::$instance)
146 {
147 self::$instance = new BSApp();
148 }
149 return self::$instance;
150 }
151
152 // ###################################################################
153 /**
154 * Sets the application name
155 *
156 * @param string Application name
157 */
158 public static function SetApplication($name)
159 {
160 self::_Instance()->application = $name;
161 }
162
163 // ###################################################################
164 /**
165 * Gets the application name
166 *
167 * @return string Application name
168 */
169 public static function GetApplication()
170 {
171 return self::_Instance()->application;
172 }
173
174 // ###################################################################
175 /**
176 * Sets the application's working path
177 *
178 * @param string Path
179 */
180 public static function SetAppPath($path)
181 {
182 self::_Instance()->appPath = BSFunctions::FetchSourcePath($path);
183 }
184
185 // ###################################################################
186 /**
187 * Returns the path to the application
188 *
189 * @return string Application path
190 */
191 public static function GetAppPath()
192 {
193 return self::_Instance()->appPath;
194 }
195
196 // ###################################################################
197 /**
198 * Sets the application version
199 *
200 * @param string Application version
201 */
202 public static function SetAppVersion($vers)
203 {
204 self::_Instance()->appVersion = $vers;
205 }
206
207 // ###################################################################
208 /**
209 * Gets the application version
210 *
211 * @return string Application version
212 */
213 public static function GetAppVersion()
214 {
215 return self::_Instance()->appVersion;
216 }
217
218 // ###################################################################
219 /**
220 * Sets the application's web path, which is the full path from the
221 * server's web root
222 *
223 * @param string Path
224 */
225 public static function SetWebPath($path)
226 {
227 self::_Instance()->webPath = BSFunctions::FetchSourcePath($path);
228 }
229
230 // ###################################################################
231 /**
232 * Returns the web path to the application
233 *
234 * @return string Application's web path
235 */
236 public static function GetWebPath()
237 {
238 return self::_Instance()->webPath;
239 }
240
241 // ###################################################################
242 /**
243 * Sets the debug state
244 *
245 * @param bool Debug mode on?
246 */
247 public static function SetDebug($debug)
248 {
249 self::_Instance()->debug = $debug;
250 }
251
252 // ###################################################################
253 /**
254 * Gets the debug mode state
255 *
256 * @return bool Debug mode on?
257 */
258 public static function GetDebug()
259 {
260 return self::_Instance()->debug;
261 }
262
263 // ###################################################################
264 /**
265 * Returns the registry object without it ever being able to be cleared
266 * or unset
267 *
268 * @return StdClass
269 */
270 public static function Registry()
271 {
272 return self::_Instance()->registry;
273 }
274
275 // ###################################################################
276 /**
277 * Adds a debug message to the array. This only works when debug mode
278 * is enabled.
279 *
280 * @param string Debug message
281 */
282 public static function Debug($msg)
283 {
284 if (self::_Instance()->debug)
285 {
286 self::_Instance()->debugInfo[] = $msg;
287 }
288 }
289
290 // ###################################################################
291 /**
292 * Returns a <select> menu of all the debug notices
293 *
294 * @return string Debug list
295 */
296 public static function GetDebugList()
297 {
298 $output = '<select><option>Debug Notices (' . sizeof(self::_Instance()->debugInfo) . ')</option>';
299 foreach (self::_Instance()->debugInfo AS $notice)
300 {
301 $output .= "<option>--- $notice</option>";
302 }
303 return "$output</select>";
304 }
305
306 // ###################################################################
307 /**
308 * Loads the specified module.
309 *
310 * @param string Module name
311 *
312 * @return object Instantiated module
313 */
314 public static function LoadModule($name)
315 {
316 if (self::GetDebug())
317 {
318 include_once("ISSO/$name.php");
319 }
320 else
321 {
322 @include_once("ISSO/$name.php");
323 }
324
325 $class = "BS$name";
326
327 if (!class_exists($class))
328 {
329 throw new Exception('Specifed module does not conform to the ISSO specification, or the class is missing');
330 return;
331 }
332
333 return new $class;
334 }
335
336 // ###################################################################
337 /**
338 * This function is used by other framework modules to check and see if
339 * the passed array of module names have been loaded. If not, this will
340 * throw an error informing the developer that the given modules are
341 * required in order for the framework to work.
342 *
343 * @param array Array of module names to check for loadedness
344 */
345 public static function RequiredModules($modules)
346 {
347 foreach ($modules AS $module)
348 {
349 if (self::Registry()->getType($module) == null AND !class_exists("BS$module"))
350 {
351 throw new Exception('The ' . $module . ' module is required in order to use this framework module');
352 }
353 }
354 }
355 }
356
357 /**
358 * Variable Registry
359 *
360 * This class can be used to store unlimited number of properties. It is a (make believe)
361 * inner class to BSApp and cannot be instantiated outside of it.
362 *
363 * @author Blue Static
364 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
365 * @version $Id$
366 * @package ISSO
367 *
368 */
369 class BSVariableRegistry
370 {
371 // ###################################################################
372 /**
373 * Constructor: only able to be called by BSApp
374 */
375 public function __construct()
376 {
377 $bt = debug_backtrace();
378 if (!isset($bt[1]['class']) OR $bt[1]['class'] != 'BSApp')
379 {
380 exit('You cannot instantiate ' . __CLASS__ . ' directly. It is an internal class.');
381 }
382 }
383
384 // ###################################################################
385 /**
386 * If you try and get a non-existent property, throw a new exception
387 */
388 public function __get($name)
389 {
390 throw new Exception('Failed to get nonexistent property "' . $name . '"');
391 }
392
393 // ###################################################################
394 /**
395 * Returns the first value of the given type in the variable registry
396 *
397 * @return mixed
398 */
399 public function getType($class)
400 {
401 $class = 'BS' . $class;
402 foreach ($this AS $object)
403 {
404 if ($object instanceof $class)
405 {
406 return $object;
407 }
408 }
409 }
410
411 // ###################################################################
412 /**
413 * Returns all the objects in the registry by iterating over it
414 *
415 * @return array
416 */
417 public function allObjects()
418 {
419 $registry = array();
420 foreach ($this AS $key => $value)
421 {
422 $registry[$key] = $value;
423 }
424 return $registry;
425 }
426 }
427
428 /*=====================================================================*\
429 || ###################################################################
430 || # $HeadURL$
431 || # $Id$
432 || ###################################################################
433 \*=====================================================================*/
434 ?>