Completed
Push — master ( 960e79...53cd45 )
by Michael
48:22 queued 33:24
created

XoopsTpl   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Test Coverage

Coverage 67.19%

Importance

Changes 0
Metric Value
eloc 60
dl 0
loc 163
ccs 43
cts 64
cp 0.6719
rs 10
c 0
b 0
f 0
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A touch() 0 8 1
A setCompileId() 0 9 4
A convertLegacyDelimiters() 0 9 3
A clearModuleCompileCache() 0 20 5
A clearCache() 0 9 1
A __construct() 0 28 1
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
namespace Xoops\Core;
13
14
/**
15
 * XOOPS template engine class
16
 *
17
 * @category  Xoops\Core
18
 * @package   Template
19
 * @author    Kazumi Ono <[email protected]>
20
 * @author    Skalpa Keo <[email protected]>
21
 * @author    Taiwen Jiang <[email protected]>
22
 * @copyright 2000-2019 XOOPS Project (https://xoops.org)
23
 * @license   GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
24
s */
25
class XoopsTpl extends \Smarty
26
{
27
    use SmartyBCTrait;
28
29
    /**
30
     * @var \Xoops\Core\Theme\XoopsTheme
31
     */
32
    public $currentTheme = null;
33
34
    /**
35
     * XoopsTpl constructor
36
     */
37 13
    public function __construct()
38
    {
39 13
        parent::__construct(); // SMARTY_PLUGINS_DIR is initialized into parent
40 13
        $xoops = \Xoops::getInstance();
41 13
        $xoops->events()->triggerEvent('core.template.construct.start', array($this));
42
43 13
        $this->registerFilter(
44 13
            'pre',
45 13
            [$this, 'convertLegacyDelimiters']
46
        );
47
48
        //$this->left_delimiter = '<{';
49
        //$this->right_delimiter = '}>';
50
51 13
        $this->setTemplateDir(\XoopsBaseConfig::get('themes-path'));
52 13
        $this->setCacheDir(\XoopsBaseConfig::get('smarty-cache'));
53 13
        $this->setCompileDir(\XoopsBaseConfig::get('smarty-compile'));
54 13
        $this->compile_check = ($xoops->getConfig('theme_fromfile') == 1);
0 ignored issues
show
Documentation Bug introduced by
The property $compile_check was declared of type integer, but $xoops->getConfig('theme_fromfile') == 1 is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
55 13
        $this->setPluginsDir(\XoopsBaseConfig::get('smarty-xoops-plugins'));
56 13
        $this->addPluginsDir(SMARTY_PLUGINS_DIR);
57 13
        $this->setCompileId();
58 13
        $this->assign(
59 13
            array('xoops_url' => \XoopsBaseConfig::get('url'),
60 13
                'xoops_rootpath' => \XoopsBaseConfig::get('root-path'),
61 13
                'xoops_langcode' => \XoopsLocale::getLangCode(),
62 13
                'xoops_charset' => \XoopsLocale::getCharset(),
63
                'xoops_version' => \Xoops::VERSION,
64 13
                'xoops_upload_url' => \XoopsBaseConfig::get('uploads-url'))
65
        );
66 13
    }
67
68
    /**
69
     * XOOPS legacy used '<{' and '}>' as delimiters rather than using the default '{' and '}'.
70
     * This prefilter function converts any legacy delimiters to Smarty default delimiters.
71
     *
72
     * The intention is to phase out the legacy delimiters entirely.
73
     *
74
     * @param string                    $tpl_source template source
75
     * @param \Smarty_Internal_Template $template   template object
76
     *
77
     * @return string source with any legacy delimiters converted to standard default delimiters
78
     */
79 5
    public function convertLegacyDelimiters($tpl_source, \Smarty_Internal_Template $template)
0 ignored issues
show
Unused Code introduced by
The parameter $template is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

79
    public function convertLegacyDelimiters($tpl_source, /** @scrutinizer ignore-unused */ \Smarty_Internal_Template $template)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
    {
81 5
        $countLeft = 0;
82 5
        $countRight = -1;
83 5
        $temp = str_replace('<{', '{', $tpl_source, $countLeft);
84 5
        if ($countLeft>0) {
85 1
            $temp = str_replace('}>', '}', $temp, $countRight);
86
        }
87 5
        return ($countLeft === $countRight) ? $temp : $tpl_source;
88
    }
89
90
    /**
91
     * XoopsTpl::touch
92
     *
93
     * @param string $resourceName name of resource
94
     *
95
     * @return bool
96
     */
97 1
    public function touch($resourceName)
98
    {
99 1
        $isForced = $this->force_compile;
100 1
        $this->force_compile = true;
101 1
        parent::clearCache($resourceName);
102 1
        $result = true;
103 1
        $this->force_compile = $isForced;
104 1
        return $result;
105
    }
106
107
    /**
108
     * XoopsTpl::setCompileId()
109
     *
110
     * @param mixed $module_dirname module directory
111
     * @param mixed $theme_set      theme set
112
     * @param mixed $template_set   template set
113
     *
114
     * @return void
115
     */
116 13
    public function setCompileId($module_dirname = null, $theme_set = null, $template_set = null)
117
    {
118 13
        $xoops = \Xoops::getInstance();
119
120 13
        $template_set = empty($template_set) ? $xoops->getConfig('template_set') : $template_set;
121 13
        $theme_set = empty($theme_set) ? $xoops->getConfig('theme_set') : $theme_set;
122 13
        $module_dirname = empty($module_dirname) ? $xoops->moduleDirname : $module_dirname;
123 13
        $this->compile_id = substr(md5(\XoopsBaseConfig::get('url')), 0, 8) . '-' . $module_dirname
0 ignored issues
show
Bug introduced by
Are you sure $module_dirname of type array|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
        $this->compile_id = substr(md5(\XoopsBaseConfig::get('url')), 0, 8) . '-' . /** @scrutinizer ignore-type */ $module_dirname
Loading history...
124 13
            . '-' . $theme_set . '-' . $template_set;
125
        //$this->_compile_id = $this->compile_id;
126 13
    }
127
128
    /**
129
     * XoopsTpl::clearModuleCompileCache()
130
     *
131
     * Clean up compiled and cached templates for a module
132
     *
133
     * TODO - handle $use_sub_dirs cases
134
     *
135
     * @param mixed $module_dirname module directory
136
     * @param mixed $theme_set      theme set
137
     * @param mixed $template_set   template set
138
     *
139
     * @return int number of deleted cache and compiler files
140
     */
141
    public function clearModuleCompileCache($module_dirname = null, $theme_set = null, $template_set = null)
0 ignored issues
show
Unused Code introduced by
The parameter $template_set is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

141
    public function clearModuleCompileCache($module_dirname = null, $theme_set = null, /** @scrutinizer ignore-unused */ $template_set = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $theme_set is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

141
    public function clearModuleCompileCache($module_dirname = null, /** @scrutinizer ignore-unused */ $theme_set = null, $template_set = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
142
    {
143
        $hold_compile_id = $this->compile_id;
144
        // $this->setCompileId($module_dirname, $template_set, $theme_set);
145
        // TODO - should handle $use_sub_dirs
146
        $this->setCompileId($module_dirname, '*', '*');
147
        $compile_id = $this->compile_id;
148
        $this->compile_id = $hold_compile_id;
149
        $compile_id = preg_replace('![^\w\|]+!', '_', $compile_id);
150
        $glob = $compile_id . '*.php';
151
        $count=0;
152
        $files = glob($this->getCompileDir() . '/' . $glob);
153
        foreach ($files as $filename) {
154
            $count += unlink($filename) ? 1 : 0;
155
        }
156
        $files = glob($this->getCacheDir() . '/*' . $glob);
157
        foreach ($files as $filename) {
158
            $count += unlink($filename) ? 1 : 0;
159
        }
160
        return $count;
161
    }
162
163
    /**
164
     * Empty cache for a specific template
165
     *
166
     * This is just a pass-through wrapper with a warning since this method previously existed
167
     * only in XoopsTpl, but now is also a regular Smarty method.
168
     *
169
     * clearModuleCompileCache() is the replacement for the old clearCache
170
     *
171
     * @param  string  $template_name template name
172
     * @param  string  $cache_id      cache id
173
     * @param  string  $compile_id    compile id
174
     * @param  integer $exp_time      expiration time
175
     * @param  string  $type          resource type
176
     *
177
     * @return integer number of cache files deleted
178
     */
179
    public function clearCache($template_name, $cache_id = null, $compile_id = null, $exp_time = null, $type = null)
180
    {
181
        $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
182
        \Xoops::getInstance()->deprecated(
183
            "XoopsTpl::clearCache() is potentially ambiguous. Called from {$trace[0]['file']} line {$trace[0]['line']}."
184
            . " See \Xoops\Core\XoopsTpl::clearModuleCompileCache() "
185
        );
186
187
        return parent::clearCache($template_name, $cache_id, $compile_id, $exp_time, $type);
188
    }
189
}
190