1<?php
2
3/**
4 * Smarty Method RegisterPlugin
5 *
6 * Smarty::registerPlugin() method
7 *
8 * @package    Smarty
9 * @subpackage PluginsInternal
10 * @author     Uwe Tews
11 */
12class Smarty_Internal_Method_RegisterPlugin
13{
14    /**
15     * Valid for Smarty and template object
16     *
17     * @var int
18     */
19    public $objMap = 3;
20
21    /**
22     * Registers plugin to be used in templates
23     *
24     * @api  Smarty::registerPlugin()
25     * @link http://www.smarty.net/docs/en/api.register.plugin.tpl
26     *
27     * @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
28     * @param string                                                          $type       plugin type
29     * @param string                                                          $name       name of template tag
30     * @param callback                                                        $callback   PHP callback to register
31     * @param bool                                                            $cacheable  if true (default) this
32     *                                                                                    function is cache able
33     * @param mixed                                                           $cache_attr caching attributes if any
34     *
35     * @return \Smarty|\Smarty_Internal_Template
36     * @throws SmartyException              when the plugin tag is invalid
37     */
38    public function registerPlugin(
39        Smarty_Internal_TemplateBase $obj,
40        $type,
41        $name,
42        $callback,
43        $cacheable = true,
44        $cache_attr = null
45    ) {
46        $smarty = $obj->_getSmartyObj();
47        if (isset($smarty->registered_plugins[ $type ][ $name ])) {
48            throw new SmartyException("Plugin tag '{$name}' already registered");
49        } elseif (!is_callable($callback)) {
50            throw new SmartyException("Plugin '{$name}' not callable");
51        } elseif ($cacheable && $cache_attr) {
52            throw new SmartyException("Cannot set caching attributes for plugin '{$name}' when it is cacheable.");
53        } else {
54            $smarty->registered_plugins[ $type ][ $name ] = array($callback, (bool)$cacheable, (array)$cache_attr);
55        }
56        return $obj;
57    }
58}
59