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