|
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 * @property integer $timestamp Source Timestamp |
|
11 * @property boolean $exists Source Existence |
|
12 * @property boolean $template Extended Template reference |
|
13 * @property string $content Source Content |
|
14 */ |
|
15 class Smarty_Template_Source { |
|
16 /** |
|
17 * Name of the Class to compile this resource's contents with |
|
18 * |
|
19 * @var string |
|
20 */ |
|
21 public $compiler_class = null; |
|
22 |
|
23 /** |
|
24 * Name of the Class to tokenize this resource's contents with |
|
25 * |
|
26 * @var string |
|
27 */ |
|
28 public $template_lexer_class = null; |
|
29 |
|
30 /** |
|
31 * Name of the Class to parse this resource's contents with |
|
32 * |
|
33 * @var string |
|
34 */ |
|
35 public $template_parser_class = null; |
|
36 |
|
37 /** |
|
38 * Unique Template ID |
|
39 * |
|
40 * @var string |
|
41 */ |
|
42 public $uid = null; |
|
43 |
|
44 /** |
|
45 * Template Resource (Smarty_Internal_Template::$template_resource) |
|
46 * |
|
47 * @var string |
|
48 */ |
|
49 public $resource = null; |
|
50 |
|
51 /** |
|
52 * Resource Type |
|
53 * |
|
54 * @var string |
|
55 */ |
|
56 public $type = null; |
|
57 |
|
58 /** |
|
59 * Resource Name |
|
60 * |
|
61 * @var string |
|
62 */ |
|
63 public $name = null; |
|
64 |
|
65 /** |
|
66 * Unique Resource Name |
|
67 * |
|
68 * @var string |
|
69 */ |
|
70 public $unique_resource = null; |
|
71 |
|
72 /** |
|
73 * Source Filepath |
|
74 * |
|
75 * @var string |
|
76 */ |
|
77 public $filepath = null; |
|
78 /** |
|
79 * Source File Base name |
|
80 * |
|
81 * @var string |
|
82 */ |
|
83 public $basename = null; |
|
84 |
|
85 /** |
|
86 * The Components an extended template is made of |
|
87 * |
|
88 * @var array |
|
89 */ |
|
90 public $components = null; |
|
91 |
|
92 /** |
|
93 * Resource Handler |
|
94 * |
|
95 * @var Smarty_Resource |
|
96 */ |
|
97 public $handler = null; |
|
98 |
|
99 /** |
|
100 * Smarty instance |
|
101 * |
|
102 * @var Smarty |
|
103 */ |
|
104 public $smarty = null; |
|
105 /** |
|
106 * Resource is source |
|
107 * |
|
108 * @var bool |
|
109 */ |
|
110 public $isConfig = false; |
|
111 /** |
|
112 * Source is bypassing compiler |
|
113 * |
|
114 * @var boolean |
|
115 */ |
|
116 public $uncompiled = false; |
|
117 |
|
118 /** |
|
119 * Source must be recompiled on every occasion |
|
120 * |
|
121 * @var boolean |
|
122 */ |
|
123 public $recompiled = false; |
|
124 /** |
|
125 * cache for Smarty_Template_Compiled instances |
|
126 * |
|
127 * @var array |
|
128 */ |
|
129 public $compileds = array(); |
|
130 |
|
131 /** |
|
132 * create Source Object container |
|
133 * |
|
134 * @param Smarty_Resource $handler Resource Handler this source object communicates with |
|
135 * @param Smarty $smarty Smarty instance this source object belongs to |
|
136 * @param string $resource full template_resource |
|
137 * @param string $type type of resource |
|
138 * @param string $name resource name |
|
139 * |
|
140 * @internal param string $unique_resource unique resource name |
|
141 */ |
|
142 public function __construct(Smarty_Resource $handler, Smarty $smarty, $resource, $type, $name) { |
|
143 $this->handler = $handler; // Note: prone to circular references |
|
144 |
|
145 $this->recompiled = $handler->recompiled; |
|
146 $this->uncompiled = $handler->uncompiled; |
|
147 $this->compiler_class = $handler->compiler_class; |
|
148 $this->template_lexer_class = $handler->template_lexer_class; |
|
149 $this->template_parser_class = $handler->template_parser_class; |
|
150 |
|
151 $this->smarty = $smarty; |
|
152 $this->resource = $resource; |
|
153 $this->type = $type; |
|
154 $this->name = $name; |
|
155 } |
|
156 |
|
157 /** |
|
158 * initialize Source Object for given resource |
|
159 * Either [$_template] or [$smarty, $template_resource] must be specified |
|
160 * |
|
161 * @param Smarty_Internal_Template $_template template object |
|
162 * @param Smarty $smarty smarty object |
|
163 * @param string $template_resource resource identifier |
|
164 * |
|
165 * @return Smarty_Template_Source Source Object |
|
166 * @throws SmartyException |
|
167 */ |
|
168 public static function load(Smarty_Internal_Template $_template = null, Smarty $smarty = null, $template_resource = null) { |
|
169 if ($_template) { |
|
170 $smarty = $_template->smarty; |
|
171 $template_resource = $_template->template_resource; |
|
172 } |
|
173 if (empty($template_resource)) { |
|
174 throw new SmartyException('Missing template name'); |
|
175 } |
|
176 // parse resource_name, load resource handler, identify unique resource name |
|
177 list($name, $type) = Smarty_Resource::parseResourceName($template_resource, $smarty->default_resource_type); |
|
178 $resource = Smarty_Resource::load($smarty, $type); |
|
179 // if resource is not recompiling and resource name is not dotted we can check the source cache |
|
180 if ($smarty->resource_caching && !$resource->recompiled && !(isset($name[1]) && $name[0] == '.' && ($name[1] == '.' || $name[1] == '/'))) { |
|
181 $unique_resource = $resource->buildUniqueResourceName($smarty, $name); |
|
182 if (isset($smarty->source_objects[$unique_resource])) { |
|
183 return $smarty->source_objects[$unique_resource]; |
|
184 } |
|
185 } else { |
|
186 $unique_resource = null; |
|
187 } |
|
188 // create new source object |
|
189 $source = new Smarty_Template_Source($resource, $smarty, $template_resource, $type, $name); |
|
190 $resource->populate($source, $_template); |
|
191 if ((!isset($source->exists) || !$source->exists) && isset($_template->smarty->default_template_handler_func)) { |
|
192 Smarty_Internal_Extension_DefaultTemplateHandler::_getDefault($_template, $source, $resObj); |
|
193 } |
|
194 // on recompiling resources we are done |
|
195 if ($smarty->resource_caching && !$resource->recompiled) { |
|
196 // may by we have already $unique_resource |
|
197 $is_relative = false; |
|
198 if (!isset($unique_resource)) { |
|
199 $is_relative = isset($name[1]) && $name[0] == '.' && ($name[1] == '.' || $name[1] == '/') && |
|
200 ($type == 'file' || (isset($_template->parent->source) && $_template->parent->source->type == 'extends')); |
|
201 $unique_resource = $resource->buildUniqueResourceName($smarty, $is_relative ? $source->filepath . $name : $name); |
|
202 } |
|
203 $source->unique_resource = $unique_resource; |
|
204 // save in runtime cache if not relative |
|
205 if (!$is_relative) { |
|
206 $smarty->source_objects[$unique_resource] = $source; |
|
207 } |
|
208 } |
|
209 return $source; |
|
210 } |
|
211 |
|
212 /** |
|
213 * render the uncompiled source |
|
214 * |
|
215 * @param Smarty_Internal_Template $_template template object |
|
216 */ |
|
217 public function renderUncompiled(Smarty_Internal_Template $_template) { |
|
218 $level = ob_get_level(); |
|
219 ob_start(); |
|
220 try { |
|
221 $this->handler->renderUncompiled($_template->source, $_template); |
|
222 return ob_get_clean(); |
|
223 } catch (Exception $e) { |
|
224 while (ob_get_level() > $level) { |
|
225 ob_end_clean(); |
|
226 } |
|
227 throw $e; |
|
228 } |
|
229 } |
|
230 |
|
231 /** |
|
232 * <<magic>> Generic Setter. |
|
233 * |
|
234 * @param string $property_name valid: timestamp, exists, content, template |
|
235 * @param mixed $value new value (is not checked) |
|
236 * |
|
237 * @throws SmartyException if $property_name is not valid |
|
238 */ |
|
239 public function __set($property_name, $value) { |
|
240 switch ($property_name) { |
|
241 // regular attributes |
|
242 case 'timestamp': |
|
243 case 'exists': |
|
244 case 'content': |
|
245 // required for extends: only |
|
246 case 'template': |
|
247 $this->$property_name = $value; |
|
248 break; |
|
249 |
|
250 default: |
|
251 throw new SmartyException("source property '$property_name' does not exist."); |
|
252 } |
|
253 } |
|
254 |
|
255 /** |
|
256 * <<magic>> Generic getter. |
|
257 * |
|
258 * @param string $property_name valid: timestamp, exists, content |
|
259 * |
|
260 * @return mixed |
|
261 * @throws SmartyException if $property_name is not valid |
|
262 */ |
|
263 public function __get($property_name) { |
|
264 switch ($property_name) { |
|
265 case 'timestamp': |
|
266 case 'exists': |
|
267 $this->handler->populateTimestamp($this); |
|
268 |
|
269 return $this->$property_name; |
|
270 |
|
271 case 'content': |
|
272 return $this->content = $this->handler->getContent($this); |
|
273 |
|
274 default: |
|
275 throw new SmartyException("source property '$property_name' does not exist."); |
|
276 } |
|
277 } |
|
278 } |