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