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