Completed
Pull Request — master (#462)
by Richard
15:21
created

XoopsTpl::clearCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
c 1
b 0
f 0
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 5
crap 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-2015 XOOPS Project (http://xoops.org)
23
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
24
 * @link      http://xoops.org
25
 */
26
class XoopsTpl extends \Smarty
27
{
28
    /**
29
     * @var \Xoops\Core\Theme\XoopsTheme
30
     */
31
    public $currentTheme = null;
32
33
    /**
34
     * XoopsTpl constructor
35
     */
36 13
    public function __construct()
37
    {
38 13
        parent::__construct(); // SMARTY_PLUGINS_DIR is initialized into parent
39 13
        $xoops = \Xoops::getInstance();
40 13
        $xoops->preload()->triggerEvent('core.template.construct.start', array($this));
41
42 13
        $this->registerFilter(
43 13
            'pre',
44 13
            [$this, 'convertLegacyDelimiters']
45 13
        );
46
47
        //$this->left_delimiter = '<{';
48
        //$this->right_delimiter = '}>';
49
50 13
        $this->setTemplateDir(\XoopsBaseConfig::get('themes-path'));
51 13
        $this->setCacheDir(\XoopsBaseConfig::get('smarty-cache'));
52 13
        $this->setCompileDir(\XoopsBaseConfig::get('smarty-compile'));
53 13
        $this->compile_check = ($xoops->getConfig('theme_fromfile') == 1);
54 13
        $this->setPluginsDir(\XoopsBaseConfig::get('smarty-xoops-plugins'));
55 13
        $this->addPluginsDir(SMARTY_PLUGINS_DIR);
56 13
        $this->setCompileId();
57 13
        $this->assign(
58 13
            array('xoops_url' => \XoopsBaseConfig::get('url'),
59 13
                'xoops_rootpath' => \XoopsBaseConfig::get('root-path'),
60 13
                'xoops_langcode' => \XoopsLocale::getLangCode(),
61 13
                'xoops_charset' => \XoopsLocale::getCharset(),
62 13
                'xoops_version' => \Xoops::VERSION,
63 13
                'xoops_upload_url' => \XoopsBaseConfig::get('uploads-url'))
64 13
        );
65 13
    }
66
67
    /**
68
     * XOOPS legacy used '<{' and '}>' as delimiters rather than using the default '{' and '}'.
69
     * This prefilter function converts any legacy delimiters to Smarty default delimiters.
70
     *
71
     * The intention is to phase out the legacy delimiters entirely.
72
     *
73
     * @param string                    $tpl_source template source
74
     * @param \Smarty_Internal_Template $template   template object
75
     *
76
     * @return string source with any legacy delimiters converted to standard default delimiters
77
     */
78 4
    public function convertLegacyDelimiters($tpl_source, \Smarty_Internal_Template $template)
79
    {
80 4
        return str_replace(['<{', '}>'], ['{', '}'], $tpl_source);
81
    }
82
83
    /**
84
     * XoopsTpl::touch
85
     *
86
     * @param string $resourceName name of resource
87
     *
88
     * @return bool
89
     */
90 1
    public function touch($resourceName)
91
    {
92 1
        $isForced = $this->force_compile;
93 1
        $this->force_compile = true;
94 1
        $this->clearCache($resourceName);
95 1
        $result = true; // $this->_compile_resource($resourceName, $this->_get_compile_path($resourceName));
96 1
        $this->force_compile = $isForced;
97 1
        return $result;
98
    }
99
100
    /**
101
     * XoopsTpl::setCompileId()
102
     *
103
     * @param mixed $module_dirname module directory
104
     * @param mixed $theme_set      theme set
105
     * @param mixed $template_set   template set
106
     *
107
     * @return void
108
     */
109 13
    public function setCompileId($module_dirname = null, $theme_set = null, $template_set = null)
110
    {
111 13
        $xoops = \Xoops::getInstance();
112
113 13
        $template_set = empty($template_set) ? $xoops->getConfig('template_set') : $template_set;
114 13
        $theme_set = empty($theme_set) ? $xoops->getConfig('theme_set') : $theme_set;
115 13
        $module_dirname = empty($module_dirname) ? $xoops->moduleDirname : $module_dirname;
116 13
        $this->compile_id = substr(md5(\XoopsBaseConfig::get('url')), 0, 8) . '-' . $module_dirname
117 13
            . '-' . $theme_set . '-' . $template_set;
118
        //$this->_compile_id = $this->compile_id;
119 13
    }
120
121
    /**
122
     * XoopsTpl::clearModuleCompileCache()
123
     *
124
     * Clean up compiled and cached templates for a module
125
     *
126
     * TODO - handle $use_sub_dirs cases
127
     *
128
     * @param mixed $module_dirname module directory
129
     * @param mixed $theme_set      theme set
130
     * @param mixed $template_set   template set
131
     *
132
     * @return int number of deleted cache and compiler files
133
     */
134
    public function clearModuleCompileCache($module_dirname = null, $theme_set = null, $template_set = null)
135
    {
136
        $hold_compile_id = $this->compile_id;
137
        // $this->setCompileId($module_dirname, $template_set, $theme_set);
138
        // TODO - should handle $use_sub_dirs
139
        $this->setCompileId($module_dirname, '*', '*');
140
        $compile_id = $this->compile_id;
141
        $this->compile_id = $hold_compile_id;
142
        $compile_id = preg_replace('![^\w\|]+!', '_', $compile_id);
143
        $glob = $compile_id . '*.php';
144
        $count=0;
145
        $files = glob($this->getCompileDir() . '/' . $glob);
146
        foreach ($files as $filename) {
147
            $count += unlink($filename) ? 1 : 0;
148
        }
149
        $files = glob($this->getCacheDir() . '/*' . $glob);
150
        foreach ($files as $filename) {
151
            $count += unlink($filename) ? 1 : 0;
152
        }
153
        return $count;
154
    }
155
156
    /**
157
     * Empty cache for a specific template
158
     *
159
     * This is just a pass-through wrapper with a warning since this method previously existed
160
     * only in XoopsTpl, but now is also a regular Smarty method.
161
     *
162
     * clearModuleCompileCache() is the replacement for the old clearCache
163
     *
164
     * @param  string  $template_name template name
165
     * @param  string  $cache_id      cache id
166
     * @param  string  $compile_id    compile id
167
     * @param  integer $exp_time      expiration time
168
     * @param  string  $type          resource type
169
     *
170
     * @return integer number of cache files deleted
171
     */
172 1
    public function clearCache($template_name, $cache_id = null, $compile_id = null, $exp_time = null, $type = null)
173
    {
174 1
        \Xoops::getInstance()->deprecated('XoopsTpl::clearCache() is potentially ambiguous');
175 1
        return parent::clearCache($template_name, $cache_id, $compile_id, $exp_time, $type);
176
    }
177
}
178