Passed
Push — development ( af9588...0a99ba )
by Mirco
02:26
created

WebCache::preCompileTemplate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
1
<?php
2
/***************************************************************************
3
 *  For license information see doc/license.txt
4
 *
5
 *  Unicode Reminder メモ
6
 ***************************************************************************/
7
8
namespace Oc\Cache;
9
10
class WebCache
11
{
12
    /** @var array $opt */
13
    private $opt;
14
15
    /** @var \translate|\translateEdit $translate */
16
    private $translate;
17
18
19
    /**
20
     * WebCache constructor.
21
     */
22
    public function __construct()
23
    {
24
        global $opt, $translate;
25
26
        $this->opt = $opt;
27
        $this->translate = $translate;
28
29
        if (!isset($this->opt['rootpath'])) {
30
            $this->opt['rootpath'] = realpath(__DIR__ . '/../../../') . '/';
31
        }
32
    }
33
34
    /**
35
     *
36
     */
37
    public function clearCache()
38
    {
39
        $this->unlinkFiles('cache2', 'php');
40
41
        $this->unlinkFiles('cache2/smarty/cache', 'tpl');
42
        $this->unlinkFiles('cache2/smarty/compiled', 'inc');
43
        $this->unlinkFiles('cache2/smarty/compiled', 'php');
44
    }
45
46
    /**
47
     * @param $relBaseDir
48
     * @param $ext
49
     */
50
    private function unlinkFiles($relBaseDir, $ext)
51
    {
52
        if (substr($relBaseDir, -1, 1) !== '/') {
53
            $relBaseDir .= '/';
54
        }
55
56
        if ($this->opt['rootpath'] . $relBaseDir) {
57
            if ($dh = opendir($this->opt['rootpath'] . $relBaseDir)) {
58
                while (($file = readdir($dh)) !== false) {
59
                    if ($file !== '.' && $file !== '..' && is_file($this->opt['rootpath'] . $relBaseDir . $file)) {
60 View Code Duplication
                        if (substr($file, -(strlen($ext) + 1), strlen($ext) + 1) === '.' . $ext) {
61
                            unlink($this->opt['rootpath'] . $relBaseDir . $file);
62
                        }
63
                    }
64
                }
65
                closedir($dh);
66
            }
67
        }
68
    }
69
70
    /**
71
     *
72
     */
73
    public function createMenuCache()
74
    {
75
        global $opt;
76
        foreach ($this->opt['locale'] as $sLanguage => $v) {
77
            if ($this->opt['template']['locales'][$sLanguage]['status'] !== OC_LOCALE_DISABLED) {
78
                // cheating a little bit
79
                $opt['template']['locale'] = $sLanguage;
80
                \set_php_locale();
81
82 View Code Duplication
                if ($this->translate->t('INTERNAL_LANG', 'all', 'OcSmarty.class.php', '') !== $sLanguage) {
83
                    echo 'setlocale() failed to set language to ' . $sLanguage ."\n";
84
                    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...
85
                }
86
87
                // this will create the cache file
88
                $menu = new \Menu();
89
90
                // change to file owner
91
                chown($menu->sMenuFilename, $this->opt['httpd']['user']);
92
                chgrp($menu->sMenuFilename, $this->opt['httpd']['group']);
93
            }
94
        }
95
    }
96
97
    /**
98
     *
99
     */
100
    public function createLabelCache()
101
    {
102
        global $opt;
103
104
        foreach ($this->opt['locale'] as $sLanguage => $v) {
105
            if ($this->opt['template']['locales'][$sLanguage]['status'] !== OC_LOCALE_DISABLED) {
106
                // cheating a little bit
107
                $opt['template']['locale'] = $sLanguage;
108
109
                \labels::CreateCacheFile();
110
111
                // change to file owner
112
                $sFilename = $this->opt['rootpath'] . 'cache2/labels-' . $this->opt['template']['locale'] . '.inc.php';
113
                chown($sFilename, $this->opt['httpd']['user']);
114
                chgrp($sFilename, $this->opt['httpd']['group']);
115
            }
116
        }
117
    }
118
119
    /**
120
     *
121
     */
122
    public function preCompileAllTemplates()
123
    {
124
        if ($hDir = opendir($this->opt['stylepath'])) {
125 View Code Duplication
            while (($sFilename = readdir($hDir)) !== false) {
126
                if (substr($sFilename, -4) === '.tpl') {
127
                    $this->preCompileTemplate(substr($sFilename, 0, strlen($sFilename) - 4));
128
                }
129
            }
130
            closedir($hDir);
131
        }
132
133
        // fix file ownership
134
        $sCompileDir = $this->opt['rootpath'] . 'cache2/smarty/compiled/';
135
        if ($hDir = opendir($sCompileDir)) {
136
            while (($sFilename = readdir($hDir)) !== false) {
137
                if (filetype($sCompileDir . $sFilename) === 'file') {
138
                    chown($sCompileDir . $sFilename, $this->opt['httpd']['user']);
139
                    chgrp($sCompileDir . $sFilename, $this->opt['httpd']['group']);
140
                }
141
            }
142
            closedir($hDir);
143
        }
144
    }
145
146
    /**
147
     * @param $sTemplate
148
     */
149
    private function preCompileTemplate($sTemplate)
150
    {
151
        foreach ($this->opt['locale'] as $sLanguage => $v) {
152
            if ($this->opt['template']['locales'][$sLanguage]['status'] !== OC_LOCALE_DISABLED) {
153
                $this->preCompileTemplateWithLanguage($sTemplate, $sLanguage);
154
            }
155
        }
156
    }
157
158
    /**
159
     * @param $sTemplate
160
     * @param $sLanguage
161
     */
162
    private function preCompileTemplateWithLanguage($sTemplate, $sLanguage)
163
    {
164
        global $opt;
165
166
        // cheating a little bit
167
        $opt['template']['locale'] = $sLanguage;
168
        \set_php_locale();
169
170 View Code Duplication
        if ($this->translate->t('INTERNAL_LANG', 'all', 'OcSmarty.class.php', '') != $sLanguage) {
171
            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...
172
        }
173
174
        $preTemplate = new \OcSmarty();
175
        $preTemplate->name = $sTemplate;
176
        $preTemplate->compile($sTemplate . '.tpl', $preTemplate->get_compile_id());
177
    }
178
}
179