1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* JS.php |
4
|
|
|
* @author Revin Roman |
5
|
|
|
* @link https://rmrevin.com |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace rmrevin\yii\minify\components; |
9
|
|
|
|
10
|
|
|
use yii\helpers\Html; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class JS |
14
|
|
|
* @package rmrevin\yii\minify\components |
15
|
|
|
*/ |
16
|
|
|
class JS extends MinifyComponent |
17
|
|
|
{ |
18
|
|
|
|
19
|
4 |
|
public function minify() |
20
|
|
|
{ |
21
|
4 |
|
$jsFiles = $this->view->jsFiles; |
22
|
|
|
|
23
|
4 |
|
foreach ($jsFiles as $position => $files) { |
24
|
4 |
|
if (false === in_array($position, $this->view->js_position, true)) { |
25
|
4 |
|
$this->view->jsFiles[$position] = []; |
26
|
4 |
|
foreach ($files as $file => $html) { |
27
|
4 |
|
$this->view->jsFiles[$position][$file] = $html; |
28
|
4 |
|
} |
29
|
4 |
|
} else { |
30
|
4 |
|
$this->view->jsFiles[$position] = []; |
31
|
|
|
|
32
|
4 |
|
$toMinify = []; |
33
|
|
|
|
34
|
4 |
View Code Duplication |
foreach ($files as $file => $html) { |
|
|
|
|
35
|
4 |
|
if ($this->thisFileNeedMinify($file, $html)) { |
36
|
4 |
|
$toMinify[$file] = $html; |
37
|
4 |
|
} else { |
38
|
4 |
|
if (!empty($toMinify)) { |
39
|
|
|
$this->process($position, $toMinify); |
40
|
|
|
|
41
|
|
|
$toMinify = []; |
42
|
|
|
} |
43
|
|
|
|
44
|
4 |
|
$this->view->jsFiles[$position][$file] = $html; |
45
|
|
|
} |
46
|
4 |
|
} |
47
|
|
|
|
48
|
4 |
|
if (!empty($toMinify)) { |
49
|
4 |
|
$this->process($position, $toMinify); |
50
|
4 |
|
} |
51
|
|
|
|
52
|
4 |
|
unset($toMinify); |
53
|
|
|
} |
54
|
4 |
|
} |
55
|
4 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param integer $position |
59
|
|
|
* @param array $files |
60
|
|
|
*/ |
61
|
4 |
|
protected function process($position, $files) |
62
|
|
|
{ |
63
|
4 |
|
$resultFile = sprintf('%s/%s.js', $this->view->minify_path, $this->_getSummaryFilesHash($files)); |
64
|
4 |
|
if (!file_exists($resultFile)) { |
65
|
4 |
|
$js = ''; |
66
|
4 |
|
foreach ($files as $file => $html) { |
67
|
4 |
|
$file = $this->getAbsoluteFilePath($file); |
68
|
4 |
|
$js .= file_get_contents($file) . ';' . PHP_EOL; |
69
|
4 |
|
} |
70
|
|
|
|
71
|
4 |
|
$this->removeJsComments($js); |
|
|
|
|
72
|
|
|
|
73
|
4 |
|
$compressedJs = (new \JSMin($js)) |
74
|
4 |
|
->min(); |
75
|
|
|
|
76
|
4 |
|
file_put_contents($resultFile, $compressedJs); |
77
|
|
|
|
78
|
4 |
|
if (false !== $this->view->file_mode) { |
79
|
4 |
|
@chmod($resultFile, $this->view->file_mode); |
|
|
|
|
80
|
4 |
|
} |
81
|
4 |
|
} |
82
|
|
|
|
83
|
4 |
|
$file = sprintf('%s%s', \Yii::getAlias($this->view->web_path), str_replace(\Yii::getAlias($this->view->base_path), '', $resultFile)); |
84
|
|
|
|
85
|
4 |
|
$this->view->jsFiles[$position][$file] = Html::jsFile($file); |
86
|
4 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $code |
90
|
|
|
*/ |
91
|
4 |
|
protected function removeJsComments(&$code) |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
// @todo |
94
|
4 |
|
if (true === $this->view->removeComments) { |
|
|
|
|
95
|
|
|
//$code = preg_replace('', '', $code); |
|
|
|
|
96
|
4 |
|
} |
97
|
|
|
} |
98
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.