]> src.bluestatic.org Git - isso.git/blob - Loader.php
- Making SharedInstance() static-friendly
[isso.git] / Loader.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 * System loader (Loader.php)
24 *
25 * @package ISSO
26 */
27
28 /**
29 * System Loader
30 *
31 * This class contains static methods that are used to create new registers and
32 * load modules.
33 *
34 * @author Blue Static
35 * @copyright Copyright ©2002 - [#]year[#], Blue Static
36 * @version $Revision$
37 * @package ISSO
38 *
39 */
40 class BSLoader
41 {
42 /**
43 * Singleton instance
44 * @var object
45 */
46 private static $instance;
47
48 /**
49 * Array of all the registers
50 * @var array
51 */
52 private $registers = array();
53
54 /**
55 * The main register
56 * @var object
57 */
58 private $main;
59
60 // ###################################################################
61 /**
62 * Constructor
63 */
64 private function __construct() {}
65
66 // ###################################################################
67 /**
68 * Returns the shared instance of the BSLoader singleton class.
69 *
70 * @return object The BSLoader shared instance
71 */
72 private function SharedInstance()
73 {
74 if (!self::$instance)
75 {
76 self::$instance = new BSLoader;
77 }
78 return self::$instance;
79 }
80
81 // ###################################################################
82 /**
83 * Creates a new BSRegister instance and returns it.
84 *
85 * @return object New register
86 */
87 public static function NewRegister()
88 {
89 require_once('ISSO/Register.php');
90
91 self::SharedInstance()->registers[] = new BSRegister();
92
93 return end(self::SharedInstance()->registers);
94 }
95
96 // ###################################################################
97 /**
98 * Returns the array of all the registers
99 *
100 * @return array The array of all the registers
101 */
102 public static function GetAllRegisters()
103 {
104 return self::SharedInstance()->registers;
105 }
106
107 // ###################################################################
108 /**
109 * Sets the main register
110 *
111 * @param object New main register
112 */
113 public static function SetRegister(BSRegister $newreg)
114 {
115 if (get_class($newreg) != 'BSRegister')
116 {
117 throw new Exception('BSLoader::SetRegister() was not passed a BSRegister object');
118 }
119
120 self::SharedInstance()->main = $register;
121 }
122
123 // ###################################################################
124 /**
125 * Gets the main register if no argument is passed, or an arbitrary
126 * one if the index of a register is passed.
127 *
128 * @param integer Register index
129 *
130 * @return object Specified register
131 */
132 public static function &GetRegister($index = null)
133 {
134 if (is_null($index))
135 {
136 if (!isset(self::SharedInstance()->main))
137 {
138 throw new Exception('Cannot fetch the main register because it has not been set with BSLoader::SetRegister()');
139 }
140 return self::SharedInstance()->main;
141 }
142 else
143 {
144 if (!isset(self::SharedInstance()->registers["$index"]))
145 {
146 throw new Exception('Invalid register index passed to BSLoader::GetRegister()');
147 }
148 return self::SharedInstance()->registers["$index"];
149 }
150 }
151
152 // ###################################################################
153 /**
154 * Loads the specified module.
155 *
156 * @param string Module name
157 *
158 * @return object Instantiated module
159 */
160 public static function LoadModule($name)
161 {
162 if (!file_exists("ISSO/$name.php"))
163 {
164 throw new Exception('Specified module does not exist');
165 }
166
167 require_once("ISSO/$name.php");
168
169 $class = "BS$name";
170
171 if (!class_exists($class))
172 {
173 throw new Exception('Specifed module does not conform to the ISSO specification, or the class is missing');
174 }
175
176 return new $class;
177 }
178 }
179
180 /*=====================================================================*\
181 || ###################################################################
182 || # $HeadURL$
183 || # $Id$
184 || ###################################################################
185 \*=====================================================================*/
186 ?>