2809 |
rexy |
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 . ':' . substr(preg_replace('/[^A-Za-z0-9.]/', '', $source->name), 0, 25);
|
|
|
51 |
$source->uid = sha1($source->type . ':' . $source->name);
|
|
|
52 |
$mtime = $this->fetchTimestamp($source->name);
|
|
|
53 |
if ($mtime !== null) {
|
|
|
54 |
$source->timestamp = $mtime;
|
|
|
55 |
} else {
|
|
|
56 |
$this->fetch($source->name, $content, $timestamp);
|
|
|
57 |
$source->timestamp = isset($timestamp) ? $timestamp : false;
|
|
|
58 |
if (isset($content)) {
|
|
|
59 |
$source->content = $content;
|
|
|
60 |
}
|
|
|
61 |
}
|
|
|
62 |
$source->exists = !!$source->timestamp;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Load template's source into current template object
|
|
|
67 |
*
|
|
|
68 |
* @param Smarty_Template_Source $source source object
|
|
|
69 |
*
|
|
|
70 |
* @return string template source
|
|
|
71 |
* @throws SmartyException if source cannot be loaded
|
|
|
72 |
*/
|
|
|
73 |
public function getContent(Smarty_Template_Source $source)
|
|
|
74 |
{
|
|
|
75 |
$this->fetch($source->name, $content, $timestamp);
|
|
|
76 |
if (isset($content)) {
|
|
|
77 |
return $content;
|
|
|
78 |
}
|
|
|
79 |
throw new SmartyException("Unable to read template {$source->type} '{$source->name}'");
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Determine basename for compiled filename
|
|
|
84 |
*
|
|
|
85 |
* @param Smarty_Template_Source $source source object
|
|
|
86 |
*
|
|
|
87 |
* @return string resource's basename
|
|
|
88 |
*/
|
|
|
89 |
public function getBasename(Smarty_Template_Source $source)
|
|
|
90 |
{
|
|
|
91 |
return basename(substr(preg_replace('/[^A-Za-z0-9.]/', '', $source->name), 0, 25));
|
|
|
92 |
}
|
|
|
93 |
}
|