Completed
Push — master ( 34901f...56f37c )
by Revin
02:39
created

MinifyComponent::_getSummaryFilesHash()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
ccs 17
cts 17
cp 1
rs 8.7624
cc 6
eloc 14
nc 5
nop 1
crap 6
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
12
/**
13
 * Class MinifyComponent
14
 * @package rmrevin\yii\minify\components
15
 */
16
abstract class MinifyComponent
17
{
18
19
    /**
20
     * @var View
21
     */
22
    protected $view;
23
24
    /**
25
     * MinifyComponent constructor.
26
     * @param View $view
27
     */
28 7
    public function __construct(View $view)
29
    {
30 7
        $this->view = $view;
31 7
    }
32
33
    abstract public function export();
34
35
    /**
36
     * @param string $file
37
     * @return string
38
     */
39 6
    protected function getAbsoluteFilePath($file)
40
    {
41 6
        return \Yii::getAlias($this->view->base_path) . str_replace(\Yii::getAlias($this->view->web_path), '', $this->cleanFileName($file));
42
    }
43
44
    /**
45
     * @param string $file
46
     * @return string
47
     */
48 6
    protected function cleanFileName($file)
49
    {
50 6
        return (strpos($file, '?')) ? parse_url($file, PHP_URL_PATH) : $file;
51
    }
52
53
    /**
54
     * @param string $file
55
     * @param string $html
56
     * @return bool
57
     */
58 6
    protected function thisFileNeedMinify($file, $html)
59
    {
60 6
        return !$this->isUrl($file, false) && !$this->isContainsConditionalComment($html);
61
    }
62
63
    /**
64
     * @param string $url
65
     * @param boolean $checkSlash
66
     * @return bool
67
     */
68
    protected function isUrl($url, $checkSlash = true)
69
    {
70 6
        $schemas = array_map(function ($val) {
71 6
            return str_replace('/', '\/', $val);
72 6
        }, $this->view->schemas);
73
74 6
        $regexp = '#^(' . implode('|', $schemas) . ')#is';
75 6
        if ($checkSlash) {
76 6
            $regexp = '#^(/|\\\\|' . implode('|', $schemas) . ')#is';
77 6
        }
78
79 6
        return (bool)preg_match($regexp, $url);
80
    }
81
82
    /**
83
     * @param string $string
84
     * @return bool
85
     */
86 6
    protected function isContainsConditionalComment($string)
87
    {
88 6
        return strpos($string, '<![endif]-->') !== false;
89
    }
90
91
    /**
92
     * @param string $resultFile
93
     * @return string
94
     */
95 6
    protected function prepareResultFile($resultFile)
96
    {
97 6
        $file = sprintf('%s%s', \Yii::getAlias($this->view->web_path), str_replace(\Yii::getAlias($this->view->base_path), '', $resultFile));
98
99 6
        $AssetManager = $this->view->getAssetManager();
100
101 6
        if ($AssetManager->appendTimestamp && ($timestamp = @filemtime($resultFile)) > 0) {
102 2
            $file .= "?v=$timestamp";
103 2
        }
104
105 6
        return $file;
106
    }
107
108
    /**
109
     * @param array $files
110
     * @return string
111
     */
112 6
    protected function _getSummaryFilesHash($files)
113
    {
114 6
        $result = '';
115 6
        foreach ($files as $file => $html) {
116 6
            $path = $this->getAbsoluteFilePath($file);
117
118 6
            if ($this->thisFileNeedMinify($file, $html) && file_exists($path)) {
119 6
                switch ($this->view->fileCheckAlgorithm) {
120 6
                    default:
121 6
                    case 'filemtime':
122 1
                        $result .= filemtime($path) . $file;
123 1
                        break;
124 5
                    case 'sha1':
125 5
                        $result .= sha1_file($path);
126 5
                        break;
127 6
                }
128 6
            }
129 6
        }
130
131 6
        return sha1($result);
132
    }
133
}