Completed
Push — master ( 2fd39c...5d8d2a )
by Revin
02:32
created

MinifyComponent::cleanFileName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 2
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 4
    public function __construct(View $view)
29
    {
30 4
        $this->view = $view;
31 4
    }
32
33
    abstract public function minify();
34
35
    /**
36
     * @param string $file
37
     * @return string
38
     */
39 4
    protected function getAbsoluteFilePath($file)
40
    {
41 4
        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 4
    protected function cleanFileName($file)
49
    {
50 4
        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 4
    protected function thisFileNeedMinify($file, $html)
59
    {
60 4
        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 4
        $schemas = array_map(function ($val) {
71 4
            return str_replace('/', '\/', $val);
72 4
        }, $this->view->schemas);
73
74 4
        $regexp = '#^(' . implode('|', $schemas) . ')#is';
75 4
        if ($checkSlash) {
76 4
            $regexp = '#^(/|\\\\|' . implode('|', $schemas) . ')#is';
77 4
        }
78
79 4
        return (bool)preg_match($regexp, $url);
80
    }
81
82
    /**
83
     * @param string $string
84
     * @return bool
85
     */
86 4
    protected function isContainsConditionalComment($string)
87
    {
88 4
        return strpos($string, '<![endif]-->') !== false;
89
    }
90
91
    /**
92
     * @param array $files
93
     * @return string
94
     */
95 4
    protected function _getSummaryFilesHash($files)
96
    {
97 4
        $result = '';
98 4
        foreach ($files as $file => $html) {
99 4
            $path = $this->getAbsoluteFilePath($file);
100
101 4
            if ($this->thisFileNeedMinify($file, $html) && file_exists($path)) {
102 4
                switch ($this->view->fileCheckAlgorithm) {
103 4
                    default:
104 4
                    case 'filemtime':
105 4
                        $result .= filemtime($path);
106 4
                        break;
107
                    case 'sha1':
108
                        $result .= sha1_file($path);
109
                        break;
110 4
                }
111 4
            }
112 4
        }
113
114 4
        return sha1($result);
115
    }
116
}