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 */
12class 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(
59            $default_handler,
60            array($source->type, $source->name, &$_content, &$_timestamp, $source->smarty)
61        );
62        if (is_string($_return)) {
63            $source->exists = is_file($_return);
64            if ($source->exists) {
65                $source->timestamp = filemtime($_return);
66            } else {
67                throw new SmartyException(
68                    'Default handler: Unable to load ' .
69                    ($source->isConfig ? 'config' : 'template') .
70                    " default file '{$_return}' for '{$source->type}:{$source->name}'"
71                );
72            }
73            $source->name = $source->filepath = $_return;
74            $source->uid = sha1($source->filepath);
75        } elseif ($_return === true) {
76            $source->content = $_content;
77            $source->exists = true;
78            $source->uid = $source->name = sha1($_content);
79            $source->handler = Smarty_Resource::load($source->smarty, 'eval');
80        } else {
81            $source->exists = false;
82            throw new SmartyException(
83                'Default handler: No ' . ($source->isConfig ? 'config' : 'template') .
84                " default content for '{$source->type}:{$source->name}'"
85            );
86        }
87    }
88}
89