blob: 6d22222374d07ea6a98c2ae5afa9bef0ad2bc99d (
plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
<?php
namespace Smarty\Resource;
use Smarty\Exception;
use Smarty\Smarty;
use Smarty\Template;
use Smarty\Template\Source;
/**
* Smarty Resource Plugin
* Base implementation for resource plugins
* @author Rodney Rehm
*/
abstract class BasePlugin
{
/**
* resource types provided by the core
*
* @var array
*/
public static $sysplugins = [
'file' => FilePlugin::class,
'string' => StringPlugin::class,
'extends' => ExtendsPlugin::class,
'stream' => StreamPlugin::class,
'eval' => StringEval::class,
];
/**
* Source must be recompiled on every occasion
*
* @var boolean
*/
public $recompiled = false;
/**
* Flag if resource does allow compilation
*
* @return bool
*/
public function supportsCompiledTemplates(): bool {
return true;
}
/**
* Check if resource must check time stamps when loading compiled or cached templates.
* Resources like 'extends' which use source components my disable timestamp checks on own resource.
* @return bool
*/
public function checkTimestamps()
{
return true;
}
/**
* Load Resource Handler
*
* @param Smarty $smarty smarty object
* @param string $type name of the resource
*
* @return BasePlugin Resource Handler
* @throws Exception
*/
public static function load(Smarty $smarty, $type)
{
// try smarty's cache
if (isset($smarty->_resource_handlers[ $type ])) {
return $smarty->_resource_handlers[ $type ];
}
// try registered resource
if (isset($smarty->registered_resources[ $type ])) {
return $smarty->_resource_handlers[ $type ] = $smarty->registered_resources[ $type ];
}
// try sysplugins dir
if (isset(self::$sysplugins[ $type ])) {
$_resource_class = self::$sysplugins[ $type ];
return $smarty->_resource_handlers[ $type ] = new $_resource_class();
}
// try plugins dir
$_resource_class = 'Smarty_Resource_' . \smarty_ucfirst_ascii($type);
if (class_exists($_resource_class, false)) {
return $smarty->_resource_handlers[ $type ] = new $_resource_class();
}
// try streams
$_known_stream = stream_get_wrappers();
if (in_array($type, $_known_stream)) {
// is known stream
if (is_object($smarty->security_policy)) {
$smarty->security_policy->isTrustedStream($type);
}
return $smarty->_resource_handlers[ $type ] = new StreamPlugin();
}
// TODO: try default_(template|config)_handler
// give up
throw new \Smarty\Exception("Unknown resource type '{$type}'");
}
/**
* Load template's source into current template object
*
* @param Source $source source object
*
* @return string template source
* @throws \Smarty\Exception if source cannot be loaded
*/
abstract public function getContent(Source $source);
/**
* populate Source Object with metadata from Resource
*
* @param Source $source source object
* @param Template|null $_template template object
*/
abstract public function populate(Source $source, ?\Smarty\Template $_template = null);
/**
* populate Source Object with timestamp and exists from Resource
*
* @param Source $source source object
*/
public function populateTimestamp(Source $source)
{
// intentionally left blank
}
/*
* Check if resource must check time stamps when when loading complied or cached templates.
* Resources like 'extends' which use source components my disable timestamp checks on own resource.
*
* @return bool
*/
/**
* Determine basename for compiled filename
*
* @param \Smarty\Template\Source $source source object
*
* @return string resource's basename
*/
public function getBasename(\Smarty\Template\Source $source)
{
return basename(preg_replace('![^\w]+!', '_', $source->name));
}
}
|