Completed
Push — master ( 0e5ba9...e0f917 )
by Richard
05:08
created

XoopsTpl::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 1

Importance

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