1<?php
2/**
3 * Smarty Internal Plugin Resource Extends
4 *
5 * @package    Smarty
6 * @subpackage TemplateResources
7 * @author     Uwe Tews
8 * @author     Rodney Rehm
9 */
10
11/**
12 * Smarty Internal Plugin Resource Extends
13 * Implements the file system as resource for Smarty which {extend}s a chain of template files templates
14 *
15 * @package    Smarty
16 * @subpackage TemplateResources
17 */
18class Smarty_Internal_Resource_Extends extends Smarty_Resource
19{
20    /**
21     * mbstring.overload flag
22     *
23     * @var int
24     */
25    public $mbstring_overload = 0;
26
27    /**
28     * populate Source Object with meta data from Resource
29     *
30     * @param Smarty_Template_Source   $source    source object
31     * @param Smarty_Internal_Template $_template template object
32     *
33     * @throws SmartyException
34     */
35    public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
36    {
37        $uid = '';
38        $sources = array();
39        $components = explode('|', $source->name);
40        $smarty = &$source->smarty;
41        $exists = true;
42        foreach ($components as $component) {
43            /* @var \Smarty_Template_Source $_s */
44            $_s = Smarty_Template_Source::load(null, $smarty, $component);
45            if ($_s->type === 'php') {
46                throw new SmartyException("Resource type {$_s->type} cannot be used with the extends resource type");
47            }
48            $sources[ $_s->uid ] = $_s;
49            $uid .= $_s->filepath;
50            if ($_template) {
51                $exists = $exists && $_s->exists;
52            }
53        }
54        $source->components = $sources;
55        $source->filepath = $_s->filepath;
56        $source->uid = sha1($uid . $source->smarty->_joined_template_dir);
57        $source->exists = $exists;
58        if ($_template) {
59            $source->timestamp = $_s->timestamp;
60        }
61    }
62
63    /**
64     * populate Source Object with timestamp and exists from Resource
65     *
66     * @param Smarty_Template_Source $source source object
67     */
68    public function populateTimestamp(Smarty_Template_Source $source)
69    {
70        $source->exists = true;
71        /* @var \Smarty_Template_Source $_s */
72        foreach ($source->components as $_s) {
73            $source->exists = $source->exists && $_s->exists;
74        }
75        $source->timestamp = $source->exists ? $_s->getTimeStamp() : false;
76    }
77
78    /**
79     * Load template's source from files into current template object
80     *
81     * @param Smarty_Template_Source $source source object
82     *
83     * @return string template source
84     * @throws SmartyException if source cannot be loaded
85     */
86    public function getContent(Smarty_Template_Source $source)
87    {
88        if (!$source->exists) {
89            throw new SmartyException("Unable to load template '{$source->type}:{$source->name}'");
90        }
91        $_components = array_reverse($source->components);
92        $_content = '';
93        /* @var \Smarty_Template_Source $_s */
94        foreach ($_components as $_s) {
95            // read content
96            $_content .= $_s->getContent();
97        }
98        return $_content;
99    }
100
101    /**
102     * Determine basename for compiled filename
103     *
104     * @param Smarty_Template_Source $source source object
105     *
106     * @return string resource's basename
107     */
108    public function getBasename(Smarty_Template_Source $source)
109    {
110        return str_replace(':', '.', basename($source->filepath));
111    }
112
113    /*
114      * Disable timestamp checks for extends resource.
115      * The individual source components will be checked.
116      *
117      * @return bool
118      */
119    /**
120     * @return bool
121     */
122    public function checkTimestamps()
123    {
124        return false;
125    }
126}
127