| 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 |
class DesignPattern_Bridge extends DesignPattern_Logger { |
| 27 |
|
| 28 |
function DesignPattern_Bridge() { |
| 29 |
|
| 30 |
$arg_list = func_get_args(); |
| 31 |
$classname = array_shift($arg_list); |
| 32 |
|
| 33 |
// expand single argument |
| 34 |
if (count($arg_list) == 1) { |
| 35 |
$arg_list = $arg_list[0]; |
| 36 |
} |
| 37 |
$attributes = &$arg_list; |
| 38 |
|
| 39 |
//print Dumper($attributes); |
| 40 |
|
| 41 |
//print "init_bridge!<br>"; |
| 42 |
//return $this->_mkInstance($classname, $attributes); |
| 43 |
//$this->_init_logger("../core/var/log/logfile.txt", 1); |
| 44 |
//parent::constructor(); |
| 45 |
$this = $this->_mkInstance($classname, $attributes); |
| 46 |
//parent::constructor(); |
| 47 |
// $this->_init_logger("../core/var/log/logfile.txt", 1); |
| 48 |
//print Dumper($this); |
| 49 |
//parent::DesignPattern_Logger(); |
| 50 |
//return $this; |
| 51 |
} |
| 52 |
|
| 53 |
function &_mkInstance($classname, $attributes = null) { |
| 54 |
parent::constructor(); |
| 55 |
$this->log( get_class($this) . "->_mkInstance( classname $classname )" ); |
| 56 |
if (isset($attributes)) { |
| 57 |
//print Dumper($attributes); |
| 58 |
|
| 59 |
/* |
| 60 |
// pass single argument 1:1 |
| 61 |
if (count($attributes) == 1) { |
| 62 |
$attributes_merged = $attributes[0]; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
// pass hash 1:1 |
| 67 |
} elseif (is_hash($attributes)) { |
| 68 |
$attributes_merged = $attributes; |
| 69 |
|
| 70 |
} else { |
| 71 |
$attributes_merged = $attributes; |
| 72 |
} |
| 73 |
*/ |
| 74 |
|
| 75 |
$args_pass = array(); |
| 76 |
for ($i=0; $i<=count($attributes); $i++) { |
| 77 |
array_push($args_pass, '$attributes[' . $i . ']'); |
| 78 |
} |
| 79 |
|
| 80 |
$arg_string = join(', ', $args_pass); |
| 81 |
|
| 82 |
/* |
| 83 |
// merge entries of numerical indexed arrays together into one hash |
| 84 |
} else { |
| 85 |
$attributes_merged = array(); |
| 86 |
foreach ($attributes as $entry) { |
| 87 |
$attributes_merged = array_merge($attributes_merged, $entry); |
| 88 |
} |
| 89 |
} |
| 90 |
*/ |
| 91 |
|
| 92 |
//print Dumper($attributes_merged); |
| 93 |
//print Dumper($attributes); |
| 94 |
//$instance = new $classname($attributes_merged); |
| 95 |
//$instance = new $classname($attributes[0]); |
| 96 |
$evalstr = 'return new $classname(' . $arg_string . ');'; |
| 97 |
$instance = eval($evalstr); |
| 98 |
//print $evalstr . "<br>"; |
| 99 |
} else { |
| 100 |
$instance = new $classname; |
| 101 |
} |
| 102 |
//$this->log("ok"); |
| 103 |
return $instance; |
| 104 |
} |
| 105 |
|
| 106 |
function _mkEmbeddedObjects($args) { |
| 107 |
|
| 108 |
$this->log( get_parent_class($this) . "->_mkEmbeddedObjects( parent='" . $args[parent_name] . "' )", PEAR_LOG_INFO ); |
| 109 |
//$this->log( get_parent_class($this) . "->_init_helpers: instantiating helper objects below '" . get_class($this) . "::'" ); |
| 110 |
|
| 111 |
//print Dumper($args); |
| 112 |
|
| 113 |
foreach ($args[class_names] as $classname) { |
| 114 |
|
| 115 |
// build objectname from classname |
| 116 |
// - make lowercase |
| 117 |
// - strip leading "Xyz_" ('Site_' here) |
| 118 |
$objectname = $classname; |
| 119 |
$objectname = str_replace('Site_', '', $objectname); |
| 120 |
$objectname = strtolower($objectname); |
| 121 |
|
| 122 |
// create new instance of helper object by classname |
| 123 |
$this->$objectname = &$this->_mkInstance($classname); |
| 124 |
|
| 125 |
// create additional references to helper object with other names if requested |
| 126 |
// the intention is (e.g.) to migrate objects over to new reference-names when development progresses and ... |
| 127 |
// ... refactoring the object hierarchy is needed, but ... |
| 128 |
// ... you wanna provide the old reference names as "fallbacks" for old code using the libs |
| 129 |
if (is_array($args[ref_names])) { |
| 130 |
foreach ($args[ref_names] as $ref_name) { |
| 131 |
//print "mkRef: $ref_name<br>"; |
| 132 |
$this->$ref_name = &$this->$objectname; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
// helper gets reference to ourselves as a parent |
| 137 |
$this->$objectname->$args[parent_name] = &$this; |
| 138 |
|
| 139 |
$this->_call_initializer($objectname, 'constructor'); |
| 140 |
if ( $method = $args[run] ) { |
| 141 |
$this->_call_initializer($objectname, $method); |
| 142 |
} |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
function _call_initializer($objectname, $method) { |
| 149 |
if (method_exists($this->$objectname, $method)) { |
| 150 |
$this->log( get_class($this) . "->_call_initializer: autocalling \$this->" . $objectname . "->$method()" ); |
| 151 |
$this->$objectname->$method(); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
} |
| 157 |
?> |