1 |
<?php |
2 |
/** |
3 |
* This file contains the DesignPattern::Proxy class |
4 |
* |
5 |
* @author Andreas Motl <andreas.motl@ilo.de> |
6 |
* @package org.netfrag.glib |
7 |
* @name DesignPattern::Proxy |
8 |
* @filesource |
9 |
* |
10 |
* |
11 |
* |
12 |
* |
13 |
* <b>Cvs-Log:</b> |
14 |
* |
15 |
* <pre> |
16 |
* $Id: Proxy.php,v 1.4 2003/03/05 16:32:18 joko Exp $ |
17 |
* |
18 |
* $Log: Proxy.php,v $ |
19 |
* Revision 1.4 2003/03/05 16:32:18 joko |
20 |
* updated docu (phpDocumentor testing....) |
21 |
* |
22 |
* Revision 1.3 2003/03/05 16:10:17 joko |
23 |
* updated docu (phpDocumentor testing....) |
24 |
* |
25 |
* Revision 1.2 2003/03/05 12:12:42 joko |
26 |
* re-renamed methods and privates: _handler becomes _proxy again |
27 |
* |
28 |
* Revision 1.1 2003/03/03 22:05:25 joko |
29 |
* abstract implementation... ;-) |
30 |
* </pre> |
31 |
* |
32 |
* |
33 |
*/ |
34 |
|
35 |
|
36 |
/** |
37 |
* Load required modules: |
38 |
* |
39 |
*/ |
40 |
loadModule('Class::Logger'); |
41 |
|
42 |
|
43 |
/** |
44 |
* An attempt to implement some software design patterns... |
45 |
* --- ProxyPattern |
46 |
* |
47 |
* @link http://c2.com/cgi-bin/wiki?ProxyPattern |
48 |
* |
49 |
* @author Andreas Motl <andreas.motl@ilo.de> |
50 |
* @link http://www.netfrag.org/~joko/ |
51 |
* |
52 |
* @copyright (c) 2003 - All Rights reserved. |
53 |
* @license GNU LGPL (GNU Lesser General Public License) |
54 |
* @link http://www.gnu.org/licenses/lgpl.txt |
55 |
* |
56 |
* @package org.netfrag.glib |
57 |
* @subpackage DesignPattern |
58 |
* @name DesignPattern::Proxy |
59 |
* |
60 |
* @todo |
61 |
* <pre> |
62 |
* x DesignPattern::DecoratorProxy (->decorate) |
63 |
* x DesignPattern::TransparentProxy (->make_transparent) |
64 |
* o DesignPattern::MultiProxy (->both???) (diamond inheritance? possible in php?) |
65 |
* o DesignPattern::GenericAdapterProxy??? |
66 |
* o no 'locator/query' or 'type/component' |
67 |
* o even more generic: just 'adapter_metadata' (-> global adapter registry?) |
68 |
* </pre> |
69 |
* |
70 |
*/ |
71 |
class DesignPattern_Proxy extends Class_Logger { |
72 |
|
73 |
/** |
74 |
* This holds some information about the tracing level. |
75 |
* |
76 |
*/ |
77 |
var $_debug = array( |
78 |
notice => 1, |
79 |
trace => 1, |
80 |
payload => 1, |
81 |
); |
82 |
|
83 |
var $_component_name; |
84 |
var $_component_options; |
85 |
|
86 |
/** |
87 |
* This var holds the Handler object |
88 |
* that is used to do the work. |
89 |
* It acts as a dispatcher combining result caching. |
90 |
* It is assumed that this provides 4 methods: |
91 |
* queryData() - execute a query against a data storage |
92 |
* querySchema() - execute a query against underlying storage metadata |
93 |
* sendCommand() - send a command against an arbitrary execution engine |
94 |
* ... or others! (these are just proposals for convenience) |
95 |
* |
96 |
*/ |
97 |
var $_proxy = NULL; |
98 |
|
99 |
|
100 |
/** |
101 |
* This var holds options fed to the Proxy object |
102 |
* These are built from locator metadata (_locator) and query arguments (_query) |
103 |
* It's a simple structured hash: |
104 |
* $proxy_options = array( |
105 |
* method => '<remote-method-name>', |
106 |
* args => array('list', 'of', 'arguments') |
107 |
* ); |
108 |
* |
109 |
*/ |
110 |
var $_proxy_options = NULL; |
111 |
|
112 |
|
113 |
function about() { |
114 |
$this->_about = |
115 |
<<<ABOUT |
116 |
* --- DesignPattern::Proxy |
117 |
* --- http://cvs.netfrag.org/nfo/php/libs/org.netfrag.glib/DesignPattern/Proxy.php |
118 |
* |
119 |
* http://c2.com/cgi-bin/wiki?ProxyPattern says... |
120 |
* Intent: Provide a surrogate or placeholder for another object to control access to it. |
121 |
* http://www.fluffycat.com/java/patterns.html says... |
122 |
* One class controls the creation of and access to objects in another class. |
123 |
|
124 |
ABOUT; |
125 |
} |
126 |
|
127 |
|
128 |
function constructor($handler = array()) { |
129 |
if (sizeof($handler)) { |
130 |
$this->set_proxy($handler); |
131 |
} |
132 |
$this->create_proxy(); |
133 |
$this->call_proxy(); |
134 |
} |
135 |
|
136 |
/* |
137 |
function _abstract_method($method) { |
138 |
$package = get_class($this); |
139 |
$package_p = get_parent_class($this); |
140 |
user_error("DesignPattern::Proxy.$package_p.$package: Please implement method '$method'."); |
141 |
} |
142 |
*/ |
143 |
|
144 |
|
145 |
/** |
146 |
* Directly inject a Handler instance to use. |
147 |
* |
148 |
* @param Handler object - &$handler |
149 |
* |
150 |
*/ |
151 |
function set_proxy( &$handler ) { |
152 |
$this->_proxy = &$handler; |
153 |
} |
154 |
|
155 |
function &get_proxy() { |
156 |
return $this->_proxy; |
157 |
} |
158 |
|
159 |
function call_proxy() { |
160 |
$this->_abstract_method('call_proxy', 'DesignPattern::Proxy'); |
161 |
} |
162 |
|
163 |
function set_component_name($name) { |
164 |
$this->_component_name = $name; |
165 |
} |
166 |
function get_component_name() { |
167 |
return $this->_component_name; |
168 |
} |
169 |
function set_component_options_v1() { |
170 |
$options = func_get_args(); |
171 |
if (is_array($options)) { |
172 |
if (is_hash($options)) { |
173 |
$this->_component_options = $options; |
174 |
} else { |
175 |
$this->_component_options = array(); |
176 |
foreach ($options as $option) { |
177 |
array_push($this->_component_options, $option); |
178 |
} |
179 |
} |
180 |
} else { |
181 |
$this->_component_options = $options; |
182 |
} |
183 |
} |
184 |
function set_component_options() { |
185 |
$options = func_get_args(); |
186 |
if (sizeof($options) >= 1) { |
187 |
$this->_component_options = array(); |
188 |
foreach ($options as $option) { |
189 |
array_push($this->_component_options, $option); |
190 |
} |
191 |
} else { |
192 |
$this->_component_options = $options; |
193 |
} |
194 |
} |
195 |
function get_component_options() { |
196 |
return $this->_component_options; |
197 |
} |
198 |
|
199 |
function create_proxy() { |
200 |
//print "DesignPattern::Proxy->create_proxy<br/>"; |
201 |
//print "comp: " . $this->get_component_name() . "<br/>"; |
202 |
//print "comp: " . $this->get_component_options() . "<br/>"; |
203 |
$handler = php::mkComponent( $this->get_component_name(), $this->get_component_options() ); |
204 |
//print Dumper($handler); |
205 |
//exit; |
206 |
|
207 |
$this->set_proxy( $handler ); |
208 |
} |
209 |
|
210 |
|
211 |
} |
212 |
|
213 |
?> |