|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
6
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
7
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
8
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
9
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
10
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
11
|
|
|
* THE SOFTWARE. |
|
12
|
|
|
* |
|
13
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
14
|
|
|
* and is licensed under the MIT license. |
|
15
|
|
|
* |
|
16
|
|
|
* Copyright (c) 2014-2019 Yuuki Takezawa |
|
17
|
|
|
* |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Ytake\LaravelSmarty; |
|
21
|
|
|
|
|
22
|
|
|
use Illuminate\Contracts\Config\Repository as ConfigContract; |
|
23
|
|
|
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract; |
|
24
|
|
|
use Illuminate\View\Engines\EngineResolver; |
|
25
|
|
|
use Illuminate\View\Factory; |
|
26
|
|
|
use Illuminate\View\ViewFinderInterface; |
|
27
|
|
|
use Illuminate\Support\Arr; |
|
28
|
|
|
use ReflectionClass; |
|
29
|
|
|
use Ytake\LaravelSmarty\Cache\Storage; |
|
30
|
|
|
use Ytake\LaravelSmarty\Engines\SmartyTemplate; |
|
31
|
|
|
use Ytake\LaravelSmarty\Exception\MethodNotFoundException; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Class SmartyManager |
|
35
|
|
|
* |
|
36
|
|
|
* @author yuuki.takezawa<[email protected]> |
|
37
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
38
|
|
|
*/ |
|
39
|
|
|
class SmartyFactory extends Factory |
|
40
|
|
|
{ |
|
41
|
|
|
/** |
|
42
|
|
|
* @var string version |
|
43
|
|
|
*/ |
|
44
|
|
|
const VERSION = '2.6.0'; |
|
45
|
|
|
|
|
46
|
|
|
/** @var Smarty $smarty */ |
|
47
|
|
|
protected $smarty; |
|
48
|
|
|
|
|
49
|
|
|
/** @var ConfigContract $config */ |
|
50
|
|
|
protected $config; |
|
51
|
|
|
|
|
52
|
|
|
/** @var array valid config keys */ |
|
53
|
|
|
protected $configKeys = [ |
|
54
|
|
|
'auto_literal', |
|
55
|
|
|
'error_unassigned', |
|
56
|
|
|
'use_include_path', |
|
57
|
|
|
'joined_template_dir', |
|
58
|
|
|
'joined_config_dir', |
|
59
|
|
|
'default_template_handler_func', |
|
60
|
|
|
'default_config_handler_func', |
|
61
|
|
|
'default_plugin_handler_func', |
|
62
|
|
|
'force_compile', |
|
63
|
|
|
'compile_check', |
|
64
|
|
|
'use_sub_dirs', |
|
65
|
|
|
'allow_ambiguous_resources', |
|
66
|
|
|
'merge_compiled_includes', |
|
67
|
|
|
'inheritance_merge_compiled_includes', |
|
68
|
|
|
'force_cache', |
|
69
|
|
|
'left_delimiter', |
|
70
|
|
|
'right_delimiter', |
|
71
|
|
|
'security_class', |
|
72
|
|
|
'php_handling', |
|
73
|
|
|
'allow_php_templates', |
|
74
|
|
|
'direct_access_security', |
|
75
|
|
|
'debugging', |
|
76
|
|
|
'debugging_ctrl', |
|
77
|
|
|
'smarty_debug_id', |
|
78
|
|
|
'debug_tpl', |
|
79
|
|
|
// 'error_reporting', added below with default value |
|
80
|
|
|
'get_used_tags', |
|
81
|
|
|
'config_overwrite', |
|
82
|
|
|
'config_booleanize', |
|
83
|
|
|
'config_read_hidden', |
|
84
|
|
|
'compile_locking', |
|
85
|
|
|
'cache_locking', |
|
86
|
|
|
'locking_timeout', |
|
87
|
|
|
'default_resource_type', |
|
88
|
|
|
'caching_type', |
|
89
|
|
|
'properties', |
|
90
|
|
|
'default_config_type', |
|
91
|
|
|
'source_objects', |
|
92
|
|
|
'template_objects', |
|
93
|
|
|
'resource_caching', |
|
94
|
|
|
'template_resource_caching', |
|
95
|
|
|
'cache_modified_check', |
|
96
|
|
|
'registered_plugins', |
|
97
|
|
|
'plugin_search_order', |
|
98
|
|
|
'registered_objects', |
|
99
|
|
|
'registered_classes', |
|
100
|
|
|
'registered_filters', |
|
101
|
|
|
'registered_resources', |
|
102
|
|
|
'_resource_handlers', |
|
103
|
|
|
'registered_cache_resources', |
|
104
|
|
|
'_cacheresource_handlers', |
|
105
|
|
|
'autoload_filters', |
|
106
|
|
|
'default_modifiers', |
|
107
|
|
|
'escape_html', |
|
108
|
|
|
'start_time', |
|
109
|
|
|
'_file_perms', |
|
110
|
|
|
'_dir_perms', |
|
111
|
|
|
'_tag_stack', |
|
112
|
|
|
'_current_file', |
|
113
|
|
|
'_parserdebug', |
|
114
|
|
|
'_is_file_cache', |
|
115
|
|
|
'cache_id', |
|
116
|
|
|
'compile_id', |
|
117
|
|
|
'caching', |
|
118
|
|
|
'cache_lifetime', |
|
119
|
|
|
'template_class', |
|
120
|
|
|
'tpl_vars', |
|
121
|
|
|
'parent', |
|
122
|
|
|
'config_vars', |
|
123
|
|
|
]; |
|
124
|
|
|
|
|
125
|
|
|
/** @var array valid security policy config keys */ |
|
126
|
|
|
protected $securityPolicyConfigKeys = [ |
|
127
|
|
|
'php_handling', |
|
128
|
|
|
'secure_dir', |
|
129
|
|
|
'trusted_dir', |
|
130
|
|
|
'trusted_uri', |
|
131
|
|
|
'trusted_constants', |
|
132
|
|
|
'static_classes', |
|
133
|
|
|
'trusted_static_methods', |
|
134
|
|
|
'trusted_static_properties', |
|
135
|
|
|
'php_functions', |
|
136
|
|
|
'php_modifiers', |
|
137
|
|
|
'allowed_tags', |
|
138
|
|
|
'disabled_tags', |
|
139
|
|
|
'allowed_modifiers', |
|
140
|
|
|
'disabled_modifiers', |
|
141
|
|
|
'disabled_special_smarty_vars', |
|
142
|
|
|
'streams', |
|
143
|
|
|
'allow_constants', |
|
144
|
|
|
'allow_super_globals', |
|
145
|
|
|
'max_template_nesting', |
|
146
|
|
|
]; |
|
147
|
|
|
|
|
148
|
|
|
/** @var string smarty template file extension */ |
|
149
|
|
|
protected $smartyFileExtension; |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param EngineResolver $engines |
|
153
|
|
|
* @param ViewFinderInterface $finder |
|
154
|
|
|
* @param DispatcherContract $events |
|
155
|
|
|
* @param Smarty $smarty |
|
156
|
|
|
* @param ConfigContract $config |
|
157
|
|
|
*/ |
|
158
|
|
|
public function __construct( |
|
159
|
|
|
EngineResolver $engines, |
|
160
|
|
|
ViewFinderInterface $finder, |
|
161
|
|
|
DispatcherContract $events, |
|
162
|
|
|
Smarty $smarty, |
|
163
|
|
|
ConfigContract $config |
|
164
|
|
|
) { |
|
165
|
|
|
parent::__construct($engines, $finder, $events); |
|
166
|
|
|
$this->smarty = $smarty; |
|
167
|
|
|
$this->config = $config; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @return Smarty |
|
172
|
|
|
*/ |
|
173
|
|
|
public function getSmarty(): Smarty |
|
174
|
|
|
{ |
|
175
|
|
|
return $this->smarty; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @return string |
|
180
|
|
|
*/ |
|
181
|
|
|
public function getVersion(): string |
|
182
|
|
|
{ |
|
183
|
|
|
return self::VERSION; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function resolveSmartyCache() |
|
187
|
|
|
{ |
|
188
|
|
|
$cacheStorage = new Storage($this->getSmarty(), $this->config); |
|
189
|
|
|
$cacheStorage->cacheStorageManaged(); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* smarty configure |
|
194
|
|
|
* |
|
195
|
|
|
* @throws \SmartyException |
|
196
|
|
|
*/ |
|
197
|
|
|
public function setSmartyConfigure() |
|
198
|
|
|
{ |
|
199
|
|
|
$config = $this->config->get('ytake-laravel-smarty'); |
|
200
|
|
|
$smarty = $this->smarty; |
|
201
|
|
|
|
|
202
|
|
|
$smarty->setTemplateDir(Arr::get($config, 'template_path')); |
|
203
|
|
|
$smarty->setCompileDir(Arr::get($config, 'compile_path')); |
|
204
|
|
|
$smarty->setCacheDir(Arr::get($config, 'cache_path')); |
|
205
|
|
|
$smarty->setConfigDir(Arr::get($config, 'config_paths')); |
|
206
|
|
|
|
|
207
|
|
|
foreach (Arr::get($config, 'plugins_paths', []) as $plugins) { |
|
208
|
|
|
$smarty->addPluginsDir($plugins); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
$smarty->error_reporting = Arr::get($config, 'error_reporting', E_ALL & ~E_NOTICE); |
|
212
|
|
|
// SmartyTemplate class for laravel |
|
213
|
|
|
$smarty->template_class = SmartyTemplate::class; |
|
214
|
|
|
foreach ($config as $key => $value) { |
|
215
|
|
|
if (in_array($key, $this->configKeys)) { |
|
216
|
|
|
$smarty->{$key} = $value; |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
if (Arr::get($config, 'enable_security')) { |
|
221
|
|
|
$smarty->enableSecurity(); |
|
222
|
|
|
$securityPolicy = $smarty->security_policy; |
|
223
|
|
|
$securityConfig = Arr::get($config, 'security_policy', []); |
|
224
|
|
|
foreach ($securityConfig as $key => $value) { |
|
225
|
|
|
if (in_array($key, $this->securityPolicyConfigKeys)) { |
|
226
|
|
|
$securityPolicy->{$key} = $value; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
$this->smartyFileExtension = $config['extension']; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @return string |
|
235
|
|
|
*/ |
|
236
|
|
|
public function getSmartyFileExtension(): string |
|
237
|
|
|
{ |
|
238
|
|
|
return $this->smartyFileExtension; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @param $name |
|
243
|
|
|
* @param $arguments |
|
244
|
|
|
* |
|
245
|
|
|
* @return mixed |
|
246
|
|
|
* @throws \ReflectionException |
|
247
|
|
|
* @throws MethodNotFoundException |
|
248
|
|
|
*/ |
|
249
|
|
|
public function __call($name, $arguments) |
|
250
|
|
|
{ |
|
251
|
|
|
$reflectionClass = new ReflectionClass($this->smarty); |
|
252
|
|
|
if (!$reflectionClass->hasMethod($name)) { |
|
253
|
|
|
throw new MethodNotFoundException("{$name} : Method Not Found"); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
return call_user_func_array([$this->smarty, $name], $arguments); |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
|