1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397 | <?php
/*
* Framework SuperObject
*
* The Framework SuperObject is a class implemented using the singleton
* design pattern which allows custom libraries to be written and be
* "loaded" and made available throughout the entire site in a simple and
* intuitive manner.
*
* @package FrameworkSuperObject
* @author Alan Briolat <alan@codescape.net>
* @copyright Copyright (c) 2007, Alan Briolat
* @license http://www.gnu.org/copyleft/gpl.html
* @link http://www.codeigniter.net/projects/ystv/fso
*/
// ------------------------------------------------------------------------
/**
* Constants
*/
define('FSO_BASEDIR', dirname(__FILE__));
/**
* Get FSO instance
*
* @return FSO
*/
function &get_instance()
{
return FSO::get_instance();
}
// ------------------------------------------------------------------------
/**
* Framework SuperObject Singleton Class
*/
class FSO
{
private static $_instance = null;
private static $_modules = array();
// {{{ constructor
/**
* Class constructor
*
* Constructor cannot be used from outside of the class - use
* FSO::get_instance() instead
*
* @return void
*/
protected function __construct()
{
}
// }}}
// {{{ public static function __get( $module )
/**
* Get module
*
* Overload __get() so that $FSO->somemodule actually uses the relevant
* module. To assign the module to a variable, use:
* $mymodule =& FSO::$modulename;
* or
* $mymodule =& $FSO->modulename;
*
* @param string $module
* @return Object
*/
public static function __get( $module )
{
$module = strtolower($module);
if ( array_key_exists($module, self::$_modules) )
{
return self::$_modules[$module];
}
else
{
trigger_error("Module '$module' not loaded!", E_USER_WARNING);
return null;
}
}
// }}}
// {{{ public static function &get_instance()
/**
* Get class instance
*
* Use this instead of instantiating the class:
* $FSO =& FSO::get_instance();
*
* @return FSO
*/
public static function &get_instance()
{
// Create the instance if it doesn't exist
if ( is_null(self::$_instance) )
{
self::$_instance = new FSO();
}
return self::$_instance;
}
// }}}
// {{{ public static function &load_module( $class [, $alias [, $basedir]] )
/**
* Load a module
*
* Load a module by class name. If the class is not already defined this
* function will try and find it at $basedir/$class.php. If the module has
* already been loaded into the superobject it will be ignored.
*
* By default the new module will be found at $FSO->classname - use the
* $alias parameter to give the module a different name when loaded - this
* allows the same module to be loaded multiple times if necessary.
*
* Returns a reference to the module just loaded (or the existing one if it
* was already loaded).
*
* @param string $class
* @param array $args
* @param string $alias
* @param string $basedir
* @return Object
*/
public static function &load_module( $module, $args = null, $alias = null, $basedir = FSO_BASEDIR )
{
// By default module variable name is same as module name
$alias = is_null($alias) ? strtolower($module) : strtolower($alias);
$args = empty($args) ? array() : $args;
// Check if the module is loaded already
if ( !array_key_exists($alias, self::$_modules) )
{
self::load_class($module, $basedir);
$ref = new ReflectionClass($module);
self::$_modules[$alias] = $ref->newInstanceArgs($args);
}
// Return the module
return self::$_modules[$alias];
}
// }}}
// {{{ public static function load_modules( $modules )
/**
* Load multiple modules
*
* Load all modules specified in an array. Array elements correspond
* exactly to args to load_module().
*
* @param array $modules
* @return void
*/
public static function load_modules( array $modules )
{
foreach ( $modules as $module )
{
call_user_func_array(array('FSO', 'load_module'), $module);
}
}
// }}}
// {{{ public static function make_module( $module [, $args [, $basedir]] )
/**
* Make a module object
*
* Load a module, instantiate it and return it without making it part of the
* superobject. The advantage to this compared to just using 'new Module()'
* is that this handles loading the file and making sure the class exists
* first.
*
* @param string $module
* @param string $basedir
* @return Object
*/
public static function make_module( $module, $args = null, $basedir = FSO_BASEDIR )
{
$args = empty($args) ? array() : $args;
self::load_class($module, $basedir);
$ref = new ReflectionClass($module);
return $ref->newInstanceArgs($args);
}
// }}}
// {{{ public static function &get_module( $alias )
/**
* Get a module
*
* Return a reference to an already-loaded module if it exists. Use this if
* you want to get to a single module without having an reference to the FSO
* object.
* FSO::get_module('mymodule');
* An alternative to
* FSO::get_instance()->mymodule;
*
* @param string $module
* @return Object
*/
public static function &get_module( $module )
{
$module = strtolower($module);
if ( array_key_exists($module, self::$_modules) )
{
return self::$_modules[$module];
}
else
{
trigger_error("Module '$module' is not loaded!", E_USER_WARNING);
return null;
}
}
// }}}
// {{{ private static function load_class( $class [, $basedir] )
/**
* Load a class
*
* Helper function to try everything possible to get a class loaded if it's
* not loaded already
*
* @param string $class
* @param string $basedir
* @return void
*/
private static function load_class( $class, $basedir = FSO_BASEDIR )
{
$class = strtolower($class);
$path = rtrim($basedir, '/')."/$class.php";
// Only do something if the class isn't defined yet
if ( !class_exists($class) )
{
// If the file exists load it
if ( file_exists($path) )
{
include_once($path);
}
// ... and check to see if that file defined the class!
if ( !class_exists($class) )
{
trigger_error("Module '$class' could not be found - "
. "please check that it is defined and/or that it "
. "exists at '$path'", E_USER_ERROR);
}
}
}
// }}}
}
// ------------------------------------------------------------------------
/**
* Framework SuperObject Module Class
*
* This class should not be instantiated directly. It should be extended by
* classes that are to be loaded as modules, to give access to all the other
* modules with the simple $this->module syntax.
*
* Remember to call parent::__construct() in the constructor of the subclass!
*/
abstract class FSO_Module
{
protected $FSO = null;
// {{{ constructor
/**
* Constructor
*
* Get the FSO object so it can be used by the class and its subclasses
*
* @return void
*/
public function __construct()
{
$this->FSO =& FSO::get_instance();
}
// }}}
// {{{ public function __get( $module )
/**
* Get module
*
* Overload __get() so that requests for $this->somemodule get passed to
* the underlying FSO object
*
* @param string $module
* @return Object
*/
public function __get( $module )
{
return $this->FSO->$module;
}
// }}}
}
// ------------------------------------------------------------------------
/**
* Franework SuperObject Database Module Class
*
* This class duplicates all of the abilities of FSO_Module above, however it
* extends the PDO class so that database functions can be accessed from
* module->function directly.
*/
abstract class FSO_DB_Module extends PDO
{
protected $FSO = null;
// {{{ constructor
/**
* Constructor
*
* Get the FSO object so it can be used by the class and its subclasses
*
* @return void
*/
public function __construct($dsn, $user, $pass)
{
$this->FSO =& FSO::get_instance();
parent::__construct($dsn, $user, $pass);
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('FSO_DB_Statement'));
}
// }}}
// {{{ public function query( $query [, $param1 [, $param2] ...] )
/**
* Use prepared statements
*
* Make all queries safe - overhead of single prepared statements is
* negligible so use them for single queries. Parameters passed do not
* need to be escaped.
*
* @return FSO_DB_Statement
*/
public function query()
{
$args = func_get_args();
$query = array_shift($args);
$stmt = $this->prepare($query);
$stmt->execute($args);
return $stmt;
}
// }}}
}
// ------------------------------------------------------------------------
/**
* Franework SuperObject Database Statement Class
*
* This extends PDOStatment so that we can add some nice stuff to it
*/
class FSO_DB_Statement extends PDOStatement
{
/**
* Returns an array with 2 elements:
*
* 'data' => array of rows as associative arrays (fieldname => value)
* 'rowcount' => number of rows in the result set
*/
public function fetchAllRows()
{
$ret = array();
$ret['data'] = $this->fetchAll(PDO::FETCH_ASSOC);
$ret['rowcount'] = count($ret['data']);
$ret['numrows'] = $ret['rowcount'];
return $ret;
}
}
?>
|