]> git.mxchange.org Git - friendica.git/blob - vendor/smarty/smarty/libs/sysplugins/smarty_internal_method_registerdefaulttemplatehandler.php
Add Smarty to Composer
[friendica.git] / vendor / smarty / smarty / libs / sysplugins / smarty_internal_method_registerdefaulttemplatehandler.php
1 <?php
2
3 /**
4  * Smarty Method RegisterDefaultTemplateHandler
5  *
6  * Smarty::registerDefaultTemplateHandler() method
7  *
8  * @package    Smarty
9  * @subpackage PluginsInternal
10  * @author     Uwe Tews
11  */
12 class Smarty_Internal_Method_RegisterDefaultTemplateHandler
13 {
14     /**
15      * Valid for Smarty and template object
16      *
17      * @var int
18      */
19     public $objMap = 3;
20
21     /**
22      * Register template default handler
23      *
24      * @api  Smarty::registerDefaultTemplateHandler()
25      *
26      * @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
27      * @param  callable                                                       $callback class/method name
28      *
29      * @return \Smarty|\Smarty_Internal_Template
30      * @throws SmartyException              if $callback is not callable
31      */
32     public function registerDefaultTemplateHandler(Smarty_Internal_TemplateBase $obj, $callback)
33     {
34         $smarty = $obj->_getSmartyObj();
35         if (is_callable($callback)) {
36             $smarty->default_template_handler_func = $callback;
37         } else {
38             throw new SmartyException("Default template handler not callable");
39         }
40         return $obj;
41     }
42
43     /**
44      * get default content from template or config resource handler
45      *
46      * @param Smarty_Template_Source $source
47      *
48      * @throws \SmartyException
49      */
50     public static function _getDefaultTemplate(Smarty_Template_Source $source)
51     {
52         if ($source->isConfig) {
53             $default_handler = $source->smarty->default_config_handler_func;
54         } else {
55             $default_handler = $source->smarty->default_template_handler_func;
56         }
57         $_content = $_timestamp = null;
58         $_return = call_user_func_array($default_handler,
59                                         array($source->type, $source->name, &$_content, &$_timestamp, $source->smarty));
60         if (is_string($_return)) {
61             $source->exists = is_file($_return);
62             if ($source->exists) {
63                 $source->timestamp = filemtime($_return);
64             } else {
65                 throw new SmartyException("Default handler: Unable to load " .
66                                           ($source->isConfig ? 'config' : 'template') .
67                                           " default file '{$_return}' for '{$source->type}:{$source->name}'");
68             }
69             $source->name = $source->filepath = $_return;
70             $source->uid = sha1($source->filepath);
71         } elseif ($_return === true) {
72             $source->content = $_content;
73             $source->exists = true;
74             $source->uid = $source->name = sha1($_content);
75             $source->handler = Smarty_Resource::load($source->smarty, 'eval');
76         } else {
77             $source->exists = false;
78             throw new SmartyException('Default handler: No ' . ($source->isConfig ? 'config' : 'template') .
79                                       " default content for '{$source->type}:{$source->name}'");
80         }
81     }
82 }