Array.php
6 years ago
Default.php
6 years ago
Plugin.php
6 years ago
Proxy.php
6 years ago
Smarty.php
6 years ago
Proxy.php
265 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Proxy class for HTML_QuickForm2 renderers and their plugins |
| 4 | * |
| 5 | * PHP version 5 |
| 6 | * |
| 7 | * LICENSE: |
| 8 | * |
| 9 | * Copyright (c) 2006-2010, Alexey Borzov <avb@php.net>, |
| 10 | * Bertrand Mansion <golgote@mamasam.com> |
| 11 | * All rights reserved. |
| 12 | * |
| 13 | * Redistribution and use in source and binary forms, with or without |
| 14 | * modification, are permitted provided that the following conditions |
| 15 | * are met: |
| 16 | * |
| 17 | * * Redistributions of source code must retain the above copyright |
| 18 | * notice, this list of conditions and the following disclaimer. |
| 19 | * * Redistributions in binary form must reproduce the above copyright |
| 20 | * notice, this list of conditions and the following disclaimer in the |
| 21 | * documentation and/or other materials provided with the distribution. |
| 22 | * * The names of the authors may not be used to endorse or promote products |
| 23 | * derived from this software without specific prior written permission. |
| 24 | * |
| 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 26 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 27 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 28 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 29 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 30 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 31 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 32 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 33 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 34 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 35 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 | * |
| 37 | * @category HTML |
| 38 | * @package HTML_QuickForm2 |
| 39 | * @author Alexey Borzov <avb@php.net> |
| 40 | * @author Bertrand Mansion <golgote@mamasam.com> |
| 41 | * @license http://opensource.org/licenses/bsd-license.php New BSD License |
| 42 | * @version SVN: $Id: Proxy.php 299706 2010-05-24 18:32:37Z avb $ |
| 43 | * @link http://pear.php.net/package/HTML_QuickForm2 |
| 44 | */ |
| 45 | |
| 46 | /** |
| 47 | * Abstract base class for QuickForm2 renderers |
| 48 | */ |
| 49 | // require_once 'HTML/QuickForm2/Renderer.php'; |
| 50 | |
| 51 | /** |
| 52 | * Proxy class for HTML_QuickForm2 renderers and their plugins |
| 53 | * |
| 54 | * This class serves two purposes: |
| 55 | * <ol> |
| 56 | * <li>Aggregates renderer and its plugins. From user's point of view |
| 57 | * renderer plugins simply add new methods to renderer instances.</li> |
| 58 | * <li>Restricts access to renderer properties and methods. Those are defined |
| 59 | * as 'public' to allow easy access from plugins, but only methods |
| 60 | * with names explicitly returned by Renderer::exportMethods() are |
| 61 | * available to the outside world.</li> |
| 62 | * </ol> |
| 63 | * |
| 64 | * @category HTML |
| 65 | * @package HTML_QuickForm2 |
| 66 | * @author Alexey Borzov <avb@php.net> |
| 67 | * @author Bertrand Mansion <golgote@mamasam.com> |
| 68 | * @version Release: @package_version@ |
| 69 | */ |
| 70 | class HTML_QuickForm2_Renderer_Proxy extends HTML_QuickForm2_Renderer |
| 71 | { |
| 72 | /** |
| 73 | * Renderer instance |
| 74 | * @var HTML_QuickForm2_Renderer |
| 75 | */ |
| 76 | private $_renderer; |
| 77 | |
| 78 | /** |
| 79 | * Additional renderer methods to proxy via __call(), as returned by exportMethods() |
| 80 | * @var array |
| 81 | */ |
| 82 | private $_rendererMethods = array(); |
| 83 | |
| 84 | /** |
| 85 | * Reference to a list of registered renderer plugins for that renderer type |
| 86 | * @var array |
| 87 | */ |
| 88 | private $_pluginClasses; |
| 89 | |
| 90 | /** |
| 91 | * Plugins for this renderer |
| 92 | * @var array |
| 93 | */ |
| 94 | private $_plugins = array(); |
| 95 | |
| 96 | /** |
| 97 | * Plugin methods to call via __call() magic method |
| 98 | * |
| 99 | * Array has the form ('lowercase method name' => 'index in _plugins array') |
| 100 | * |
| 101 | * @var array |
| 102 | */ |
| 103 | private $_pluginMethods = array(); |
| 104 | |
| 105 | /** |
| 106 | * Constructor, sets proxied renderer and its plugins |
| 107 | * |
| 108 | * @param HTML_QuickForm2_Renderer Renderer instance to proxy |
| 109 | * @param array Plugins registered for that renderer type |
| 110 | */ |
| 111 | protected function __construct(HTML_QuickForm2_Renderer $renderer, array &$pluginClasses) |
| 112 | { |
| 113 | foreach ($renderer->exportMethods() as $method) { |
| 114 | $this->_rendererMethods[strtolower($method)] = true; |
| 115 | } |
| 116 | $this->_renderer = $renderer; |
| 117 | $this->_pluginClasses = &$pluginClasses; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Magic function; call an imported method of a renderer or its plugin |
| 122 | * |
| 123 | * @param string method name |
| 124 | * @param array method arguments |
| 125 | * @return mixed |
| 126 | */ |
| 127 | public function __call($name, $arguments) |
| 128 | { |
| 129 | $lower = strtolower($name); |
| 130 | if (isset($this->_rendererMethods[$lower])) { |
| 131 | // support fluent interfaces |
| 132 | $ret = call_user_func_array(array($this->_renderer, $name), $arguments); |
| 133 | return $ret === $this->_renderer? $this: $ret; |
| 134 | } |
| 135 | // any additional plugins since last __call()? |
| 136 | for ($i = count($this->_plugins); $i < count($this->_pluginClasses); $i++) { |
| 137 | list($className, $includeFile) = $this->_pluginClasses[$i]; |
| 138 | if (!class_exists($className)) { |
| 139 | HTML_QuickForm2_Loader::loadClass($className, $includeFile); |
| 140 | } |
| 141 | $this->addPlugin($i, new $className); |
| 142 | } |
| 143 | if (isset($this->_pluginMethods[$lower])) { |
| 144 | return call_user_func_array( |
| 145 | array($this->_plugins[$this->_pluginMethods[$lower]], $name), |
| 146 | $arguments |
| 147 | ); |
| 148 | } |
| 149 | trigger_error("Fatal error: Call to undefined method " . |
| 150 | get_class($this->_renderer) . "::" . $name . "()", E_USER_ERROR); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Adds a plugin for the current renderer instance |
| 155 | * |
| 156 | * Plugin's methods are imported and can be later called as this object's own |
| 157 | * |
| 158 | * @param HTML_QuickForm2_Renderer_Plugin a plugin instance |
| 159 | * @throws HTML_QuickForm2_InvalidArgumentException if a plugin has already |
| 160 | * imported name |
| 161 | */ |
| 162 | protected function addPlugin($index, HTML_QuickForm2_Renderer_Plugin $plugin) |
| 163 | { |
| 164 | $methods = array(); |
| 165 | $reflection = new ReflectionObject($plugin); |
| 166 | foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
| 167 | $lower = strtolower($method->getName()); |
| 168 | if ('HTML_QuickForm2_Renderer_Plugin' == $method->getDeclaringClass()->getName()) { |
| 169 | continue; |
| 170 | } elseif (isset($this->_rendererMethods[$lower]) |
| 171 | || isset($this->_pluginMethods[$lower]) |
| 172 | ) { |
| 173 | throw new HTML_QuickForm2_InvalidArgumentException( |
| 174 | 'Duplicate method name: name ' . $method->getName() . ' in plugin ' . |
| 175 | get_class($plugin) . ' already taken by ' . |
| 176 | (isset($this->_rendererMethods[$lower])? |
| 177 | get_class($this->_renderer): |
| 178 | get_class($this->_plugins[$this->_pluginMethods[$lower]]) |
| 179 | ) |
| 180 | ); |
| 181 | } |
| 182 | $methods[$lower] = $index; |
| 183 | } |
| 184 | $plugin->setRenderer($this->_renderer); |
| 185 | $this->_plugins[$index] = $plugin; |
| 186 | $this->_pluginMethods += $methods; |
| 187 | } |
| 188 | |
| 189 | /**#@+ |
| 190 | * Proxies for methods defined in {@link HTML_QuickForm2_Renderer} |
| 191 | */ |
| 192 | public function setOption($nameOrOptions, $value = null) |
| 193 | { |
| 194 | $this->_renderer->setOption($nameOrOptions, $value); |
| 195 | return $this; |
| 196 | } |
| 197 | |
| 198 | public function getOption($name = null) |
| 199 | { |
| 200 | return $this->_renderer->getOption($name); |
| 201 | } |
| 202 | |
| 203 | public function getJavascriptBuilder() |
| 204 | { |
| 205 | return $this->_renderer->getJavascriptBuilder(); |
| 206 | } |
| 207 | |
| 208 | public function setJavascriptBuilder(HTML_QuickForm2_JavascriptBuilder $builder = null) |
| 209 | { |
| 210 | $this->_renderer->setJavascriptBuilder($builder); |
| 211 | return $this; |
| 212 | } |
| 213 | |
| 214 | public function renderElement(HTML_QuickForm2_Node $element) |
| 215 | { |
| 216 | $this->_renderer->renderElement($element); |
| 217 | } |
| 218 | |
| 219 | public function renderHidden(HTML_QuickForm2_Node $element) |
| 220 | { |
| 221 | $this->_renderer->renderHidden($element); |
| 222 | } |
| 223 | |
| 224 | public function startForm(HTML_QuickForm2_Node $form) |
| 225 | { |
| 226 | $this->_renderer->startForm($form); |
| 227 | } |
| 228 | |
| 229 | public function finishForm(HTML_QuickForm2_Node $form) |
| 230 | { |
| 231 | $this->_renderer->finishForm($form); |
| 232 | } |
| 233 | |
| 234 | public function startContainer(HTML_QuickForm2_Node $container) |
| 235 | { |
| 236 | $this->_renderer->startContainer($container); |
| 237 | } |
| 238 | |
| 239 | public function finishContainer(HTML_QuickForm2_Node $container) |
| 240 | { |
| 241 | $this->_renderer->finishContainer($container); |
| 242 | } |
| 243 | |
| 244 | public function startGroup(HTML_QuickForm2_Node $group) |
| 245 | { |
| 246 | $this->_renderer->startGroup($group); |
| 247 | } |
| 248 | |
| 249 | public function finishGroup(HTML_QuickForm2_Node $group) |
| 250 | { |
| 251 | $this->_renderer->finishGroup($group); |
| 252 | } |
| 253 | /**#@-*/ |
| 254 | |
| 255 | public function __toString() |
| 256 | { |
| 257 | if (method_exists($this->_renderer, '__toString')) { |
| 258 | return $this->_renderer->__toString(); |
| 259 | } |
| 260 | trigger_error("Fatal error: Object of class " . get_class($this->_renderer) . |
| 261 | " could not be converted to string", E_USER_ERROR); |
| 262 | } |
| 263 | } |
| 264 | ?> |
| 265 |