1 |
joko |
1.1 |
<? |
2 |
joko |
1.2 |
/** |
3 |
|
|
* This file contains the Class::Inner class. |
4 |
|
|
* |
5 |
|
|
* @author Andreas Motl <andreas.motl@ilo.de> |
6 |
|
|
* @name Class::Inner |
7 |
|
|
* |
8 |
|
|
*/ |
9 |
|
|
|
10 |
|
|
|
11 |
joko |
1.1 |
// --------------------------------------------------------------------------- |
12 |
joko |
1.2 |
// $Id: Inner.php,v 1.1 2003/03/03 21:26:30 joko Exp $ |
13 |
joko |
1.1 |
// --------------------------------------------------------------------------- |
14 |
joko |
1.2 |
// $Log: Inner.php,v $ |
15 |
|
|
// Revision 1.1 2003/03/03 21:26:30 joko |
16 |
|
|
// refactored from DesignPattern::Bridge |
17 |
|
|
// |
18 |
joko |
1.1 |
// Revision 1.5 2003/02/27 18:09:56 joko |
19 |
|
|
// mungled mechanism to shift in and pass on arguments |
20 |
|
|
// |
21 |
|
|
// Revision 1.4 2003/02/09 17:07:53 joko |
22 |
|
|
// + minor update related to new log level constants |
23 |
|
|
// + generic argument merger to show full constructor args |
24 |
|
|
// |
25 |
|
|
// Revision 1.3 2003/02/03 14:46:57 joko |
26 |
|
|
// + wrapped calls to available initializers (constructors, declared startup methods) |
27 |
|
|
// - moved logger-code to DesignPattern::Logger |
28 |
|
|
// |
29 |
|
|
// Revision 1.2 2003/02/03 05:01:27 joko |
30 |
|
|
// + now attributes can get passed in to the constructors |
31 |
|
|
// |
32 |
|
|
// Revision 1.1 2003/02/03 03:33:48 joko |
33 |
|
|
// + initial commit |
34 |
|
|
// |
35 |
|
|
// --------------------------------------------------------------------------- |
36 |
|
|
|
37 |
|
|
|
38 |
joko |
1.2 |
/** |
39 |
|
|
* This requires DesignPattern::Facade as a base class |
40 |
|
|
* |
41 |
|
|
*/ |
42 |
joko |
1.1 |
loadModule('DesignPattern::Facade'); |
43 |
joko |
1.2 |
|
44 |
|
|
/** |
45 |
|
|
* Class::Inner |
46 |
|
|
* |
47 |
|
|
* @author Andreas Motl <andreas.motl@ilo.de> |
48 |
|
|
* @copyright (c) 2003 - All Rights reserved. |
49 |
|
|
* @license GNU LGPL (GNU Lesser General Public License) |
50 |
|
|
* |
51 |
|
|
* @link http://www.netfrag.org/~joko/ |
52 |
|
|
* @link http://www.gnu.org/licenses/lgpl.txt |
53 |
|
|
* |
54 |
|
|
* @package org.netfrag.glib |
55 |
|
|
* @subpackage Class |
56 |
|
|
* @name Class::Inner |
57 |
|
|
* |
58 |
|
|
*/ |
59 |
joko |
1.1 |
class Class_Inner extends DesignPattern_Facade { |
60 |
|
|
|
61 |
|
|
/* |
62 |
|
|
var $_parent_class; |
63 |
|
|
var $_inner_classes; |
64 |
|
|
var $_inner_objects; |
65 |
|
|
var $_call_constructor; |
66 |
|
|
*/ |
67 |
|
|
|
68 |
|
|
function perform($args) { |
69 |
|
|
$this->__create_inners($args); |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
function __call_constructor($objectname, $method) { |
73 |
|
|
if (method_exists($this->$objectname, $method)) { |
74 |
|
|
$this->log( get_class($this) . "->__call_constructor: autocalling \$this->" . $objectname . "->$method()" ); |
75 |
|
|
$this->$objectname->$method(); |
76 |
|
|
} |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
function __create_inners($args) { |
80 |
|
|
|
81 |
|
|
$this->log( get_parent_class($this) . "->__create_inners( parent='" . $args[parent_name] . "' )", PEAR_LOG_INFO ); |
82 |
|
|
//$this->log( get_parent_class($this) . "->_init_helpers: instantiating helper objects below '" . get_class($this) . "::'" ); |
83 |
|
|
|
84 |
|
|
//print Dumper($args); |
85 |
|
|
|
86 |
|
|
foreach ($args[class_names] as $classname) { |
87 |
|
|
|
88 |
|
|
// build objectname from classname |
89 |
|
|
// - make lowercase |
90 |
|
|
// - strip leading "Xyz_" ('Site_' here) |
91 |
|
|
$objectname = $classname; |
92 |
|
|
$objectname = str_replace('Site_', '', $objectname); // FIXME!!! |
93 |
|
|
$objectname = strtolower($objectname); |
94 |
|
|
|
95 |
|
|
// create new instance of helper object by classname |
96 |
|
|
// V1: |
97 |
|
|
//$this->$objectname = &$this->_mkInstance($classname); |
98 |
|
|
// V2: |
99 |
|
|
$this->$objectname = &php::mkInstance($classname); |
100 |
|
|
|
101 |
|
|
// create additional references to helper object with other names if requested |
102 |
|
|
// the intention is (e.g.) to migrate objects over to new reference-names when development progresses and ... |
103 |
|
|
// ... refactoring the object hierarchy is needed, but ... |
104 |
|
|
// ... you wanna provide the old reference names as "fallbacks" for old code using the libs |
105 |
|
|
if (is_array($args[ref_names])) { |
106 |
|
|
foreach ($args[ref_names] as $ref_name) { |
107 |
|
|
//print "mkRef: $ref_name<br>"; |
108 |
|
|
$this->$ref_name = &$this->$objectname; |
109 |
|
|
} |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
// helper gets reference to ourselves as a parent |
113 |
|
|
$this->$objectname->$args[parent_name] = &$this; |
114 |
|
|
|
115 |
|
|
$this->__call_constructor($objectname, 'constructor'); |
116 |
|
|
if ( $method = $args[run] ) { |
117 |
|
|
$this->__call_constructor($objectname, $method); |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
|
125 |
|
|
|
126 |
|
|
// --- old code |
127 |
|
|
|
128 |
|
|
function DesignPattern_Bridge_old() { |
129 |
|
|
|
130 |
|
|
$arg_list = func_get_args(); |
131 |
|
|
$classname = array_shift($arg_list); |
132 |
|
|
|
133 |
|
|
// expand single argument |
134 |
|
|
if (count($arg_list) == 1) { |
135 |
|
|
$arg_list = $arg_list[0]; |
136 |
|
|
} |
137 |
|
|
$attributes = &$arg_list; |
138 |
|
|
|
139 |
|
|
//print Dumper($attributes); |
140 |
|
|
|
141 |
|
|
//print "init_bridge!<br>"; |
142 |
|
|
//return $this->_mkInstance($classname, $attributes); |
143 |
|
|
//$this->_init_logger("../core/var/log/logfile.txt", 1); |
144 |
|
|
//parent::constructor(); |
145 |
|
|
|
146 |
|
|
// V1: |
147 |
|
|
$this = $this->_mkInstance($classname, $attributes); |
148 |
|
|
// V2: (don't do that - will crash your apache!!! - hehe - it's an infinite loop) |
149 |
|
|
//$this = php::mkInstance($classname, $attributes); |
150 |
|
|
|
151 |
|
|
//parent::constructor(); |
152 |
|
|
// $this->_init_logger("../core/var/log/logfile.txt", 1); |
153 |
|
|
//print Dumper($this); |
154 |
|
|
|
155 |
|
|
//return $this; |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
function &_mkInstance_old($classname, $attributes = null) { |
159 |
|
|
parent::constructor(); |
160 |
|
|
$this->log( get_class($this) . "->_mkInstance( classname $classname )" ); |
161 |
|
|
if (isset($attributes)) { |
162 |
|
|
//print Dumper($attributes); |
163 |
|
|
|
164 |
|
|
/* |
165 |
|
|
// pass single argument 1:1 |
166 |
|
|
if (count($attributes) == 1) { |
167 |
|
|
$attributes_merged = $attributes[0]; |
168 |
|
|
|
169 |
|
|
|
170 |
|
|
|
171 |
|
|
// pass hash 1:1 |
172 |
|
|
} elseif (is_hash($attributes)) { |
173 |
|
|
$attributes_merged = $attributes; |
174 |
|
|
|
175 |
|
|
} else { |
176 |
|
|
$attributes_merged = $attributes; |
177 |
|
|
} |
178 |
|
|
*/ |
179 |
|
|
|
180 |
|
|
$args_pass = array(); |
181 |
|
|
for ($i=0; $i<=count($attributes); $i++) { |
182 |
|
|
array_push($args_pass, '$attributes[' . $i . ']'); |
183 |
|
|
} |
184 |
|
|
|
185 |
|
|
$arg_string = join(', ', $args_pass); |
186 |
|
|
|
187 |
|
|
/* |
188 |
|
|
// merge entries of numerical indexed arrays together into one hash |
189 |
|
|
} else { |
190 |
|
|
$attributes_merged = array(); |
191 |
|
|
foreach ($attributes as $entry) { |
192 |
|
|
$attributes_merged = array_merge($attributes_merged, $entry); |
193 |
|
|
} |
194 |
|
|
} |
195 |
|
|
*/ |
196 |
|
|
|
197 |
|
|
//print Dumper($attributes_merged); |
198 |
|
|
//print Dumper($attributes); |
199 |
|
|
//$instance = new $classname($attributes_merged); |
200 |
|
|
//$instance = new $classname($attributes[0]); |
201 |
|
|
$evalstr = 'return new $classname(' . $arg_string . ');'; |
202 |
|
|
$instance = eval($evalstr); |
203 |
|
|
//print $evalstr . "<br>"; |
204 |
|
|
} else { |
205 |
|
|
$instance = new $classname; |
206 |
|
|
} |
207 |
|
|
//$this->log("ok"); |
208 |
|
|
return $instance; |
209 |
|
|
} |
210 |
|
|
|
211 |
|
|
} |
212 |
|
|
|
213 |
|
|
|
214 |
|
|
|
215 |
|
|
|
216 |
|
|
?> |