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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
<?php
namespace Smarty\Extension;
use Smarty\BlockHandler\BlockPluginWrapper;
use Smarty\Compile\CompilerInterface;
use Smarty\Compile\Modifier\BCPluginWrapper as ModifierCompilerPluginWrapper;
use Smarty\Compile\Tag\BCPluginWrapper as TagPluginWrapper;
use Smarty\Filter\FilterPluginWrapper;
use Smarty\FunctionHandler\BCPluginWrapper as FunctionPluginWrapper;
class BCPluginsAdapter extends Base {
/**
* @var \Smarty\Smarty
*/
private $smarty;
public function __construct(\Smarty\Smarty $smarty) {
$this->smarty = $smarty;
}
private function findPlugin($type, $name): ?array {
if (null !== $plugin = $this->smarty->getRegisteredPlugin($type, $name)) {
return $plugin;
}
return null;
}
public function getTagCompiler(string $tag): ?\Smarty\Compile\CompilerInterface {
$plugin = $this->findPlugin(\Smarty\Smarty::PLUGIN_COMPILER, $tag);
if ($plugin === null) {
return null;
}
if (is_callable($plugin[0])) {
$callback = $plugin[0];
$cacheable = (bool) $plugin[1] ?? true;
return new TagPluginWrapper($callback, $cacheable);
} elseif (class_exists($plugin[0])) {
$compiler = new $plugin[0];
if ($compiler instanceof CompilerInterface) {
return $compiler;
}
}
return null;
}
public function getFunctionHandler(string $functionName): ?\Smarty\FunctionHandler\FunctionHandlerInterface {
$plugin = $this->findPlugin(\Smarty\Smarty::PLUGIN_FUNCTION, $functionName);
if ($plugin === null) {
return null;
}
$callback = $plugin[0];
$cacheable = (bool) $plugin[1] ?? true;
return new FunctionPluginWrapper($callback, $cacheable);
}
public function getBlockHandler(string $blockTagName): ?\Smarty\BlockHandler\BlockHandlerInterface {
$plugin = $this->findPlugin(\Smarty\Smarty::PLUGIN_BLOCK, $blockTagName);
if ($plugin === null) {
return null;
}
$callback = $plugin[0];
$cacheable = (bool) $plugin[1] ?? true;
return new BlockPluginWrapper($callback, $cacheable);
}
public function getModifierCallback(string $modifierName) {
$plugin = $this->findPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, $modifierName);
if ($plugin === null) {
return null;
}
return $plugin[0];
}
public function getModifierCompiler(string $modifier): ?\Smarty\Compile\Modifier\ModifierCompilerInterface {
$plugin = $this->findPlugin(\Smarty\Smarty::PLUGIN_MODIFIERCOMPILER, $modifier);
if ($plugin === null) {
return null;
}
$callback = $plugin[0];
return new ModifierCompilerPluginWrapper($callback);
}
/**
* @var array
*/
private $preFilters = [];
public function getPreFilters(): array {
return $this->preFilters;
}
public function addPreFilter(\Smarty\Filter\FilterInterface $filter) {
$this->preFilters[] = $filter;
}
public function addCallableAsPreFilter(callable $callable, ?string $name = null) {
if ($name === null) {
$this->preFilters[] = new FilterPluginWrapper($callable);
} else {
$this->preFilters[$name] = new FilterPluginWrapper($callable);
}
}
public function removePrefilter(string $name) {
unset($this->preFilters[$name]);
}
/**
* @var array
*/
private $postFilters = [];
public function getPostFilters(): array {
return $this->postFilters;
}
public function addPostFilter(\Smarty\Filter\FilterInterface $filter) {
$this->postFilters[] = $filter;
}
public function addCallableAsPostFilter(callable $callable, ?string $name = null) {
if ($name === null) {
$this->postFilters[] = new FilterPluginWrapper($callable);
} else {
$this->postFilters[$name] = new FilterPluginWrapper($callable);
}
}
public function removePostFilter(string $name) {
unset($this->postFilters[$name]);
}
/**
* @var array
*/
private $outputFilters = [];
public function getOutputFilters(): array {
return $this->outputFilters;
}
public function addOutputFilter(\Smarty\Filter\FilterInterface $filter) {
$this->outputFilters[] = $filter;
}
public function addCallableAsOutputFilter(callable $callable, ?string $name = null) {
if ($name === null) {
$this->outputFilters[] = new FilterPluginWrapper($callable);
} else {
$this->outputFilters[$name] = new FilterPluginWrapper($callable);
}
}
public function removeOutputFilter(string $name) {
unset($this->outputFilters[$name]);
}
public function loadPluginsFromDir(string $path) {
foreach([
'function',
'modifier',
'block',
'compiler',
'prefilter',
'postfilter',
'outputfilter',
'modifiercompiler',
] as $type) {
foreach (glob($path . $type . '.?*.php') as $filename) {
$pluginName = $this->getPluginNameFromFilename($filename);
if ($pluginName !== null) {
require_once $filename;
$functionOrClassName = 'smarty_' . $type . '_' . $pluginName;
if (function_exists($functionOrClassName) || class_exists($functionOrClassName)) {
$this->smarty->registerPlugin($type, $pluginName, $functionOrClassName, true, []);
}
}
}
}
$type = 'resource';
foreach (glob($path . $type . '.?*.php') as $filename) {
$pluginName = $this->getPluginNameFromFilename($filename);
if ($pluginName !== null) {
require_once $filename;
if (class_exists($className = 'smarty_' . $type . '_' . $pluginName)) {
$this->smarty->registerResource($pluginName, new $className());
}
}
}
$type = 'cacheresource';
foreach (glob($path . $type . '.?*.php') as $filename) {
$pluginName = $this->getPluginNameFromFilename($filename);
if ($pluginName !== null) {
require_once $filename;
if (class_exists($className = 'smarty_' . $type . '_' . $pluginName)) {
$this->smarty->registerCacheResource($pluginName, new $className());
}
}
}
}
/**
* @param $filename
*
* @return string|null
*/
private function getPluginNameFromFilename($filename) {
if (!preg_match('/.*\.([a-z_A-Z0-9]+)\.php$/',$filename,$matches)) {
return null;
}
return $matches[1];
}
}
|