/[cvs]/nfo/php/libs/org.netfrag.glib/DesignPattern/Bridge.php
ViewVC logotype

Annotation of /nfo/php/libs/org.netfrag.glib/DesignPattern/Bridge.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations)
Thu Feb 27 18:09:56 2003 UTC (21 years, 6 months ago) by joko
Branch: MAIN
Changes since 1.4: +18 -2 lines
mungled mechanism to shift in and pass on arguments

1 joko 1.1 <?
2     // ---------------------------------------------------------------------------
3 joko 1.5 // $Id: Bridge.php,v 1.4 2003/02/09 17:07:53 joko Exp $
4 joko 1.1 // ---------------------------------------------------------------------------
5 joko 1.2 // $Log: Bridge.php,v $
6 joko 1.5 // 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 joko 1.4 // 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 joko 1.3 // Revision 1.2 2003/02/03 05:01:27 joko
15     // + now attributes can get passed in to the constructors
16     //
17 joko 1.2 // Revision 1.1 2003/02/03 03:33:48 joko
18     // + initial commit
19     //
20 joko 1.1 // ---------------------------------------------------------------------------
21    
22    
23     class DesignPattern_Bridge extends DesignPattern_Logger {
24    
25 joko 1.5 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 joko 1.3 //print "init_bridge!<br>";
39 joko 1.2 //return $this->_mkInstance($classname, $attributes);
40 joko 1.3 //$this->_init_logger("../core/var/log/logfile.txt", 1);
41     //parent::constructor();
42 joko 1.2 $this = $this->_mkInstance($classname, $attributes);
43 joko 1.3 //parent::constructor();
44     // $this->_init_logger("../core/var/log/logfile.txt", 1);
45     //print Dumper($this);
46     //parent::DesignPattern_Logger();
47 joko 1.2 //return $this;
48 joko 1.1 }
49    
50 joko 1.2 function &_mkInstance($classname, $attributes = null) {
51 joko 1.3 parent::constructor();
52 joko 1.4 $this->log( get_class($this) . "->_mkInstance( classname $classname )" );
53 joko 1.2 if (isset($attributes)) {
54 joko 1.4 //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 joko 1.2 } else {
97     $instance = new $classname;
98     }
99 joko 1.4 //$this->log("ok");
100 joko 1.2 return $instance;
101 joko 1.1 }
102    
103     function _mkEmbeddedObjects($args) {
104    
105 joko 1.4 $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 joko 1.1
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 joko 1.3 $this->_call_initializer($objectname, 'constructor');
137 joko 1.1 if ( $method = $args[run] ) {
138 joko 1.3 $this->_call_initializer($objectname, $method);
139 joko 1.1 }
140    
141     }
142    
143     }
144    
145 joko 1.3 function _call_initializer($objectname, $method) {
146     if (method_exists($this->$objectname, $method)) {
147 joko 1.4 $this->log( get_class($this) . "->_call_initializer: autocalling \$this->" . $objectname . "->$method()" );
148 joko 1.3 $this->$objectname->$method();
149     }
150 joko 1.1 }
151 joko 1.3
152 joko 1.1
153     }
154     ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed