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