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
|
8 |
|
public function __construct(View $view) |
29
|
|
|
{ |
30
|
8 |
|
$this->view = $view; |
31
|
8 |
|
} |
32
|
|
|
|
33
|
|
|
abstract public function export(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $file |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
7 |
|
protected function getAbsoluteFilePath($file) |
40
|
|
|
{ |
41
|
7 |
|
return \Yii::getAlias($this->view->basePath) . str_replace(\Yii::getAlias($this->view->webPath), '', $this->cleanFileName($file)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $file |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
7 |
|
protected function cleanFileName($file) |
49
|
|
|
{ |
50
|
7 |
|
return (strpos($file, '?')) |
51
|
7 |
|
? parse_url($file, PHP_URL_PATH) |
52
|
7 |
|
: $file; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $file |
57
|
|
|
* @param string $html |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
7 |
|
protected function thisFileNeedMinify($file, $html) |
61
|
|
|
{ |
62
|
7 |
|
return !$this->isUrl($file, false) |
63
|
7 |
|
&& !$this->isContainsConditionalComment($html) |
64
|
7 |
|
&& !$this->isExcludedFile($file); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $url |
69
|
|
|
* @param boolean $checkSlash |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
protected function isUrl($url, $checkSlash = true) |
73
|
|
|
{ |
74
|
7 |
|
$schemas = array_map(function ($val) { |
75
|
7 |
|
return str_replace('/', '\/', $val); |
76
|
7 |
|
}, $this->view->schemas); |
77
|
|
|
|
78
|
7 |
|
$regexp = '#^(' . implode('|', $schemas) . ')#is'; |
79
|
7 |
|
if ($checkSlash) { |
80
|
7 |
|
$regexp = '#^(/|\\\\|' . implode('|', $schemas) . ')#is'; |
81
|
7 |
|
} |
82
|
|
|
|
83
|
7 |
|
return (bool)preg_match($regexp, $url); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $string |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
7 |
|
protected function isContainsConditionalComment($string) |
91
|
|
|
{ |
92
|
7 |
|
return strpos($string, '<![endif]-->') !== false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $file |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
7 |
|
protected function isExcludedFile($file) |
100
|
|
|
{ |
101
|
7 |
|
return in_array(basename($file), $this->view->excludeFiles, true); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $resultFile |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
7 |
|
protected function prepareResultFile($resultFile) |
109
|
|
|
{ |
110
|
7 |
|
$file = sprintf('%s%s', \Yii::getAlias($this->view->webPath), str_replace(\Yii::getAlias($this->view->basePath), '', $resultFile)); |
111
|
|
|
|
112
|
7 |
|
$AssetManager = $this->view->getAssetManager(); |
113
|
|
|
|
114
|
7 |
|
if ($AssetManager->appendTimestamp && ($timestamp = @filemtime($resultFile)) > 0) { |
115
|
2 |
|
$file .= "?v=$timestamp"; |
116
|
2 |
|
} |
117
|
|
|
|
118
|
7 |
|
return $file; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param array $files |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
7 |
|
protected function _getSummaryFilesHash($files) |
126
|
|
|
{ |
127
|
7 |
|
$result = ''; |
128
|
7 |
|
foreach ($files as $file => $html) { |
129
|
7 |
|
$path = $this->getAbsoluteFilePath($file); |
130
|
|
|
|
131
|
7 |
|
if ($this->thisFileNeedMinify($file, $html) && file_exists($path)) { |
132
|
7 |
|
switch ($this->view->fileCheckAlgorithm) { |
133
|
7 |
|
default: |
134
|
7 |
|
case 'filemtime': |
135
|
1 |
|
$result .= filemtime($path) . $file; |
136
|
1 |
|
break; |
137
|
6 |
|
case 'sha1': |
138
|
6 |
|
$result .= sha1_file($path); |
139
|
6 |
|
break; |
140
|
7 |
|
} |
141
|
7 |
|
} |
142
|
7 |
|
} |
143
|
|
|
|
144
|
7 |
|
return sha1($result); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|