1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* JS.php |
4
|
|
|
* @author Revin Roman |
5
|
|
|
* @link https://processfast.com |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace processfast\yii\minify\components; |
9
|
|
|
|
10
|
|
|
use yii\helpers\Html; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class JS |
14
|
|
|
* @package processfast\yii\minify\components |
15
|
|
|
*/ |
16
|
|
|
class JS extends MinifyComponent |
17
|
|
|
{ |
18
|
|
|
|
19
|
8 |
|
public function export() |
20
|
|
|
{ |
21
|
8 |
|
$jsFiles = $this->view->jsFiles; |
22
|
|
|
|
23
|
8 |
|
$jsPosition = $this->view->jsPosition; |
24
|
8 |
|
$jsOptions = $this->view->jsOptions; |
25
|
|
|
|
26
|
8 |
|
if (!empty($jsFiles)) { |
27
|
7 |
|
foreach ($jsFiles as $position => $files) { |
28
|
7 |
|
if (false === in_array($position, $jsPosition, true)) { |
29
|
7 |
|
$this->view->jsFiles[$position] = []; |
30
|
|
|
|
31
|
7 |
|
foreach ($files as $file => $html) { |
32
|
7 |
|
$this->view->jsFiles[$position][$file] = $html; |
33
|
7 |
|
} |
34
|
7 |
|
} else { |
35
|
7 |
|
$this->view->jsFiles[$position] = []; |
36
|
|
|
|
37
|
7 |
|
$toMinify = []; |
38
|
|
|
|
39
|
7 |
|
foreach ($files as $file => $html) { |
40
|
7 |
View Code Duplication |
if ($this->thisFileNeedMinify($file, $html)) { |
41
|
7 |
|
if ($this->view->concatJs) { |
42
|
7 |
|
$toMinify[$file] = $html; |
43
|
7 |
|
} else { |
44
|
|
|
$this->process($position, $jsOptions, [$file => $html]); |
45
|
|
|
} |
46
|
7 |
|
} else { |
47
|
7 |
|
if (!empty($toMinify)) { |
48
|
|
|
$this->process($position, $jsOptions, $toMinify); |
49
|
|
|
|
50
|
|
|
$toMinify = []; |
51
|
|
|
} |
52
|
|
|
|
53
|
7 |
|
$this->view->jsFiles[$position][$file] = $html; |
54
|
|
|
} |
55
|
7 |
|
} |
56
|
|
|
|
57
|
7 |
|
if (!empty($toMinify)) { |
58
|
7 |
|
$this->process($position, $jsOptions, $toMinify); |
59
|
7 |
|
} |
60
|
|
|
|
61
|
7 |
|
unset($toMinify); |
62
|
|
|
} |
63
|
7 |
|
} |
64
|
7 |
|
} |
65
|
8 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param integer $position |
69
|
|
|
* @param array $options |
70
|
|
|
* @param array $files |
71
|
|
|
*/ |
72
|
7 |
|
protected function process($position, $options, $files) |
73
|
|
|
{ |
74
|
7 |
|
$hash = $this->_getSummaryFilesHash($files) ; |
75
|
7 |
|
$resultFile = sprintf('%s/%s.js', $this->view->minifyPath, $hash); |
76
|
|
|
|
77
|
7 |
|
if( $this->view->S3Upload && $this->doesObjectExist( $resultFile , "JS" , $hash ) ) |
78
|
7 |
|
{ |
79
|
|
|
// It exist on s3 so just get |
80
|
|
|
$resultFile = $this->getS3Path( $resultFile , "JS" , $hash ); |
81
|
|
|
} |
82
|
7 |
|
else if (!file_exists($resultFile)) |
83
|
7 |
|
{ |
84
|
7 |
|
$js = ''; |
85
|
|
|
|
86
|
7 |
|
foreach ($files as $file => $html) { |
87
|
7 |
|
$file = $this->getAbsoluteFilePath($file); |
88
|
|
|
|
89
|
7 |
|
$content = ''; |
90
|
|
|
|
91
|
7 |
|
if (!file_exists($file)) { |
92
|
|
|
\Yii::warning(sprintf('Asset file not found `%s`', $file), __METHOD__); |
93
|
7 |
|
} elseif (!is_readable($file)) { |
94
|
|
|
\Yii::warning(sprintf('Asset file not readable `%s`', $file), __METHOD__); |
95
|
|
|
} else { |
96
|
7 |
|
$content .= file_get_contents($file) . ';' . "\n"; |
97
|
|
|
} |
98
|
|
|
|
99
|
7 |
|
$js .= $content; |
100
|
7 |
|
} |
101
|
|
|
|
102
|
7 |
|
$this->removeJsComments($js); |
103
|
|
|
|
104
|
7 |
|
if ($this->view->minifyJs) { |
105
|
7 |
|
$js = (new \JSMin($js)) |
106
|
7 |
|
->min(); |
107
|
7 |
|
} |
108
|
|
|
|
109
|
7 |
|
if( $this->view->gzipEncodeJs ){ |
110
|
|
|
$js = gzencode( $js , 9 ); |
111
|
|
|
} |
112
|
|
|
|
113
|
7 |
|
file_put_contents($resultFile, $js); |
114
|
|
|
|
115
|
7 |
|
if (false !== $this->view->fileMode) { |
116
|
7 |
|
@chmod($resultFile, $this->view->fileMode); |
|
|
|
|
117
|
7 |
|
} |
118
|
|
|
|
119
|
7 |
|
if( $this->view->S3Upload ) |
120
|
7 |
|
{ |
121
|
|
|
$resultFile = $this->uploadToS3( $resultFile , "JS" , $hash); |
122
|
|
|
} |
123
|
7 |
|
} |
124
|
|
|
else |
125
|
|
|
{ |
126
|
|
|
if( $this->view->S3Upload ) |
127
|
|
|
{ |
128
|
|
|
$resultFile = $this->uploadToS3( $resultFile , "JS" , $hash); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
7 |
|
$file = $this->prepareResultFile($resultFile); |
133
|
|
|
|
134
|
7 |
|
$this->view->jsFiles[$position][$file] = Html::jsFile($file, $options); |
135
|
7 |
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @todo |
139
|
|
|
* @param string $code |
140
|
|
|
*/ |
141
|
7 |
|
protected function removeJsComments(&$code) |
142
|
|
|
{ |
143
|
7 |
|
if (true === $this->view->removeComments) { |
144
|
|
|
//$code = preg_replace('', '', $code); |
145
|
7 |
|
} |
146
|
7 |
|
} |
147
|
|
|
} |
148
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: