Initial repo created
[timetracker.git] / WEB-INF / lib / smarty / sysplugins / smarty_internal_register.php
1 <?php
2
3 /**
4  * Smarty Internal Plugin Register
5  * 
6  * External Smarty methods register/unregister
7  * 
8  * @package Smarty
9  * @author Uwe Tews 
10  */
11
12 /**
13  * Class for register/unregister methods
14  */
15 class Smarty_Internal_Register {
16
17     function __construct($smarty)
18     {
19         $this->smarty = $smarty;
20     } 
21     /**
22      * Registers plugin to be used in templates
23      * 
24      * @param string $type plugin type
25      * @param string $tag name of template tag
26      * @param callback $callback PHP callback to register
27      * @param boolean $cacheable if true (default) this fuction is cachable
28      * @param array $cache_attr caching attributes if any
29      */
30
31         public function registerPlugin($type, $tag, $callback, $cacheable = true, $cache_attr = null)
32         {
33                 if (isset($this->smarty->registered_plugins[$type][$tag])) {
34                 throw new Exception("Plugin tag \"{$tag}\" already registered");
35         } elseif (!is_callable($callback)) {
36                 throw new Exception("Plugin \"{$tag}\" not callable");
37         } else {
38                 $this->smarty->registered_plugins[$type][$tag] = array($callback, (bool) $cacheable, (array) $cache_attr);
39         }
40         } 
41
42     /**
43      * Unregister Plugin
44      * 
45      * @param string $type of plugin
46      * @param string $tag name of plugin
47      */
48     function unregisterPlugin($type, $tag)
49     {
50         if (isset($this->smarty->registered_plugins[$type][$tag])) {
51             unset($this->smarty->registered_plugins[$type][$tag]);
52         } 
53     } 
54  
55     /**
56      * Registers a resource to fetch a template
57      * 
58      * @param string $type name of resource type
59      * @param array $callback array of callbacks to handle resource
60      */
61         public function registerResource($type, $callback)
62         {
63         $this->smarty->registered_resources[$type] = array($callback, false);
64     }
65
66     /**
67      * Unregisters a resource 
68      * 
69      * @param string $type name of resource type
70      */
71    function unregisterResource($type)
72     {
73         if (isset($this->smarty->registered_resources[$type])) {
74             unset($this->smarty->registered_resources[$type]);
75         } 
76     } 
77
78
79     /**
80      * Registers object to be used in templates
81      * 
82      * @param string $object name of template object
83      * @param object $ &$object_impl the referenced PHP object to register
84      * @param mixed $ null | array $allowed list of allowed methods (empty = all)
85      * @param boolean $smarty_args smarty argument format, else traditional
86      * @param mixed $ null | array $block_functs list of methods that are block format
87      */
88     function registerObject($object_name, $object_impl, $allowed = array(), $smarty_args = true, $block_methods = array())
89     { 
90         // test if allowed methodes callable
91         if (!empty($allowed)) {
92             foreach ((array)$allowed as $method) {
93                 if (!is_callable(array($object_impl, $method))) {
94                     throw new SmartyException("Undefined method '$method' in registered object");
95                 } 
96             } 
97         } 
98         // test if block methodes callable
99         if (!empty($block_methods)) {
100             foreach ((array)$block_methods as $method) {
101                 if (!is_callable(array($object_impl, $method))) {
102                     throw new SmartyException("Undefined method '$method' in registered object");
103                 } 
104             } 
105         } 
106         // register the object
107         $this->smarty->registered_objects[$object_name] =
108         array($object_impl, (array)$allowed, (boolean)$smarty_args, (array)$block_methods);
109     } 
110
111     /**
112      * Registers static classes to be used in templates
113      * 
114      * @param string $class name of template class
115      * @param string $class_impl the referenced PHP class to register
116      */
117     function registerClass($class_name, $class_impl)
118     { 
119         // test if exists
120         if (!class_exists($class_impl)) {
121             throw new SmartyException("Undefined class '$class_impl' in register template class");
122         } 
123         // register the class
124         $this->smarty->registered_classes[$class_name] = $class_impl;
125     } 
126
127     /**
128      * Registers a default plugin handler
129      * 
130      * @param  $callback mixed string | array $plugin class/methode name
131      */
132     function registerDefaultPluginHandler($callback)
133     {
134         if (is_callable($callback)) {
135             $this->smarty->default_plugin_handler_func = $callback;
136         } else {
137             throw new SmartyException("Default plugin handler '$callback' not callable");
138         } 
139     } 
140
141     /**
142      * Registers a default template handler
143      * 
144      * @param  $callback mixed string | array class/method name
145      */
146     function registerDefaultTemplateHandler($callback)
147     {
148         if (is_callable($callback)) {
149             $this->smarty->default_template_handler_func = $callback;
150         } else {
151             throw new SmartyException("Default template handler '$callback' not callable");
152         } 
153     } 
154
155 }
156 ?>