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