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
|
|
|
public function __construct(View $view) |
29
|
|
|
{ |
30
|
|
|
$this->view = $view; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
abstract public function export(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $file |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
protected function getAbsoluteFilePath($file) |
40
|
|
|
{ |
41
|
|
|
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
|
|
|
protected function cleanFileName($file) |
49
|
|
|
{ |
50
|
|
|
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
|
|
|
protected function thisFileNeedMinify($file, $html) |
59
|
|
|
{ |
60
|
|
|
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
|
|
|
$schemas = array_map(function ($val) { |
71
|
|
|
return str_replace('/', '\/', $val); |
72
|
|
|
}, $this->view->schemas); |
73
|
|
|
|
74
|
|
|
$regexp = '#^(' . implode('|', $schemas) . ')#is'; |
75
|
|
|
if ($checkSlash) { |
76
|
|
|
$regexp = '#^(/|\\\\|' . implode('|', $schemas) . ')#is'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return (bool)preg_match($regexp, $url); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $string |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
protected function isContainsConditionalComment($string) |
87
|
|
|
{ |
88
|
|
|
return strpos($string, '<![endif]-->') !== false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $resultFile |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
protected function prepareResultFile($resultFile) |
96
|
|
|
{ |
97
|
|
|
$file = sprintf('%s%s', \Yii::getAlias($this->view->web_path), str_replace(\Yii::getAlias($this->view->base_path), '', $resultFile)); |
98
|
|
|
|
99
|
|
|
$AssetManager = $this->view->getAssetManager(); |
100
|
|
|
|
101
|
|
|
if ($AssetManager->appendTimestamp && ($timestamp = @filemtime($resultFile)) > 0) { |
102
|
|
|
$file .= "?v=$timestamp"; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $file; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param array $files |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
protected function _getSummaryFilesHash($files) |
113
|
|
|
{ |
114
|
|
|
$result = ''; |
115
|
|
|
foreach ($files as $file => $html) { |
116
|
|
|
$path = $this->getAbsoluteFilePath($file); |
117
|
|
|
|
118
|
|
|
if ($this->thisFileNeedMinify($file, $html) && file_exists($path)) { |
119
|
|
|
switch ($this->view->fileCheckAlgorithm) { |
120
|
|
|
default: |
121
|
|
|
case 'filemtime': |
122
|
|
|
$result .= filemtime($path) . $file; |
123
|
|
|
break; |
124
|
|
|
case 'sha1': |
125
|
|
|
$result .= sha1_file($path); |
126
|
|
|
break; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return sha1($result); |
132
|
|
|
} |
133
|
|
|
} |