Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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