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