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