MinifyComponent   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.38%

Importance

Changes 0
Metric Value
wmc 28
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 191
ccs 62
cts 65
cp 0.9538
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
export() 0 1 ?
A __construct() 0 4 1
A getAbsoluteFilePath() 0 7 1
A cleanFileName() 0 6 2
A thisFileNeedMinify() 0 6 3
A isContainsConditionalComment() 0 4 1
A buildCacheKey() 0 4 1
A getFromCache() 0 8 2
A saveToCache() 0 8 2
A isUrl() 0 14 2
A isExcludedFile() 0 15 3
A prepareResultFile() 0 15 3
B _getSummaryFilesHash() 0 25 7
1
<?php
2
/**
3
 * Minify.php
4
 * @author Revin Roman
5
 * @link https://rmrevin.com
6
 */
7
8
namespace rmrevin\yii\minify\components;
9
10
use rmrevin\yii\minify\View;
11
use yii\caching\Cache;
12
use yii\caching\TagDependency;
13
14
/**
15
 * Class MinifyComponent
16
 * @package rmrevin\yii\minify\components
17
 */
18
abstract class MinifyComponent
19
{
20
21
    const CACHE_TAG = 'minify-view-tag';
22
23
    /**
24
     * @var View
25
     */
26
    protected $view;
27
28
    /**
29
     * MinifyComponent constructor.
30
     * @param View $view
31
     */
32 9
    public function __construct(View $view)
33
    {
34 9
        $this->view = $view;
35 9
    }
36
37
    abstract public function export();
38
39
    /**
40
     * @param string $file
41
     * @return string
42
     */
43 8
    protected function getAbsoluteFilePath($file)
44
    {
45 8
        $basePath = $this->view->basePath;
46 8
        $webPath = $this->view->webPath;
47
48 8
        return $basePath . str_replace($webPath, '', $this->cleanFileName($file));
49
    }
50
51
    /**
52
     * @param string $file
53
     * @return string
54
     */
55 8
    protected function cleanFileName($file)
56
    {
57 8
        return false !== mb_strpos($file, '?')
58 2
            ? parse_url($file, PHP_URL_PATH)
59 8
            : $file;
60
    }
61
62
    /**
63
     * @param string $file
64
     * @param string $html
65
     * @return bool
66
     */
67 8
    protected function thisFileNeedMinify($file, $html)
68
    {
69 8
        return !$this->isUrl($file, false)
70 8
            && !$this->isContainsConditionalComment($html)
71 8
            && !$this->isExcludedFile($file);
72
    }
73
74
    /**
75
     * @param string $url
76
     * @param boolean $checkSlash
77
     * @return bool
78
     */
79
    protected function isUrl($url, $checkSlash = true)
80
    {
81 8
        $schemas = array_map(function ($val) {
82 8
            return str_replace('/', '\/', $val);
83 8
        }, $this->view->schemas);
84
85 8
        $regexp = '#^(' . implode('|', $schemas) . ')#i';
86
87 8
        if ($checkSlash) {
88 6
            $regexp = '#^(/|\\\\|' . implode('|', $schemas) . ')#i';
89
        }
90
91 8
        return (bool)preg_match($regexp, $url);
92
    }
93
94
    /**
95
     * @param string $string
96
     * @return bool
97
     */
98 8
    protected function isContainsConditionalComment($string)
99
    {
100 8
        return mb_strpos($string, '<![endif]-->') !== false;
101
    }
102
103
    /**
104
     * @param string $file
105
     * @return bool
106
     */
107 8
    protected function isExcludedFile($file)
108
    {
109 8
        $result = false;
110
111 8
        foreach ((array)$this->view->excludeFiles as $excludedFile) {
112 2
            if (!preg_match('!' . $excludedFile . '!i', $file)) {
113 2
                continue;
114
            }
115
116 2
            $result = true;
117 2
            break;
118
        }
119
120 8
        return $result;
121
    }
122
123
    /**
124
     * @param string $resultFile
125
     * @return string
126
     */
127 8
    protected function prepareResultFile($resultFile)
128
    {
129 8
        $basePath = $this->view->basePath;
130 8
        $webPath = $this->view->webPath;
131
132 8
        $file = sprintf('%s%s', $webPath, str_replace($basePath, '', $resultFile));
133
134 8
        $AssetManager = $this->view->getAssetManager();
135
136 8
        if ($AssetManager->appendTimestamp && ($timestamp = @filemtime($resultFile)) > 0) {
137 2
            $file .= '?v=' . $timestamp;
138
        }
139
140 8
        return $file;
141
    }
142
143
    /**
144
     * @param array $files
145
     * @return string
146
     */
147 8
    protected function _getSummaryFilesHash($files)
148
    {
149 8
        $result = '';
150
151 8
        foreach ($files as $file => $html) {
152 8
            $path = $this->getAbsoluteFilePath($file);
153
154 8
            if (!$this->thisFileNeedMinify($file, $html) || !file_exists($path)) {
155
                continue;
156
            }
157
158 8
            switch ($this->view->fileCheckAlgorithm) {
159
                default:
160 8
                case 'filemtime':
161 1
                    $result .= filemtime($path) . $file;
162 1
                    break;
163 7
                case 'sha1': // deprecated
164 7
                case 'hash':
165 7
                    $result .= hash_file($this->view->currentHashAlgo, $path);
166 7
                    break;
167
            }
168
        }
169
170 8
        return hash($this->view->currentHashAlgo, $result);
171
    }
172
173
    /**
174
     * @param string $file
175
     * @return string
176
     */
177 8
    protected function buildCacheKey($file)
178
    {
179 8
        return hash('sha1', __CLASS__ . '/' . $file);
180
    }
181
182
    /**
183
     * @param string $key
184
     * @return string|false
185
     */
186 8
    protected function getFromCache($key)
187
    {
188 8
        if ($this->view->cache instanceof Cache) {
189 8
            return $this->view->cache->get($key);
190
        }
191
192
        return false;
193
    }
194
195
    /**
196
     * @param string $key
197
     * @param string $content
198
     * @return bool
199
     */
200 8
    protected function saveToCache($key, $content)
201
    {
202 8
        if ($this->view->cache instanceof Cache) {
203 8
            return $this->view->cache->set($key, $content, null, new TagDependency(['tags' => static::CACHE_TAG]));
204
        }
205
206
        return false;
207
    }
208
}
209