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