1<?php
2
3/**
4 * Smarty Resource Data Object
5 * Meta Data Container for Template Files
6 *
7 * @package    Smarty
8 * @subpackage TemplateResources
9 * @author     Rodney Rehm
10 */
11class Smarty_Template_Source
12{
13    /**
14     * Unique Template ID
15     *
16     * @var string
17     */
18    public $uid = null;
19
20    /**
21     * Template Resource (Smarty_Internal_Template::$template_resource)
22     *
23     * @var string
24     */
25    public $resource = null;
26
27    /**
28     * Resource Type
29     *
30     * @var string
31     */
32    public $type = null;
33
34    /**
35     * Resource Name
36     *
37     * @var string
38     */
39    public $name = null;
40
41    /**
42     * Source Filepath
43     *
44     * @var string
45     */
46    public $filepath = null;
47
48    /**
49     * Source Timestamp
50     *
51     * @var integer
52     */
53    public $timestamp = null;
54
55    /**
56     * Source Existence
57     *
58     * @var boolean
59     */
60    public $exists = false;
61
62    /**
63     * Source File Base name
64     *
65     * @var string
66     */
67    public $basename = null;
68
69    /**
70     * The Components an extended template is made of
71     *
72     * @var \Smarty_Template_Source[]
73     */
74    public $components = null;
75
76    /**
77     * Resource Handler
78     *
79     * @var \Smarty_Resource
80     */
81    public $handler = null;
82
83    /**
84     * Smarty instance
85     *
86     * @var Smarty
87     */
88    public $smarty = null;
89
90    /**
91     * Resource is source
92     *
93     * @var bool
94     */
95    public $isConfig = false;
96
97    /**
98     * Template source content eventually set by default handler
99     *
100     * @var string
101     */
102    public $content = null;
103
104    /**
105     * Name of the Class to compile this resource's contents with
106     *
107     * @var string
108     */
109    public $compiler_class = 'Smarty_Internal_SmartyTemplateCompiler';
110
111    /**
112     * Name of the Class to tokenize this resource's contents with
113     *
114     * @var string
115     */
116    public $template_lexer_class = 'Smarty_Internal_Templatelexer';
117
118    /**
119     * Name of the Class to parse this resource's contents with
120     *
121     * @var string
122     */
123    public $template_parser_class = 'Smarty_Internal_Templateparser';
124
125    /**
126     * create Source Object container
127     *
128     * @param Smarty $smarty   Smarty instance this source object belongs to
129     * @param string $resource full template_resource
130     * @param string $type     type of resource
131     * @param string $name     resource name
132     *
133     * @throws   \SmartyException
134     * @internal param \Smarty_Resource $handler Resource Handler this source object communicates with
135     */
136    public function __construct(Smarty $smarty, $resource, $type, $name)
137    {
138        $this->handler =
139            isset($smarty->_cache[ 'resource_handlers' ][ $type ]) ? $smarty->_cache[ 'resource_handlers' ][ $type ] :
140                Smarty_Resource::load($smarty, $type);
141        $this->smarty = $smarty;
142        $this->resource = $resource;
143        $this->type = $type;
144        $this->name = $name;
145    }
146
147    /**
148     * initialize Source Object for given resource
149     * Either [$_template] or [$smarty, $template_resource] must be specified
150     *
151     * @param Smarty_Internal_Template $_template         template object
152     * @param Smarty                   $smarty            smarty object
153     * @param string                   $template_resource resource identifier
154     *
155     * @return Smarty_Template_Source Source Object
156     * @throws SmartyException
157     */
158    public static function load(
159        Smarty_Internal_Template $_template = null,
160        Smarty $smarty = null,
161        $template_resource = null
162    ) {
163        if ($_template) {
164            $smarty = $_template->smarty;
165            $template_resource = $_template->template_resource;
166        }
167        if (empty($template_resource)) {
168            throw new SmartyException('Source: Missing  name');
169        }
170        // parse resource_name, load resource handler, identify unique resource name
171        if (preg_match('/^([A-Za-z0-9_\-]{2,})[:]([\s\S]*)$/', $template_resource, $match)) {
172            $type = $match[ 1 ];
173            $name = $match[ 2 ];
174        } else {
175            // no resource given, use default
176            // or single character before the colon is not a resource type, but part of the filepath
177            $type = $smarty->default_resource_type;
178            $name = $template_resource;
179        }
180        // create new source  object
181        $source = new Smarty_Template_Source($smarty, $template_resource, $type, $name);
182        $source->handler->populate($source, $_template);
183        if (!$source->exists && isset($_template->smarty->default_template_handler_func)) {
184            Smarty_Internal_Method_RegisterDefaultTemplateHandler::_getDefaultTemplate($source);
185            $source->handler->populate($source, $_template);
186        }
187        return $source;
188    }
189
190    /**
191     * Get source time stamp
192     *
193     * @return int
194     */
195    public function getTimeStamp()
196    {
197        if (!isset($this->timestamp)) {
198            $this->handler->populateTimestamp($this);
199        }
200        return $this->timestamp;
201    }
202
203    /**
204     * Get source content
205     *
206     * @return string
207     * @throws \SmartyException
208     */
209    public function getContent()
210    {
211        return isset($this->content) ? $this->content : $this->handler->getContent($this);
212    }
213}
214