| 3 | //  $Id$ | //  $Id$ | 
| 4 | // --------------------------------------------------------------------------- | // --------------------------------------------------------------------------- | 
| 5 | //  $Log$ | //  $Log$ | 
| 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 | //  Revision 1.1  2003/02/03 03:33:48  joko | 
| 21 | //  + initial commit | //  + initial commit | 
| 22 | // | // | 
| 25 |  |  | 
| 26 | class DesignPattern_Bridge extends DesignPattern_Logger { | class DesignPattern_Bridge extends DesignPattern_Logger { | 
| 27 |  |  | 
| 28 | function DesignPattern_Bridge($classname) { | function DesignPattern_Bridge() { | 
| 29 | return $this->_mkInstance($classname); |  | 
| 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) { | function &_mkInstance($classname, $attributes = null) { | 
| 54 | $this->log( get_class($this) . "->_mkInstance( classname $classname )", LOG_DEBUG ); | parent::constructor(); | 
| 55 | return new $classname; | $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) { | function _mkEmbeddedObjects($args) { | 
| 107 |  |  | 
| 108 | $this->log( get_class($this) . "->_mkEmbeddedObjects()", LOG_DEBUG ); | $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) { | foreach ($args[class_names] as $classname) { | 
| 114 |  |  | 
| 136 | // helper gets reference to ourselves as a parent | // helper gets reference to ourselves as a parent | 
| 137 | $this->$objectname->$args[parent_name] = &$this; | $this->$objectname->$args[parent_name] = &$this; | 
| 138 |  |  | 
| 139 |  | $this->_call_initializer($objectname, 'constructor'); | 
| 140 | if ( $method = $args[run] ) { | if ( $method = $args[run] ) { | 
| 141 | if (method_exists($this->$objectname, $method)) { | $this->_call_initializer($objectname, $method); | 
|  | $this->log( get_class($this) . "->_mkEmbeddedObjects: calling method \$this->" . $objectname . "->$method()", LOG_DEBUG ); |  | 
|  | $this->$objectname->$method(); |  | 
|  | } |  | 
| 142 | } | } | 
| 143 |  |  | 
| 144 | } | } | 
| 145 |  |  | 
| 146 | } | } | 
| 147 |  |  | 
| 148 | function _init_logger() { | function _call_initializer($objectname, $method) { | 
| 149 | // Log | if (method_exists($this->$objectname, $method)) { | 
| 150 | // valid logging-levels are: | $this->log( get_class($this) . "->_call_initializer: autocalling \$this->" . $objectname . "->$method()" ); | 
| 151 | // LOG_EMERG, LOG_ALERT, LOG_CRIT, | $this->$objectname->$method(); | 
| 152 | // LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, and LOG_DEBUG. | } | 
|  | // The default is LOG_INFO. |  | 
|  | $logfile = 'log.txt'; |  | 
|  | $logfile = $this->config[path][base] . "core/var/log/logfile.txt"; |  | 
|  |  |  | 
|  | // TODO: maybe include userid here? |  | 
|  | //$log_ident = substr(session_id(), 10, 6); |  | 
|  | $log_ident = session_id(); |  | 
|  | if ($this->config[mode][LOG]) { |  | 
|  | $this->logger = &Log::singleton('file', $logfile, $log_ident); |  | 
|  | //$site->logger = new Log_file($logfile, $log_ident); |  | 
|  | } else { |  | 
|  | $this->logger = &Log::singleton('dummy', $logfile, $log_ident); |  | 
|  | } |  | 
|  | $this->log( get_class($this) . "->_init_logger: ready\t\t--------------------", LOG_DEBUG ); |  | 
| 153 | } | } | 
| 154 |  |  | 
| 155 |  |  | 
| 156 | } | } | 
| 157 | ?> | ?> |