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
|
7 |
|
public function export() |
20
|
|
|
{ |
21
|
7 |
|
$jsFiles = $this->view->jsFiles; |
22
|
|
|
|
23
|
7 |
|
if (!empty($jsFiles)) { |
24
|
|
|
foreach ($jsFiles as $position => $files) { |
25
|
|
|
if (false === in_array($position, $this->view->js_position, true)) { |
26
|
6 |
|
$this->view->jsFiles[$position] = []; |
27
|
|
|
|
28
|
|
|
foreach ($files as $file => $html) { |
29
|
6 |
|
$this->view->jsFiles[$position][$file] = $html; |
30
|
|
|
} |
31
|
|
|
} else { |
32
|
6 |
|
$this->view->jsFiles[$position] = []; |
33
|
|
|
|
34
|
6 |
|
$toMinify = []; |
35
|
|
|
|
36
|
|
|
foreach ($files as $file => $html) { |
37
|
|
View Code Duplication |
if ($this->thisFileNeedMinify($file, $html)) { |
|
|
|
|
38
|
6 |
|
if ($this->view->concatJs) { |
39
|
6 |
|
$toMinify[$file] = $html; |
40
|
|
|
} else { |
41
|
|
|
$this->process($position, [$file => $html]); |
42
|
6 |
|
} |
43
|
|
|
} else { |
44
|
6 |
|
if (!empty($toMinify)) { |
45
|
|
|
$this->process($position, $toMinify); |
46
|
|
|
|
47
|
|
|
$toMinify = []; |
48
|
|
|
} |
49
|
|
|
|
50
|
6 |
|
$this->view->jsFiles[$position][$file] = $html; |
51
|
6 |
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
6 |
|
if (!empty($toMinify)) { |
55
|
|
|
$this->process($position, $toMinify); |
56
|
|
|
} |
57
|
|
|
|
58
|
6 |
|
unset($toMinify); |
59
|
6 |
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
7 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param integer $position |
66
|
|
|
* @param array $files |
67
|
|
|
*/ |
68
|
6 |
|
protected function process($position, $files) |
69
|
|
|
{ |
70
|
|
|
$resultFile = sprintf('%s/%s.js', $this->view->minify_path, $this->_getSummaryFilesHash($files)); |
71
|
|
|
|
72
|
|
|
if (!file_exists($resultFile)) { |
73
|
6 |
|
$js = ''; |
74
|
|
|
|
75
|
|
|
foreach ($files as $file => $html) { |
76
|
|
|
$file = $this->getAbsoluteFilePath($file); |
77
|
|
|
|
78
|
6 |
|
$content = ''; |
79
|
|
|
|
80
|
|
|
if (!file_exists($file)) { |
81
|
|
|
\Yii::warning(sprintf('Asset file not found `%s`', $file), __METHOD__); |
82
|
|
|
} elseif (!is_readable($file)) { |
83
|
|
|
\Yii::warning(sprintf('Asset file not readable `%s`', $file), __METHOD__); |
84
|
|
|
} else { |
85
|
|
|
$content .= file_get_contents($file) . ';' . "\n"; |
86
|
|
|
} |
87
|
|
|
|
88
|
6 |
|
$js .= $content; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$this->removeJsComments($js); |
|
|
|
|
92
|
|
|
|
93
|
6 |
|
if ($this->view->minifyJs) { |
94
|
|
|
$js = (new \JSMin($js)) |
95
|
|
|
->min(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
file_put_contents($resultFile, $js); |
99
|
|
|
|
100
|
6 |
|
if (false !== $this->view->file_mode) { |
101
|
|
|
@chmod($resultFile, $this->view->file_mode); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$file = $this->prepareResultFile($resultFile); |
106
|
|
|
|
107
|
|
|
$this->view->jsFiles[$position][$file] = Html::jsFile($file); |
108
|
6 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @todo |
112
|
|
|
* @param string $code |
113
|
|
|
*/ |
114
|
6 |
|
protected function removeJsComments(&$code) |
|
|
|
|
115
|
|
|
{ |
116
|
6 |
|
if (true === $this->view->removeComments) { |
|
|
|
|
117
|
|
|
//$code = preg_replace('', '', $code); |
|
|
|
|
118
|
|
|
} |
119
|
6 |
|
} |
120
|
|
|
} |
121
|
|
|
|
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.