1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* View.php |
4
|
|
|
* @author Revin Roman |
5
|
|
|
* @link https://rmrevin.ru |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace rmrevin\yii\minify; |
9
|
|
|
|
10
|
|
|
use yii\base\Event; |
11
|
|
|
use yii\helpers\FileHelper; |
12
|
|
|
use yii\web\Response; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class View |
16
|
|
|
* @package rmrevin\yii\minify |
17
|
|
|
*/ |
18
|
|
|
class View extends \yii\web\View |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
public $enableMinify = true; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string filemtime or sha1 |
28
|
|
|
*/ |
29
|
|
|
public $fileCheckAlgorithm = 'sha1'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
public $concatCss = true; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
public $minifyCss = true; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
public $concatJs = true; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var bool |
48
|
|
|
*/ |
49
|
|
|
public $minifyJs = true; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var bool |
53
|
|
|
*/ |
54
|
|
|
public $minifyOutput = false; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var bool |
58
|
|
|
*/ |
59
|
|
|
public $removeComments = true; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var string path alias to web base (in url) |
63
|
|
|
*/ |
64
|
|
|
public $web_path = '@web'; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var string path alias to web base (absolute) |
68
|
|
|
*/ |
69
|
|
|
public $base_path = '@webroot'; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var string path alias to save minify result |
73
|
|
|
*/ |
74
|
|
|
public $minify_path = '@webroot/minify'; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var array positions of js files to be minified |
78
|
|
|
*/ |
79
|
|
|
public $js_position = [self::POS_END, self::POS_HEAD]; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var bool|string charset forcibly assign, otherwise will use all of the files found charset |
83
|
|
|
*/ |
84
|
|
|
public $force_charset = false; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var bool whether to change @import on content |
88
|
|
|
*/ |
89
|
|
|
public $expand_imports = true; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var int |
93
|
|
|
*/ |
94
|
|
|
public $css_linebreak_pos = 2048; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @var int|bool chmod of minified file. If false chmod not set |
98
|
|
|
*/ |
99
|
|
|
public $file_mode = 0664; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @var array schemes that will be ignored during normalization url |
103
|
|
|
*/ |
104
|
|
|
public $schemas = ['//', 'http://', 'https://', 'ftp://']; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @deprecated |
108
|
|
|
* @var bool do I need to compress the result html page. |
109
|
|
|
*/ |
110
|
|
|
public $compress_output = false; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @var array options for compressing output result |
114
|
|
|
* * extra - use more compact algorithm |
115
|
|
|
* * no-comments - cut all the html comments |
116
|
|
|
*/ |
117
|
|
|
public $compress_options = ['extra' => true]; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @var array |
121
|
|
|
*/ |
122
|
|
|
public $excludeBundles = []; |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @throws \rmrevin\yii\minify\Exception |
126
|
|
|
*/ |
127
|
9 |
|
public function init() |
128
|
|
|
{ |
129
|
9 |
|
parent::init(); |
130
|
|
|
|
131
|
9 |
|
$minify_path = $this->minify_path = (string)\Yii::getAlias($this->minify_path); |
132
|
9 |
|
if (!file_exists($minify_path)) { |
133
|
9 |
|
FileHelper::createDirectory($minify_path); |
134
|
9 |
|
} |
135
|
|
|
|
136
|
9 |
|
if (!is_readable($minify_path)) { |
137
|
|
|
throw new Exception('Directory for compressed assets is not readable.'); |
138
|
|
|
} |
139
|
|
|
|
140
|
9 |
|
if (!is_writable($minify_path)) { |
141
|
|
|
throw new Exception('Directory for compressed assets is not writable.'); |
142
|
|
|
} |
143
|
|
|
|
144
|
9 |
|
if (true === $this->enableMinify && (true === $this->minifyOutput || true === $this->compress_output)) { |
|
|
|
|
145
|
|
|
\Yii::$app->response->on(Response::EVENT_BEFORE_SEND, function (Event $Event) { |
146
|
|
|
/** @var Response $Response */ |
147
|
|
|
$Response = $Event->sender; |
148
|
|
|
|
149
|
|
|
if ($Response->format === Response::FORMAT_HTML) { |
150
|
|
|
if (!empty($Response->data)) { |
151
|
|
|
$Response->data = HtmlCompressor::compress($Response->data, $this->compress_options); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if (!empty($Response->content)) { |
155
|
|
|
$Response->content = HtmlCompressor::compress($Response->content, $this->compress_options); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
}); |
159
|
|
|
} |
160
|
9 |
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @inheritdoc |
164
|
|
|
*/ |
165
|
7 |
|
public function endBody() |
166
|
|
|
{ |
167
|
7 |
|
$this->trigger(self::EVENT_END_BODY); |
168
|
7 |
|
echo self::PH_BODY_END; |
169
|
|
|
|
170
|
7 |
View Code Duplication |
foreach (array_keys($this->assetBundles) as $bundle) { |
|
|
|
|
171
|
7 |
|
if (!in_array($bundle, $this->excludeBundles, true)) { |
172
|
7 |
|
$this->registerAssetFiles($bundle); |
173
|
7 |
|
} |
174
|
7 |
|
} |
175
|
|
|
|
176
|
7 |
|
if (true === $this->enableMinify) { |
177
|
7 |
|
(new components\CSS($this))->export(); |
178
|
7 |
|
(new components\JS($this))->export(); |
179
|
7 |
|
} |
180
|
|
|
|
181
|
7 |
View Code Duplication |
foreach (array_keys($this->assetBundles) as $bundle) { |
|
|
|
|
182
|
1 |
|
if (in_array($bundle, $this->excludeBundles, true)) { |
183
|
1 |
|
$this->registerAssetFiles($bundle); |
184
|
1 |
|
} |
185
|
7 |
|
} |
186
|
7 |
|
} |
187
|
|
|
} |
188
|
|
|
|
This property has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.