1: <?php
2: /**
3: * Smarty Resource Plugin
4: *
5: * @package Smarty
6: * @subpackage TemplateResources
7: * @author Rodney Rehm
8: */
9:
10: /**
11: * Smarty Resource Plugin
12: * Wrapper Implementation for custom resource plugins
13: *
14: * @package Smarty
15: * @subpackage TemplateResources
16: */
17: abstract class Smarty_Resource_Custom extends Smarty_Resource
18: {
19: /**
20: * fetch template and its modification time from data source
21: *
22: * @param string $name template name
23: * @param string &$source template source
24: * @param integer &$mtime template modification timestamp (epoch)
25: */
26: abstract protected function fetch($name, &$source, &$mtime);
27:
28: /**
29: * Fetch template's modification timestamp from data source
30: * {@internal implementing this method is optional.
31: * Only implement it if modification times can be accessed faster than loading the complete template source.}}
32: *
33: * @param string $name template name
34: *
35: * @return integer|boolean timestamp (epoch) the template was modified, or false if not found
36: */
37: protected function fetchTimestamp($name)
38: {
39: return null;
40: }
41:
42: /**
43: * populate Source Object with meta data from Resource
44: *
45: * @param Smarty_Template_Source $source source object
46: * @param Smarty_Internal_Template $_template template object
47: */
48: public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
49: {
50: $source->filepath = $source->type . ':' . $source->name;
51: $source->uid = sha1($source->type . ':' . $source->name);
52:
53: $mtime = $this->fetchTimestamp($source->name);
54: if ($mtime !== null) {
55: $source->timestamp = $mtime;
56: } else {
57: $this->fetch($source->name, $content, $timestamp);
58: $source->timestamp = isset($timestamp) ? $timestamp : false;
59: if (isset($content)) {
60: $source->content = $content;
61: }
62: }
63: $source->exists = !!$source->timestamp;
64: }
65:
66: /**
67: * Load template's source into current template object
68: *
69: * @param Smarty_Template_Source $source source object
70: *
71: * @return string template source
72: * @throws SmartyException if source cannot be loaded
73: */
74: public function getContent(Smarty_Template_Source $source)
75: {
76: $this->fetch($source->name, $content, $timestamp);
77: if (isset($content)) {
78: return $content;
79: }
80:
81: throw new SmartyException("Unable to read template {$source->type} '{$source->name}'");
82: }
83:
84: /**
85: * Determine basename for compiled filename
86: *
87: * @param Smarty_Template_Source $source source object
88: *
89: * @return string resource's basename
90: */
91: protected function getBasename(Smarty_Template_Source $source)
92: {
93: return basename($source->name);
94: }
95: }
96: