1 |
<? |
2 |
// --------------------------------------------------------------------------- |
3 |
// $Id: Object.php,v 1.7 2003/03/01 21:13:39 joko Exp $ |
4 |
// --------------------------------------------------------------------------- |
5 |
// $Log: Object.php,v $ |
6 |
// Revision 1.7 2003/03/01 21:13:39 joko |
7 |
// modified loadModule (take care!) |
8 |
// |
9 |
// Revision 1.6 2003/02/28 04:18:13 joko |
10 |
// + special-case-handling for single argument pass-in |
11 |
// + testing with constructor autocalling: not used for now since weird sideeffects occoured! |
12 |
// o however: rethink it completely! |
13 |
// |
14 |
// Revision 1.5 2003/02/27 16:36:02 joko |
15 |
// re-allowed underscores ('_') in php classnames |
16 |
// does this break something? |
17 |
// |
18 |
// Revision 1.4 2003/02/22 16:34:16 joko |
19 |
// + minor fix: can seperate namespace identifier with slashes ('/') now |
20 |
// |
21 |
// Revision 1.3 2003/02/09 17:16:06 joko |
22 |
// + handling arguments perl style ;-) |
23 |
// |
24 |
// Revision 1.2 2003/02/03 05:01:48 joko |
25 |
// + now attributes can get passed in to the constructors |
26 |
// |
27 |
// Revision 1.1 2003/02/03 03:33:48 joko |
28 |
// + initial commit |
29 |
// |
30 |
// --------------------------------------------------------------------------- |
31 |
|
32 |
|
33 |
/* |
34 |
class DesignPattern_Object { |
35 |
function DesignPattern_Object() { |
36 |
|
37 |
} |
38 |
} |
39 |
*/ |
40 |
|
41 |
function _ns2file($nsName) { |
42 |
if ($filename = str_replace('::', '/', $nsName)) { |
43 |
return $filename; |
44 |
} |
45 |
} |
46 |
|
47 |
function &mkObject() { |
48 |
$arg_list = func_get_args(); |
49 |
|
50 |
// patch arglist if all arguments are passed in as a single array |
51 |
if (count($arg_list) == 1 && is_array($arg_list[0])) { |
52 |
$arg_list = $arg_list[0]; |
53 |
} |
54 |
|
55 |
//print Dumper($arg_list); |
56 |
$namespacedClassname = array_shift($arg_list); |
57 |
if (strstr($namespacedClassname, '_')) { |
58 |
//print "mkObject: unallowed character in namespaced classname: '_' ($namespacedClassname)<br/>"; |
59 |
//return; |
60 |
} |
61 |
//print "class: $classname<br>"; |
62 |
$attributes = $arg_list; |
63 |
$classname = $namespacedClassname; |
64 |
if (loadModule($namespacedClassname)) { |
65 |
$classname = str_replace('::', '_', $namespacedClassname); |
66 |
$classname = str_replace('/', '_', $classname); |
67 |
} |
68 |
$obj = new DesignPattern_Bridge($classname, $attributes); |
69 |
|
70 |
// call constructor if possible |
71 |
// FIXME: take care for side-effects!!! |
72 |
/* |
73 |
if (method_exists($obj, 'constructor')) { |
74 |
$obj->constructor(); |
75 |
} |
76 |
*/ |
77 |
|
78 |
return $obj; |
79 |
} |
80 |
|
81 |
function loadModule($namespacedClassname, $extension = 'php') { |
82 |
if ($filename = _ns2file($namespacedClassname)) { |
83 |
if ($extension) { |
84 |
$filename .= ".$extension"; |
85 |
} |
86 |
|
87 |
// V1: just require |
88 |
//require_once($filename); |
89 |
|
90 |
// V2: check file for existance (didn't work!!! problem with path-style conversion regarding platform running on?) |
91 |
/* |
92 |
if (file_exists($filename)) { |
93 |
require_once($filename); |
94 |
} else { |
95 |
//user_error("Could not load module '$namespacedClassname', file '$filename' does not exist."); |
96 |
} |
97 |
*/ |
98 |
|
99 |
// V3: wrapped through eval (weird) |
100 |
/* |
101 |
$evalstring = "return include_once('$filename');"; |
102 |
$result = eval($evalstring); |
103 |
if (!$result) { |
104 |
user_error("DesignPattern::Object: Could not load module '$namespacedClassname', file '$filename' does not exist."); |
105 |
return; |
106 |
} |
107 |
*/ |
108 |
|
109 |
// V4: just include |
110 |
if (!include_once($filename)) { |
111 |
// TODO: do stack-backtracing here to determine function called two or three levels before!!! |
112 |
user_error("DesignPattern::Object: Could not load module '$namespacedClassname', file '$filename' does not exist."); |
113 |
return; |
114 |
} |
115 |
|
116 |
return 1; |
117 |
} |
118 |
} |
119 |
|
120 |
?> |