Completed
Pull Request — master (#45)
by yuuki
01:42
created

SmartyFactory::getVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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-2016 Yuuki Takezawa
16
 *
17
 */
18
19
namespace Ytake\LaravelSmarty;
20
21
use ReflectionClass;
22
use Illuminate\View\Factory;
23
use Illuminate\View\ViewFinderInterface;
24
use Illuminate\View\Engines\EngineResolver;
25
use Ytake\LaravelSmarty\Cache\Storage;
26
use Ytake\LaravelSmarty\Engines\SmartyTemplate;
27
use Ytake\LaravelSmarty\Exception\MethodNotFoundException;
28
use Illuminate\Contracts\Config\Repository as ConfigContract;
29
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
30
31
/**
32
 * Class SmartyManager
33
 *
34
 * @author  yuuki.takezawa<[email protected]>
35
 * @license http://opensource.org/licenses/MIT MIT
36
 */
37
class SmartyFactory extends Factory
38
{
39
    /**
40
     * @var string  version
41
     */
42
    const VERSION = '2.1.12';
43
44
    /** @var Smarty $smarty */
45
    protected $smarty;
46
47
    /** @var ConfigContract $config */
48
    protected $config;
49
50
    /** @var array valid config keys */
51
    protected $configKeys = [
52
        'auto_literal',
53
        'error_unassigned',
54
        'use_include_path',
55
        'joined_template_dir',
56
        'joined_config_dir',
57
        'default_template_handler_func',
58
        'default_config_handler_func',
59
        'default_plugin_handler_func',
60
        'force_compile',
61
        'compile_check',
62
        'use_sub_dirs',
63
        'allow_ambiguous_resources',
64
        'merge_compiled_includes',
65
        'inheritance_merge_compiled_includes',
66
        'force_cache',
67
        'left_delimiter',
68
        'right_delimiter',
69
        'security_class',
70
        'php_handling',
71
        'allow_php_templates',
72
        'direct_access_security',
73
        'debugging',
74
        'debugging_ctrl',
75
        'smarty_debug_id',
76
        'debug_tpl',
77
//      'error_reporting', added below with default value
78
        'get_used_tags',
79
        'config_overwrite',
80
        'config_booleanize',
81
        'config_read_hidden',
82
        'compile_locking',
83
        'cache_locking',
84
        'locking_timeout',
85
        'default_resource_type',
86
        'caching_type',
87
        'properties',
88
        'default_config_type',
89
        'source_objects',
90
        'template_objects',
91
        'resource_caching',
92
        'template_resource_caching',
93
        'cache_modified_check',
94
        'registered_plugins',
95
        'plugin_search_order',
96
        'registered_objects',
97
        'registered_classes',
98
        'registered_filters',
99
        'registered_resources',
100
        '_resource_handlers',
101
        'registered_cache_resources',
102
        '_cacheresource_handlers',
103
        'autoload_filters',
104
        'default_modifiers',
105
        'escape_html',
106
        'start_time',
107
        '_file_perms',
108
        '_dir_perms',
109
        '_tag_stack',
110
        '_current_file',
111
        '_parserdebug',
112
        '_is_file_cache',
113
        'cache_id',
114
        'compile_id',
115
        'caching',
116
        'cache_lifetime',
117
        'template_class',
118
        'tpl_vars',
119
        'parent',
120
        'config_vars',
121
    ];
122
123
    /** @var array valid security policy config keys */
124
    protected $securityPolicyConfigKeys = [
125
        'php_handling',
126
        'secure_dir',
127
        'trusted_dir',
128
        'trusted_uri',
129
        'trusted_constants',
130
        'static_classes',
131
        'trusted_static_methods',
132
        'trusted_static_properties',
133
        'php_functions',
134
        'php_modifiers',
135
        'allowed_tags',
136
        'disabled_tags',
137
        'allowed_modifiers',
138
        'disabled_modifiers',
139
        'disabled_special_smarty_vars',
140
        'streams',
141
        'allow_constants',
142
        'allow_super_globals',
143
        'max_template_nesting',
144
    ];
145
146
    /** @var string  smarty template file extension */
147
    protected $smartyFileExtension;
148
149
    /**
150
     * @param EngineResolver      $engines
151
     * @param ViewFinderInterface $finder
152
     * @param DispatcherContract  $events
153
     * @param Smarty              $smarty
154
     * @param ConfigContract      $config
155
     */
156
    public function __construct(
157
        EngineResolver $engines,
158
        ViewFinderInterface $finder,
159
        DispatcherContract $events,
160
        Smarty $smarty,
161
        ConfigContract $config
162
    ) {
163
        parent::__construct($engines, $finder, $events);
164
        $this->smarty = $smarty;
165
        $this->config = $config;
166
    }
167
168
    /**
169
     * @return Smarty
170
     */
171
    public function getSmarty()
172
    {
173
        return $this->smarty;
174
    }
175
176
    /**
177
     * @return string
178
     */
179
    public function getVersion()
180
    {
181
        return self::VERSION;
182
    }
183
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
201
        $smarty = $this->smarty;
202
203
        $smarty->setTemplateDir(array_get($config, 'template_path'));
204
        $smarty->setCompileDir(array_get($config, 'compile_path'));
205
        $smarty->setCacheDir(array_get($config, 'cache_path'));
206
        $smarty->setConfigDir(array_get($config, 'config_paths'));
207
208
        foreach (array_get($config, 'plugins_paths', []) as $plugins) {
209
            $smarty->addPluginsDir($plugins);
210
        }
211
212
        $smarty->error_reporting = array_get($config, 'error_reporting', E_ALL & ~E_NOTICE);
213
        // SmartyTemplate class for laravel
214
        $smarty->template_class = SmartyTemplate::class;
215
        foreach ($config as $key => $value) {
216
            if (in_array($key, $this->configKeys)) {
217
                $smarty->{$key} = $value;
218
            }
219
        }
220
221
        if (array_get($config, 'enable_security')) {
222
            $smarty->enableSecurity();
223
            $securityPolicy = $smarty->security_policy;
224
            $securityConfig = array_get($config, 'security_policy', []);
225
            foreach ($securityConfig as $key => $value) {
226
                if (in_array($key, $this->securityPolicyConfigKeys)) {
227
                    $securityPolicy->{$key} = $value;
228
                }
229
            }
230
        }
231
        $this->smartyFileExtension = $config['extension'];
232
    }
233
234
    /**
235
     * @return string
236
     */
237
    public function getSmartyFileExtension()
238
    {
239
        return $this->smartyFileExtension;
240
    }
241
242
    /**
243
     * @param $name
244
     * @param $arguments
245
     *
246
     * @throws MethodNotFoundException
247
     * @return mixed
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