1 |
<? |
2 |
// --------------------------------------------------------------------------- |
3 |
// $Id: Bridge.php,v 1.5 2003/02/27 18:09:56 joko Exp $ |
4 |
// --------------------------------------------------------------------------- |
5 |
// $Log: Bridge.php,v $ |
6 |
// Revision 1.5 2003/02/27 18:09:56 joko |
7 |
// mungled mechanism to shift in and pass on arguments |
8 |
// |
9 |
// Revision 1.4 2003/02/09 17:07:53 joko |
10 |
// + minor update related to new log level constants |
11 |
// + generic argument merger to show full constructor args |
12 |
// |
13 |
// Revision 1.3 2003/02/03 14:46:57 joko |
14 |
// + wrapped calls to available initializers (constructors, declared startup methods) |
15 |
// - moved logger-code to DesignPattern::Logger |
16 |
// |
17 |
// Revision 1.2 2003/02/03 05:01:27 joko |
18 |
// + now attributes can get passed in to the constructors |
19 |
// |
20 |
// Revision 1.1 2003/02/03 03:33:48 joko |
21 |
// + initial commit |
22 |
// |
23 |
// --------------------------------------------------------------------------- |
24 |
|
25 |
|
26 |
loadModule('DesignPattern::Facade'); |
27 |
class Class_Inner extends DesignPattern_Facade { |
28 |
|
29 |
/* |
30 |
var $_parent_class; |
31 |
var $_inner_classes; |
32 |
var $_inner_objects; |
33 |
var $_call_constructor; |
34 |
*/ |
35 |
|
36 |
function perform($args) { |
37 |
$this->__create_inners($args); |
38 |
} |
39 |
|
40 |
function __call_constructor($objectname, $method) { |
41 |
if (method_exists($this->$objectname, $method)) { |
42 |
$this->log( get_class($this) . "->__call_constructor: autocalling \$this->" . $objectname . "->$method()" ); |
43 |
$this->$objectname->$method(); |
44 |
} |
45 |
} |
46 |
|
47 |
function __create_inners($args) { |
48 |
|
49 |
$this->log( get_parent_class($this) . "->__create_inners( parent='" . $args[parent_name] . "' )", PEAR_LOG_INFO ); |
50 |
//$this->log( get_parent_class($this) . "->_init_helpers: instantiating helper objects below '" . get_class($this) . "::'" ); |
51 |
|
52 |
//print Dumper($args); |
53 |
|
54 |
foreach ($args[class_names] as $classname) { |
55 |
|
56 |
// build objectname from classname |
57 |
// - make lowercase |
58 |
// - strip leading "Xyz_" ('Site_' here) |
59 |
$objectname = $classname; |
60 |
$objectname = str_replace('Site_', '', $objectname); // FIXME!!! |
61 |
$objectname = strtolower($objectname); |
62 |
|
63 |
// create new instance of helper object by classname |
64 |
// V1: |
65 |
//$this->$objectname = &$this->_mkInstance($classname); |
66 |
// V2: |
67 |
$this->$objectname = &php::mkInstance($classname); |
68 |
|
69 |
// create additional references to helper object with other names if requested |
70 |
// the intention is (e.g.) to migrate objects over to new reference-names when development progresses and ... |
71 |
// ... refactoring the object hierarchy is needed, but ... |
72 |
// ... you wanna provide the old reference names as "fallbacks" for old code using the libs |
73 |
if (is_array($args[ref_names])) { |
74 |
foreach ($args[ref_names] as $ref_name) { |
75 |
//print "mkRef: $ref_name<br>"; |
76 |
$this->$ref_name = &$this->$objectname; |
77 |
} |
78 |
} |
79 |
|
80 |
// helper gets reference to ourselves as a parent |
81 |
$this->$objectname->$args[parent_name] = &$this; |
82 |
|
83 |
$this->__call_constructor($objectname, 'constructor'); |
84 |
if ( $method = $args[run] ) { |
85 |
$this->__call_constructor($objectname, $method); |
86 |
} |
87 |
|
88 |
} |
89 |
|
90 |
} |
91 |
|
92 |
|
93 |
|
94 |
// --- old code |
95 |
|
96 |
function DesignPattern_Bridge_old() { |
97 |
|
98 |
$arg_list = func_get_args(); |
99 |
$classname = array_shift($arg_list); |
100 |
|
101 |
// expand single argument |
102 |
if (count($arg_list) == 1) { |
103 |
$arg_list = $arg_list[0]; |
104 |
} |
105 |
$attributes = &$arg_list; |
106 |
|
107 |
//print Dumper($attributes); |
108 |
|
109 |
//print "init_bridge!<br>"; |
110 |
//return $this->_mkInstance($classname, $attributes); |
111 |
//$this->_init_logger("../core/var/log/logfile.txt", 1); |
112 |
//parent::constructor(); |
113 |
|
114 |
// V1: |
115 |
$this = $this->_mkInstance($classname, $attributes); |
116 |
// V2: (don't do that - will crash your apache!!! - hehe - it's an infinite loop) |
117 |
//$this = php::mkInstance($classname, $attributes); |
118 |
|
119 |
//parent::constructor(); |
120 |
// $this->_init_logger("../core/var/log/logfile.txt", 1); |
121 |
//print Dumper($this); |
122 |
|
123 |
//return $this; |
124 |
} |
125 |
|
126 |
function &_mkInstance_old($classname, $attributes = null) { |
127 |
parent::constructor(); |
128 |
$this->log( get_class($this) . "->_mkInstance( classname $classname )" ); |
129 |
if (isset($attributes)) { |
130 |
//print Dumper($attributes); |
131 |
|
132 |
/* |
133 |
// pass single argument 1:1 |
134 |
if (count($attributes) == 1) { |
135 |
$attributes_merged = $attributes[0]; |
136 |
|
137 |
|
138 |
|
139 |
// pass hash 1:1 |
140 |
} elseif (is_hash($attributes)) { |
141 |
$attributes_merged = $attributes; |
142 |
|
143 |
} else { |
144 |
$attributes_merged = $attributes; |
145 |
} |
146 |
*/ |
147 |
|
148 |
$args_pass = array(); |
149 |
for ($i=0; $i<=count($attributes); $i++) { |
150 |
array_push($args_pass, '$attributes[' . $i . ']'); |
151 |
} |
152 |
|
153 |
$arg_string = join(', ', $args_pass); |
154 |
|
155 |
/* |
156 |
// merge entries of numerical indexed arrays together into one hash |
157 |
} else { |
158 |
$attributes_merged = array(); |
159 |
foreach ($attributes as $entry) { |
160 |
$attributes_merged = array_merge($attributes_merged, $entry); |
161 |
} |
162 |
} |
163 |
*/ |
164 |
|
165 |
//print Dumper($attributes_merged); |
166 |
//print Dumper($attributes); |
167 |
//$instance = new $classname($attributes_merged); |
168 |
//$instance = new $classname($attributes[0]); |
169 |
$evalstr = 'return new $classname(' . $arg_string . ');'; |
170 |
$instance = eval($evalstr); |
171 |
//print $evalstr . "<br>"; |
172 |
} else { |
173 |
$instance = new $classname; |
174 |
} |
175 |
//$this->log("ok"); |
176 |
return $instance; |
177 |
} |
178 |
|
179 |
} |
180 |
|
181 |
|
182 |
|
183 |
|
184 |
?> |