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