/framework/vendor/smarty3/lib/libs/sysplugins/smarty_internal_register.php

http://zoop.googlecode.com/ · PHP · 247 lines · 118 code · 17 blank · 112 comment · 21 complexity · b4a5768fd5ef6d9a765b1e5501bb75d5 MD5 · raw file

  1. <?php
  2. /**
  3. * Project: Smarty: the PHP compiling template engine
  4. * File: smarty_internal_register.php
  5. * SVN: $Id: $
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * For questions, help, comments, discussion, etc., please join the
  22. * Smarty mailing list. Send a blank e-mail to
  23. * smarty-discussion-subscribe@googlegroups.com
  24. *
  25. * @link http://www.smarty.net/
  26. * @copyright 2008 New Digital Group, Inc.
  27. * @author Monte Ohrt <monte at ohrt dot com>
  28. * @author Uwe Tews
  29. * @package Smarty
  30. * @subpackage PluginsInternal
  31. * @version 3-SVN$Rev: 3286 $
  32. */
  33. class Smarty_Internal_Register {
  34. protected $smarty;
  35. function __construct($smarty) {
  36. $this->smarty = $smarty;
  37. }
  38. /**
  39. * Registers block function to be used in templates
  40. *
  41. * @param string $block_tag name of template block
  42. * @param string $block_impl PHP function to register
  43. * @param boolean $cacheable if true (default) this fuction is cachable
  44. * @param array $cache_attr caching attributes if any
  45. */
  46. function block($block_tag, $block_impl, $cacheable = true, $cache_attr = array())
  47. {
  48. if (isset($this->smarty->registered_plugins['block'][$block_tag])) {
  49. throw new Exception("Plugin tag \"{$block_tag}\" already registered");
  50. } elseif (!is_callable($block_impl)) {
  51. throw new Exception("Plugin \"{$block_tag}\" not callable");
  52. } else {
  53. $this->smarty->registered_plugins['block'][$block_tag] =
  54. array($block_impl, $cacheable, $cache_attr);
  55. }
  56. }
  57. /**
  58. * Registers compiler function
  59. *
  60. * @param string $compiler_tag of template function
  61. * @param string $compiler_impl name of PHP function to register
  62. * @param boolean $cacheable if true (default) this fuction is cachable
  63. */
  64. function compilerFunction($compiler_tag, $compiler_impl, $cacheable = true)
  65. {
  66. if (isset($this->smarty->registered_plugins['compiler'][$compiler_tag])) {
  67. throw new Exception("Plugin tag \"{$compiler_tag}\" already registered");
  68. } elseif (!is_callable($compiler_impl)) {
  69. throw new Exception("Plugin \"{$compiler_tag}\" not callable");
  70. } else {
  71. $this->smarty->registered_plugins['compiler'][$compiler_tag] =
  72. array($compiler_impl, $cacheable);
  73. }
  74. }
  75. /**
  76. * Registers custom function to be used in templates
  77. *
  78. * @param string $function_tag the name of the template function
  79. * @param string $function_impl the name of the PHP function to register
  80. * @param boolean $cacheable if true (default) this fuction is cachable
  81. * @param array $cache_attr caching attributes if any
  82. */
  83. function templateFunction($function_tag, $function_impl, $cacheable = true, $cache_attr = array())
  84. {
  85. if (isset($this->smarty->registered_plugins['function'][$function_tag])) {
  86. throw new Exception("Plugin tag \"{$function_tag}\" already registered");
  87. } elseif (!is_callable($function_impl)) {
  88. throw new Exception("Plugin \"{$function_tag}\" not callable");
  89. } else {
  90. $this->smarty->registered_plugins['function'][$function_tag] =
  91. array($function_impl, $cacheable, $cache_attr);
  92. }
  93. }
  94. /**
  95. * Registers modifier to be used in templates
  96. *
  97. * @param string $modifier_name name of template modifier
  98. * @param string $modifier_impl name of PHP function to register
  99. */
  100. function modifier($modifier_name, $modifier_impl)
  101. {
  102. if (isset($this->smarty->registered_plugins['modifier'][$modifier_name])) {
  103. throw new Exception("Plugin \"{$modifier}\" already registered");
  104. } elseif (!is_callable($modifier_impl)) {
  105. throw new Exception("Plugin \"{$modifier}\" not callable");
  106. } else {
  107. $this->smarty->registered_plugins['modifier'][$modifier_name] =
  108. array($modifier_impl);
  109. }
  110. }
  111. /**
  112. * Registers object to be used in templates
  113. *
  114. * @param string $object name of template object
  115. * @param object $ &$object_impl the referenced PHP object to register
  116. * @param mixed null | array $allowed list of allowed methods (empty = all)
  117. * @param boolean $smarty_args smarty argument format, else traditional
  118. * @param mixed null | array $block_functs list of methods that are block format
  119. */
  120. function templateObject($object_name, $object_impl, $allowed = array(), $smarty_args = true, $block_methods = array())
  121. {
  122. // test if allowed methodes callable
  123. if (!empty($allowed)) {
  124. foreach ((array)$allowed as $method) {
  125. if (!is_callable(array($object_impl, $method))) {
  126. throw new Exception("Undefined method '$method' in registered object");
  127. }
  128. }
  129. }
  130. // test if block methodes callable
  131. if (!empty($block_methods)) {
  132. foreach ((array)$block_methods as $method) {
  133. if (!is_callable(array($object_impl, $method))) {
  134. throw new Exception("Undefined method '$method' in registered object");
  135. }
  136. }
  137. }
  138. // register the object
  139. $this->smarty->registered_objects[$object_name] =
  140. array($object_impl, (array)$allowed, (boolean)$smarty_args, (array)$block_methods);
  141. }
  142. /**
  143. * Registers an output filter function to apply
  144. * to a template output
  145. *
  146. * @param callback $function_name
  147. */
  148. function outputFilter($function_name)
  149. {
  150. $this->smarty->registered_filters['output'][$this->smarty->_get_filter_name($function_name)] = $function_name;
  151. }
  152. /**
  153. * Registers a postfilter function to apply
  154. * to a compiled template after compilation
  155. *
  156. * @param callback $function_name
  157. */
  158. function postFilter($function_name)
  159. {
  160. $this->smarty->registered_filters['post'][$this->smarty->_get_filter_name($function_name)] = $function_name;
  161. }
  162. /**
  163. * Registers a prefilter function to apply
  164. * to a template before compiling
  165. *
  166. * @param callback $function_name
  167. */
  168. function preFilter($function_name)
  169. {
  170. $this->smarty->registered_filters['pre'][$this->smarty->_get_filter_name($function_name)] = $function_name;
  171. }
  172. /**
  173. * Registers a resource to fetch a template
  174. *
  175. * @param string $resource_type name of resource type
  176. * @param array $function_names array of functions to handle resource
  177. */
  178. function resource($resource_type, $function_names)
  179. {
  180. if (count($function_names) == 4) {
  181. $this->smarty->_plugins['resource'][$resource_type] =
  182. array($function_names, false);
  183. } elseif (count($function_names) == 5) {
  184. $this->smarty->_plugins['resource'][$resource_type] =
  185. array(array(array(&$function_names[0], $function_names[1]),
  186. array(&$function_names[0], $function_names[2]),
  187. array(&$function_names[0], $function_names[3]),
  188. array(&$function_names[0], $function_names[4])),
  189. false);
  190. } else {
  191. throw new Exception("malformed function-list for '$resource_type' in register_resource");
  192. }
  193. }
  194. /**
  195. * Registers an output filter function which
  196. * runs over any variable output
  197. *
  198. * @param callback $function_name
  199. */
  200. function variableFilter($function_name)
  201. {
  202. $this->smarty->registered_filters['variable'][$this->smarty->_get_filter_name($function_name)] = $function_name;
  203. }
  204. /**
  205. * Registers a default plugin handler
  206. *
  207. * @param $function_name mixed string | array $plugin class/methode name
  208. */
  209. function defaultPluginHandler($function_name)
  210. {
  211. if (is_callable($function_name)) {
  212. $this->smarty->default_plugin_handler_func = $function_name;
  213. } else {
  214. throw new Exception("Default plugin handler '$function_name' not callable");
  215. }
  216. }
  217. /**
  218. * Registers a default template handler
  219. *
  220. * @param $function_name mixed string | array class/method name
  221. */
  222. function defaultTemplateHandler($function_name)
  223. {
  224. if (is_callable($function_name)) {
  225. $this->smarty->default_template_handler_func = $function_name;
  226. } else {
  227. throw new Exception("Default template handler '$function_name' not callable");
  228. }
  229. }
  230. }