Removing a lot of old system variables from BSApp
[isso.git] / App.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2005-2008 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)2005 - 2008, 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 * Debug mode?
79 * @var bool
80 */
81 private $debug = false;
82
83 /**
84 * The master registry list
85 * @var array
86 */
87 private $registry = null;
88
89 /**
90 * An array of debug messages
91 * @var array
92 */
93 private $debugInfo = array();
94
95 // ###################################################################
96 /**
97 * Constructor
98 */
99 private function __construct()
100 {
101 $this->registry = new BSVariableRegistry();
102 }
103
104 // ###################################################################
105 /**
106 * Returns the single instance of the register
107 *
108 * @param string A string param
109 *
110 * @return object BSApp instance
111 */
112 private static function _instance()
113 {
114 if (!self::$instance)
115 {
116 self::$instance = new BSApp();
117 }
118 return self::$instance;
119 }
120
121 // ###################################################################
122 /**
123 * Sets the debug state
124 *
125 * @param bool Debug mode on?
126 */
127 public static function set_debug($debug)
128 {
129 self::_instance()->debug = $debug;
130 }
131
132 // ###################################################################
133 /**
134 * Gets the debug mode state
135 *
136 * @return bool Debug mode on?
137 */
138 public static function get_debug()
139 {
140 return self::_instance()->debug;
141 }
142
143 // ###################################################################
144 /**
145 * Returns the registry object without it ever being able to be cleared
146 * or unset
147 *
148 * @return StdClass
149 */
150 public static function registry()
151 {
152 return self::_instance()->registry;
153 }
154
155 // ###################################################################
156 /**
157 * Adds a debug message to the array. This only works when debug mode
158 * is enabled.
159 *
160 * @param string Debug message
161 */
162 public static function debug($msg)
163 {
164 if (self::_instance()->debug)
165 {
166 self::_instance()->debugInfo[] = $msg;
167 }
168 }
169
170 // ###################################################################
171 /**
172 * Returns a <select> menu of all the debug notices
173 *
174 * @return string Debug list
175 */
176 public static function get_debug_list()
177 {
178 $output = '<select><option>Debug Notices (' . sizeof(self::_instance()->debugInfo) . ')</option>';
179 foreach (self::_instance()->debugInfo AS $notice)
180 {
181 $output .= "<option>--- $notice</option>";
182 }
183 return "$output</select>";
184 }
185
186 // ###################################################################
187 /**
188 * Loads the specified module.
189 *
190 * @param string Module name
191 *
192 * @return object Instantiated module
193 */
194 public static function load_module($name)
195 {
196 if (self::get_debug())
197 {
198 include_once(ISSO . "/$name.php");
199 }
200 else
201 {
202 @include_once(ISSO . "/$name.php");
203 }
204
205 $class = "BS$name";
206
207 if (!class_exists($class))
208 {
209 throw new Exception('Specifed module does not conform to the ISSO specification, or the class is missing');
210 return;
211 }
212
213 return new $class;
214 }
215
216 // ###################################################################
217 /**
218 * This function is used by other framework modules to check and see if
219 * the passed array of module names have been loaded. If not, this will
220 * throw an error informing the developer that the given modules are
221 * required in order for the framework to work.
222 *
223 * @param array Array of module names to check for loadedness
224 */
225 public static function required_modules($modules)
226 {
227 foreach ($modules AS $module)
228 {
229 if (self::registry()->getType($module) == null)
230 {
231 throw new Exception('The ' . $module . ' module is required in order to use this framework module');
232 }
233 }
234 }
235 }
236
237 /**
238 * Variable Registry
239 *
240 * This class can be used to store unlimited number of properties. It is a (make believe)
241 * inner class to BSApp and cannot be instantiated outside of it.
242 *
243 * @author Blue Static
244 * @copyright Copyright (c)2005 - 2008, Blue Static
245 * @version $Id$
246 * @package ISSO
247 *
248 */
249 class BSVariableRegistry
250 {
251 // ###################################################################
252 /**
253 * Constructor: only able to be called by BSApp
254 */
255 public function __construct()
256 {
257 $bt = debug_backtrace();
258 if (!isset($bt[1]['class']) OR $bt[1]['class'] != 'BSApp')
259 {
260 exit('You cannot instantiate ' . __CLASS__ . ' directly. It is an internal class.');
261 }
262 }
263
264 // ###################################################################
265 /**
266 * If you try and get a non-existent property, throw a new exception
267 */
268 public function __get($name)
269 {
270 throw new Exception('Failed to get nonexistent property "' . $name . '"');
271 }
272
273 // ###################################################################
274 /**
275 * Returns the first value of the given type in the variable registry
276 *
277 * @return mixed
278 */
279 public function getType($class)
280 {
281 $class = 'BS' . $class;
282 foreach ($this AS $object)
283 {
284 if ($object instanceof $class)
285 {
286 return $object;
287 }
288 }
289 }
290
291 // ###################################################################
292 /**
293 * Returns all the objects in the registry by iterating over it
294 *
295 * @return array
296 */
297 public function allObjects()
298 {
299 $registry = array();
300 foreach ($this AS $key => $value)
301 {
302 $registry[$key] = $value;
303 }
304 return $registry;
305 }
306 }
307
308 ?>