| 1 | <?php | <?php | 
|  |  |  | 
| 2 | /** | /** | 
| 3 | * This file contains the DesignPattern::Proxy class. | * This file contains the DesignPattern::Proxy class | 
| 4 | * | * | 
| 5 | * @author Andreas Motl <andreas.motl@ilo.de> | * @author Andreas Motl <andreas.motl@ilo.de> | 
| 6 | * @module DesignPattern::Proxy | * @package org.netfrag.glib | 
| 7 |  | * @name DesignPattern::Proxy | 
| 8 |  | * @filesource | 
| 9 | * | * | 
| 10 | */ | * | 
| 11 |  | * | 
| 12 | /** | * | 
| 13 |  | * <b>Cvs-Log:</b> | 
| 14 |  | * | 
| 15 |  | * <pre> | 
| 16 | * $Id$ | * $Id$ | 
| 17 | * | * | 
| 18 | * $Log$ | * $Log$ | 
| 19 |  | * Revision 1.5  2003/03/05 17:28:43  joko | 
| 20 |  | * updated docu (phpDocumentor testing....) | 
| 21 |  | * | 
| 22 |  | * Revision 1.4  2003/03/05 16:32:18  joko | 
| 23 |  | * updated docu (phpDocumentor testing....) | 
| 24 |  | * | 
| 25 |  | * Revision 1.3  2003/03/05 16:10:17  joko | 
| 26 |  | * updated docu (phpDocumentor testing....) | 
| 27 |  | * | 
| 28 |  | * Revision 1.2  2003/03/05 12:12:42  joko | 
| 29 |  | * re-renamed methods and privates: _handler becomes _proxy again | 
| 30 |  | * | 
| 31 | * Revision 1.1  2003/03/03 22:05:25  joko | * Revision 1.1  2003/03/03 22:05:25  joko | 
| 32 | * abstract implementation...  ;-) | * abstract implementation...  ;-) | 
| 33 |  | * </pre> | 
| 34 | * | * | 
| 35 | * | * | 
| 36 | */ | */ | 
| 37 |  |  | 
| 38 |  |  | 
| 39 | /** | /** | 
| 40 | * --- An attempt to implement some software design patterns... | * Load required modules: | 
| 41 |  | * | 
| 42 |  | */ | 
| 43 |  | loadModule('Class::Logger'); | 
| 44 |  |  | 
| 45 |  |  | 
| 46 |  | /** | 
| 47 |  | * An attempt to implement some software design patterns... | 
| 48 | * --- ProxyPattern | * --- ProxyPattern | 
| 49 | * | * | 
| 50 | * --- http://c2.com/cgi-bin/wiki?ProxyPattern | * @link http://c2.com/cgi-bin/wiki?ProxyPattern | 
| 51 | * | * | 
| 52 | * @author Andreas Motl <andreas.motl@ilo.de> | * @author Andreas Motl <andreas.motl@ilo.de> | 
| 53 |  | * @link http://www.netfrag.org/~joko/ | 
| 54 |  | * | 
| 55 | * @copyright (c) 2003 - All Rights reserved. | * @copyright (c) 2003 - All Rights reserved. | 
| 56 | * @license GNU LGPL (GNU Lesser General Public License) | * @license GNU LGPL (GNU Lesser General Public License) | 
| 57 | * | * @link http://www.gnu.org/licenses/lgpl.txt | 
|  | * @author-url http://www.netfrag.org/~joko/ |  | 
|  | * @license-url http://www.gnu.org/licenses/lgpl.txt |  | 
| 58 | * | * | 
| 59 | * @package org.netfrag.glib | * @package org.netfrag.glib | 
| 60 | * @module DesignPattern::Proxy | * @subpackage DesignPattern | 
| 61 | * | * @name DesignPattern::Proxy | 
|  | */ |  | 
|  |  |  | 
|  | /** |  | 
|  | * Todo: |  | 
| 62 | * | * | 
| 63 |  | * @todo | 
| 64 |  | * <pre> | 
| 65 | *    x DesignPattern::DecoratorProxy  (->decorate) | *    x DesignPattern::DecoratorProxy  (->decorate) | 
| 66 | *    x DesignPattern::TransparentProxy  (->make_transparent) | *    x DesignPattern::TransparentProxy  (->make_transparent) | 
| 67 | *    o DesignPattern::MultiProxy   (->both???) (diamond inheritance? possible in php?) | *    o DesignPattern::MultiProxy   (->both???) (diamond inheritance? possible in php?) | 
| 68 | *    o DesignPattern::GenericAdapterProxy??? | *    o DesignPattern::GenericAdapterProxy??? | 
| 69 | *      o no 'locator/query' or 'type/component' | *      o no 'locator/query' or 'type/component' | 
| 70 | *      o even more generic: just 'adapter_metadata'    (-> global adapter registry?) | *      o even more generic: just 'adapter_metadata'    (-> global adapter registry?) | 
| 71 |  | * </pre> | 
|  |  |  | 
|  | * |  | 
| 72 | * | * | 
| 73 | */ | */ | 
|  |  |  | 
|  | /** |  | 
|  | * Load required modules: |  | 
|  | * |  | 
|  | */ |  | 
|  | loadModule('Class::Logger'); |  | 
|  |  |  | 
| 74 | class DesignPattern_Proxy extends Class_Logger { | class DesignPattern_Proxy extends Class_Logger { | 
| 75 |  |  | 
| 76 | /** | /** | 
| 78 | * | * | 
| 79 | */ | */ | 
| 80 | var $_debug = array( | var $_debug = array( | 
| 81 | notice => 0, | notice => 1, | 
| 82 | trace => 0, | trace => 1, | 
| 83 | payload => 0, | payload => 1, | 
| 84 | ); | ); | 
| 85 |  |  | 
| 86 | var $_component_name; | var $_component_name; | 
| 97 | * ... or others! (these are just proposals for convenience) | * ... or others! (these are just proposals for convenience) | 
| 98 | * | * | 
| 99 | */ | */ | 
| 100 | var $_handler = NULL; | var $_proxy = NULL; | 
| 101 |  |  | 
| 102 |  |  | 
| 103 | /** | /** | 
| 110 | *  ); | *  ); | 
| 111 | * | * | 
| 112 | */ | */ | 
| 113 | var $_handler_options = NULL; | var $_proxy_options = NULL; | 
| 114 |  |  | 
| 115 |  |  | 
| 116 | function about() { | function about() { | 
| 130 |  |  | 
| 131 | function constructor($handler = array()) { | function constructor($handler = array()) { | 
| 132 | if (sizeof($handler)) { | if (sizeof($handler)) { | 
| 133 | $this->set_handler($handler); | $this->set_proxy($handler); | 
| 134 | } | } | 
| 135 | $this->create_handler(); | $this->create_proxy(); | 
| 136 | $this->call_handler(); | $this->call_proxy(); | 
| 137 | } | } | 
| 138 |  |  | 
| 139 | /* | /* | 
| 151 | * @param Handler object - &$handler | * @param Handler object - &$handler | 
| 152 | * | * | 
| 153 | */ | */ | 
| 154 | function set_handler( &$handler ) { | function set_proxy( &$handler ) { | 
| 155 | $this->_handler = &$handler; | $this->_proxy = &$handler; | 
| 156 | } | } | 
| 157 |  |  | 
| 158 | function &get_handler() { | function &get_proxy() { | 
| 159 | return $this->_handler; | return $this->_proxy; | 
| 160 | } | } | 
| 161 |  |  | 
| 162 | function call_handler() { | function call_proxy() { | 
| 163 | $this->_abstract_method('call_handler', 'DesignPattern::Proxy'); | $this->_abstract_method('call_proxy', 'DesignPattern::Proxy'); | 
| 164 | } | } | 
| 165 |  |  | 
| 166 | function set_component_name($name) { | function set_component_name($name) { | 
| 169 | function get_component_name() { | function get_component_name() { | 
| 170 | return $this->_component_name; | return $this->_component_name; | 
| 171 | } | } | 
| 172 | function set_component_options($options) { | function set_component_options_v1() { | 
| 173 | $this->_component_options= $options; | $options = func_get_args(); | 
| 174 |  | if (is_array($options)) { | 
| 175 |  | if (is_hash($options)) { | 
| 176 |  | $this->_component_options = $options; | 
| 177 |  | } else { | 
| 178 |  | $this->_component_options = array(); | 
| 179 |  | foreach ($options as $option) { | 
| 180 |  | array_push($this->_component_options, $option); | 
| 181 |  | } | 
| 182 |  | } | 
| 183 |  | } else { | 
| 184 |  | $this->_component_options = $options; | 
| 185 |  | } | 
| 186 |  | } | 
| 187 |  | function set_component_options() { | 
| 188 |  | $options = func_get_args(); | 
| 189 |  | if (sizeof($options) >= 1) { | 
| 190 |  | $this->_component_options = array(); | 
| 191 |  | foreach ($options as $option) { | 
| 192 |  | array_push($this->_component_options, $option); | 
| 193 |  | } | 
| 194 |  | } else { | 
| 195 |  | $this->_component_options = $options; | 
| 196 |  | } | 
| 197 | } | } | 
| 198 | function get_component_options() { | function get_component_options() { | 
| 199 | return $this->_component_options; | return $this->_component_options; | 
| 200 | } | } | 
| 201 |  |  | 
| 202 | function create_handler() { | function create_proxy() { | 
| 203 | //print "DesignPattern::Proxy->create_handler<br/>"; | //print "DesignPattern::Proxy->create_proxy<br/>"; | 
| 204 | //print "comp: " . $this->get_component_name() . "<br/>"; | //print "comp: " . $this->get_component_name() . "<br/>"; | 
| 205 | //print "comp: " . $this->get_component_options() . "<br/>"; | //print "comp: " . $this->get_component_options() . "<br/>"; | 
| 206 | $handler = php::mkComponent( $this->get_component_name(), $this->get_component_options() ); | $handler = php::mkComponent( $this->get_component_name(), $this->get_component_options() ); | 
| 207 | //print Dumper($handler); | //print Dumper($handler); | 
| 208 | //exit; | //exit; | 
| 209 |  |  | 
| 210 | $this->set_handler( $handler ); | $this->set_proxy( $handler ); | 
| 211 | } | } | 
| 212 |  |  | 
| 213 |  |  |