Completed
Pull Request — development (#560)
by Thomas
09:56 queued 02:10
created

WebCache::preCompileAllTemplates()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 23
Code Lines 13

Duplication

Lines 5
Ratio 21.74 %

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 8
nop 0
dl 5
loc 23
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/***************************************************************************
3
 * for license information see LICENSE.md
4
 ***************************************************************************/
5
6
namespace OcLegacy\Cache;
7
8
class WebCache
9
{
10
    /**
11
     * @var array $opt
12
     */
13
    private $opt;
14
15
    /** @var \translate|\translateEdit $translate */
16
    private $translate;
17
18
    /**
19
     * WebCache constructor.
20
     */
21
    public function __construct()
22
    {
23
        global $opt, $translate;
24
25
        $this->opt = $opt;
26
        $this->translate = $translate;
27
28
        if (!isset($this->opt['rootpath'])) {
29
            $this->opt['rootpath'] = dirname(dirname(dirname(__DIR__))) . '/';
30
        }
31
    }
32
33
    /**
34
     *
35
     */
36
    public function clearCache()
37
    {
38
        $this->unlinkFiles('var/cache2', 'php');
39
40
        $this->unlinkFiles('var/cache2/smarty/cache', 'tpl');
41
        $this->unlinkFiles('var/cache2/smarty/compiled', 'inc');
42
        $this->unlinkFiles('var/cache2/smarty/compiled', 'php');
43
    }
44
45
    /**
46
     * @param string $relBaseDir
47
     * @param string $ext
48
     */
49
    private function unlinkFiles($relBaseDir, $ext)
50
    {
51
        if (substr($relBaseDir, -1, 1) !== '/') {
52
            $relBaseDir .= '/';
53
        }
54
55
        if ($this->opt['rootpath'] . $relBaseDir) {
56
            if ($dh = opendir($this->opt['rootpath'] . $relBaseDir)) {
57
                while (($file = readdir($dh)) !== false) {
58
                    if ($file !== '.' && $file !== '..' && is_file($this->opt['rootpath'] . $relBaseDir . $file)) {
59 View Code Duplication
                        if (substr($file, -(strlen($ext) + 1), strlen($ext) + 1) === '.' . $ext) {
60
                            unlink($this->opt['rootpath'] . $relBaseDir . $file);
61
                        }
62
                    }
63
                }
64
                closedir($dh);
65
            }
66
        }
67
    }
68
69
    /**
70
     *
71
     */
72
    public function createMenuCache()
73
    {
74
        global $opt;
75
        foreach ($this->opt['locale'] as $sLanguage => $v) {
76
            if ($this->opt['template']['locales'][$sLanguage]['status'] !== OC_LOCALE_DISABLED) {
77
                // cheating a little bit
78
                $opt['template']['locale'] = $sLanguage;
79
                \set_php_locale();
80
81 View Code Duplication
                if ($this->translate->t('INTERNAL_LANG', 'all', 'OcSmarty.class.php', '') !== $sLanguage) {
82
                    echo 'setlocale() failed to set language to ' . $sLanguage ."\n";
83
                    die("Is the translation of INTERNAL_LANG correct?\n");
0 ignored issues
show
Coding Style Compatibility introduced by
The method createMenuCache() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
84
                }
85
86
                // this will create the cache file
87
                $menu = new \Menu();
88
89
                // change to file owner
90
                chown($menu->sMenuFilename, $this->opt['httpd']['user']);
91
                chgrp($menu->sMenuFilename, $this->opt['httpd']['group']);
92
            }
93
        }
94
    }
95
96
    /**
97
     *
98
     */
99
    public function createLabelCache()
100
    {
101
        global $opt;
102
103
        foreach ($this->opt['locale'] as $sLanguage => $v) {
104
            if ($this->opt['template']['locales'][$sLanguage]['status'] !== OC_LOCALE_DISABLED) {
105
                // cheating a little bit
106
                $opt['template']['locale'] = $sLanguage;
107
108
                \labels::CreateCacheFile();
109
110
                // change to file owner
111
                $sFilename = $this->opt['rootpath'] . 'var/cache2/labels-' . $this->opt['template']['locale'] . '.inc.php';
112
                chown($sFilename, $this->opt['httpd']['user']);
113
                chgrp($sFilename, $this->opt['httpd']['group']);
114
            }
115
        }
116
    }
117
118
    /**
119
     *
120
     */
121
    public function preCompileAllTemplates()
122
    {
123
        if ($hDir = opendir($this->opt['stylepath'])) {
124 View Code Duplication
            while (($sFilename = readdir($hDir)) !== false) {
125
                if (substr($sFilename, -4) === '.tpl') {
126
                    $this->preCompileTemplate(substr($sFilename, 0, strlen($sFilename) - 4));
127
                }
128
            }
129
            closedir($hDir);
130
        }
131
132
        // fix file ownership
133
        $sCompileDir = $this->opt['rootpath'] . 'var/cache2/smarty/compiled/';
134
        if ($hDir = opendir($sCompileDir)) {
135
            while (($sFilename = readdir($hDir)) !== false) {
136
                if (filetype($sCompileDir . $sFilename) === 'file') {
137
                    chown($sCompileDir . $sFilename, $this->opt['httpd']['user']);
138
                    chgrp($sCompileDir . $sFilename, $this->opt['httpd']['group']);
139
                }
140
            }
141
            closedir($hDir);
142
        }
143
    }
144
145
    /**
146
     * @param string $sTemplate
147
     */
148
    private function preCompileTemplate($sTemplate)
149
    {
150
        foreach ($this->opt['locale'] as $sLanguage => $v) {
151
            if ($this->opt['template']['locales'][$sLanguage]['status'] !== OC_LOCALE_DISABLED) {
152
                $this->preCompileTemplateWithLanguage($sTemplate, $sLanguage);
153
            }
154
        }
155
    }
156
157
    /**
158
     * @param $sTemplate
159
     * @param $sLanguage
160
     */
161
    private function preCompileTemplateWithLanguage($sTemplate, $sLanguage)
162
    {
163
        global $opt;
164
165
        // cheating a little bit
166
        $opt['template']['locale'] = $sLanguage;
167
        \set_php_locale();
168
169 View Code Duplication
        if ($this->translate->t('INTERNAL_LANG', 'all', 'OcSmarty.class.php', '') != $sLanguage) {
170
            die('setlocale() failed to set language to ' . $sLanguage . ". Is the translation of INTERNAL_LANG correct?\n");
0 ignored issues
show
Coding Style Compatibility introduced by
The method preCompileTemplateWithLanguage() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
171
        }
172
173
        $preTemplate = new \OcSmarty();
174
        $preTemplate->name = $sTemplate;
175
        $preTemplate->compile($sTemplate . '.tpl', $preTemplate->get_compile_id());
176
    }
177
}
178