|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Minify.php |
|
4
|
|
|
* @author Revin Roman |
|
5
|
|
|
* @link https://processfast.com |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace processfast\yii\minify\components; |
|
9
|
|
|
|
|
10
|
|
|
use processfast\yii\minify\View; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class MinifyComponent |
|
15
|
|
|
* @package processfast\yii\minify\components |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class MinifyComponent |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var View |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $view; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* MinifyComponent constructor. |
|
27
|
|
|
* @param View $view |
|
28
|
|
|
*/ |
|
29
|
8 |
|
public function __construct(View $view) |
|
30
|
|
|
{ |
|
31
|
8 |
|
$this->view = $view; |
|
32
|
8 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
abstract public function export(); |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $file |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
7 |
|
protected function getAbsoluteFilePath($file) |
|
41
|
|
|
{ |
|
42
|
7 |
|
return \Yii::getAlias($this->view->basePath) . str_replace(\Yii::getAlias($this->view->webPath), '', $this->cleanFileName($file)); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $file |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
7 |
|
protected function cleanFileName($file) |
|
50
|
|
|
{ |
|
51
|
7 |
|
return (strpos($file, '?')) |
|
52
|
7 |
|
? parse_url($file, PHP_URL_PATH) |
|
53
|
7 |
|
: $file; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $file |
|
58
|
|
|
* @param string $html |
|
59
|
|
|
* @return bool |
|
60
|
|
|
*/ |
|
61
|
7 |
|
protected function thisFileNeedMinify($file, $html) |
|
62
|
|
|
{ |
|
63
|
7 |
|
return !$this->isUrl($file, false) |
|
64
|
7 |
|
&& !$this->isContainsConditionalComment($html) |
|
65
|
7 |
|
&& !$this->isExcludedFile($file); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string $url |
|
70
|
|
|
* @param boolean $checkSlash |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function isUrl($url, $checkSlash = true) |
|
74
|
|
|
{ |
|
75
|
7 |
|
$schemas = array_map(function ($val) { |
|
76
|
7 |
|
return str_replace('/', '\/', $val); |
|
77
|
7 |
|
}, $this->view->schemas); |
|
78
|
|
|
|
|
79
|
7 |
|
$regexp = '#^(' . implode('|', $schemas) . ')#is'; |
|
80
|
7 |
|
if ($checkSlash) { |
|
81
|
7 |
|
$regexp = '#^(/|\\\\|' . implode('|', $schemas) . ')#is'; |
|
82
|
7 |
|
} |
|
83
|
|
|
|
|
84
|
7 |
|
return (bool)preg_match($regexp, $url); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $string |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
7 |
|
protected function isContainsConditionalComment($string) |
|
92
|
|
|
{ |
|
93
|
7 |
|
return strpos($string, '<![endif]-->') !== false; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param string $file |
|
98
|
|
|
* @return bool |
|
99
|
|
|
*/ |
|
100
|
7 |
|
protected function isExcludedFile($file) |
|
101
|
|
|
{ |
|
102
|
7 |
|
$result = false; |
|
103
|
|
|
|
|
104
|
7 |
|
if (!empty($this->view->excludeFiles)) { |
|
105
|
2 |
|
foreach ((array)$this->view->excludeFiles as $excludedFile) { |
|
106
|
2 |
|
$reg = sprintf('!%s!i', $excludedFile); |
|
107
|
|
|
|
|
108
|
2 |
|
if (preg_match($reg, $file)) { |
|
109
|
2 |
|
$result = true; |
|
110
|
2 |
|
break; |
|
111
|
|
|
} |
|
112
|
2 |
|
} |
|
113
|
2 |
|
} |
|
114
|
|
|
|
|
115
|
7 |
|
return $result; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param string $resultFile |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
7 |
|
protected function prepareResultFile($resultFile) |
|
123
|
|
|
{ |
|
124
|
7 |
|
if( !$this->view->S3Upload ) |
|
125
|
7 |
|
{ |
|
126
|
7 |
|
$file = sprintf('%s%s', \Yii::getAlias($this->view->webPath), str_replace(\Yii::getAlias($this->view->basePath), '', $resultFile)); |
|
127
|
7 |
|
} |
|
128
|
|
|
else |
|
129
|
|
|
{ |
|
130
|
|
|
$file = $resultFile ; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
7 |
|
$AssetManager = $this->view->getAssetManager(); |
|
134
|
|
|
|
|
135
|
7 |
|
if ($AssetManager->appendTimestamp && ($timestamp = @filemtime($resultFile)) > 0) { |
|
136
|
2 |
|
$file .= "?v=$timestamp"; |
|
137
|
2 |
|
} |
|
138
|
|
|
|
|
139
|
7 |
|
return $file; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param array $files |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
7 |
|
protected function _getSummaryFilesHash($files) |
|
147
|
|
|
{ |
|
148
|
7 |
|
$result = ''; |
|
149
|
7 |
|
foreach ($files as $file => $html) { |
|
150
|
7 |
|
$path = $this->getAbsoluteFilePath($file); |
|
151
|
|
|
|
|
152
|
|
|
|
|
153
|
7 |
|
if( $this->view->modifyPath ) |
|
154
|
7 |
|
{ |
|
155
|
|
|
$modifyPathData = $this->view->modifyPathData ; |
|
156
|
|
|
if( strpos( $path , $modifyPathData ) !== false ) |
|
157
|
|
|
{ |
|
158
|
|
|
$path = str_replace( $modifyPathData , '' , $path ); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
7 |
|
if ($this->thisFileNeedMinify($file, $html) && file_exists($path)) { |
|
163
|
7 |
|
switch ($this->view->fileCheckAlgorithm) { |
|
164
|
7 |
|
default: |
|
165
|
7 |
|
case 'filemtime': |
|
166
|
1 |
|
$result .= filemtime($path) . $file; |
|
167
|
1 |
|
break; |
|
168
|
6 |
|
case 'sha1': |
|
169
|
6 |
|
$result .= sha1_file($path); |
|
170
|
6 |
|
break; |
|
171
|
7 |
|
} |
|
172
|
7 |
|
} |
|
173
|
7 |
|
} |
|
174
|
|
|
|
|
175
|
7 |
|
return sha1($result); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
protected function uploadToS3( $resultFile , $type , $hash ) |
|
179
|
|
|
{ |
|
180
|
|
|
$fileName = $this->getFileInfo( $resultFile , $type , $hash , "FILENAME" ); |
|
181
|
|
|
$mime_type = $this->getFileInfo( $resultFile , $type , $hash , "MIMETYPE" ); |
|
182
|
|
|
$file = $this->handleAssetUpload( $resultFile , $mime_type , $fileName ); |
|
183
|
|
|
return $file; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
protected function getS3Path( $resultFile , $type , $hash ) |
|
187
|
|
|
{ |
|
188
|
|
|
$fileName = $this->getFileInfo( $resultFile , $type , $hash , "FILENAME" ); |
|
189
|
|
|
$mime_type = $this->getFileInfo( $resultFile , $type , $hash , "MIMETYPE" ); |
|
|
|
|
|
|
190
|
|
|
$bucket = $this->view->awsBucket; |
|
191
|
|
|
return $this->s3Path( $bucket , $fileName ); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
protected function handleAssetUpload($temp, $type, $fileName, $acl = "public-read", $storageClass = "STANDARD") |
|
195
|
|
|
{ |
|
196
|
|
|
$aws = \Yii::$app->awssdk->getAwsSdk(); |
|
197
|
|
|
$s3 = $aws->createS3(); |
|
198
|
|
|
$bucket = $this->view->awsBucket; |
|
199
|
|
|
$contentEncoding = "gzip" ; |
|
200
|
|
|
|
|
201
|
|
|
try |
|
202
|
|
|
{ |
|
203
|
|
|
$result = $s3->putObject(array( |
|
|
|
|
|
|
204
|
|
|
'Bucket' => $bucket, |
|
205
|
|
|
'Key' => $fileName, |
|
206
|
|
|
'SourceFile' => $temp, |
|
207
|
|
|
'ContentType' => $type, |
|
208
|
|
|
'ContentEncoding'=>$contentEncoding, |
|
209
|
|
|
'ACL' => $acl, |
|
210
|
|
|
'StorageClass' => $storageClass |
|
211
|
|
|
)); |
|
212
|
|
|
} |
|
213
|
|
|
catch (Exception $e) |
|
|
|
|
|
|
214
|
|
|
{ |
|
215
|
|
|
throw new HttpException(431, 'Image upload failed to bucket.'); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
return $this->s3Path( $bucket , $fileName ); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
protected function getVersionName() |
|
222
|
|
|
{ |
|
223
|
|
|
$sql = 'SELECT current_version FROM `application_version`' ; |
|
224
|
|
|
$version = \Yii::$app->db->createCommand( $sql )->queryScalar(); |
|
225
|
|
|
return $version; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
protected function s3Path( $bucket , $fileName ) |
|
229
|
|
|
{ |
|
230
|
|
|
return "https://s3.amazonaws.com/".$bucket."/".$fileName; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
protected function checkS3Path( $fileName , $resultFile , $type ) |
|
234
|
|
|
{ |
|
235
|
|
|
$aws = \Yii::$app->awssdk->getAwsSdk(); |
|
236
|
|
|
$s3 = $aws->createS3(); |
|
237
|
|
|
$bucket = $this->view->awsBucket; |
|
238
|
|
|
|
|
239
|
|
|
if( $s3->doesObjectExist( $bucket , $fileName ) ) |
|
240
|
|
|
{ |
|
241
|
|
|
return $this->s3Path( $bucket , $fileName ); |
|
242
|
|
|
} |
|
243
|
|
|
else |
|
244
|
|
|
{ |
|
245
|
|
|
return $this->handleAssetUpload( $resultFile , $type ,$fileName ); |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
protected function doesObjectExist( $resultFile , $type , $hash ) |
|
250
|
|
|
{ |
|
251
|
|
|
$fileName = $this->getFileInfo( $resultFile , $type , $hash , "FILENAME" ); |
|
252
|
|
|
$mime_type = $this->getFileInfo( $resultFile , $type , $hash , "MIMETYPE" ); |
|
|
|
|
|
|
253
|
|
|
|
|
254
|
|
|
$aws = \Yii::$app->awssdk->getAwsSdk(); |
|
255
|
|
|
$s3 = $aws->createS3(); |
|
256
|
|
|
$bucket = $this->view->awsBucket; |
|
257
|
|
|
|
|
258
|
|
|
return $s3->doesObjectExist( $bucket , $fileName ); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
protected function getFileInfo( $resultFile , $type , $hash , $typeIn = "FILENAME" ) |
|
|
|
|
|
|
262
|
|
|
{ |
|
263
|
|
|
$versionName = self::getVersionName() ; |
|
264
|
|
|
|
|
265
|
|
|
$env = "" ; |
|
266
|
|
|
if( YII_ENV == 'dev' ) |
|
267
|
|
|
{ |
|
268
|
|
|
$env = "dev" ; |
|
269
|
|
|
} |
|
270
|
|
|
else if( YII_ENV == 'qa' ) |
|
271
|
|
|
{ |
|
272
|
|
|
$env = "qa" ; |
|
273
|
|
|
} |
|
274
|
|
|
else if( YII_ENV == 'prod' ) |
|
275
|
|
|
{ |
|
276
|
|
|
$env = "prod" ; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
$prefixCss = $this->view->prefixCssFile; |
|
280
|
|
|
$prefixJs = $this->view->prefixJsFile; |
|
281
|
|
|
|
|
282
|
|
|
$mime_type = null ; |
|
283
|
|
|
$fileName = null ; |
|
284
|
|
|
if( $type == "CSS" ) |
|
285
|
|
|
{ |
|
286
|
|
|
$mime_type = "text/css" ; |
|
287
|
|
|
$fileName = "web-assets/".$env."/minify/".$prefixCss."all-in-one-".$versionName."-".$hash.".css"; |
|
288
|
|
|
} |
|
289
|
|
|
else if( $type == "JS" ) |
|
290
|
|
|
{ |
|
291
|
|
|
$mime_type = "application/javascript" ; |
|
292
|
|
|
$fileName = "web-assets/".$env."/minify/".$prefixJs."all-in-one-".$versionName."-".$hash.".js"; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
if( $typeIn == "FILENAME" ) |
|
296
|
|
|
{ |
|
297
|
|
|
return $fileName; |
|
298
|
|
|
} |
|
299
|
|
|
else if( $typeIn == "MIMETYPE" ) |
|
300
|
|
|
{ |
|
301
|
|
|
return $mime_type; |
|
302
|
|
|
} |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
} |
|
306
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.