Test Setup Failed
Push — master ( 7b9e96...318657 )
by Jaimin
107:29 queued 67:29
created

View   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 350
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 30
lcom 1
cbo 9
dl 0
loc 350
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A endBody() 0 14 3
F init() 0 77 27
1
<?php
2
/**
3
 * View.php
4
 * @author Revin Roman
5
 * @link https://processfast.ru
6
 */
7
8
namespace processfast\yii\minify;
9
10
use yii\base\Event;
11
use yii\helpers\FileHelper;
12
use yii\web\AssetBundle;
13
use yii\web\Response;
14
15
16
17
/**
18
 * Class View
19
 * @package processfast\yii\minify
20
 */
21
class View extends \yii\web\View
22
{
23
24
    /**
25
     * @var bool
26
     */
27
    public $enableMinify = true;
28
29
    /**
30
     * @var string filemtime or sha1
31
     */
32
    public $fileCheckAlgorithm = 'sha1';
33
34
    /**
35
     * @var bool
36
     */
37
    public $concatCss = true;
38
39
    /**
40
     * @var bool
41
     */
42
    public $minifyCss = true;
43
44
    /**
45
     * @var bool
46
     */
47
    public $concatJs = true;
48
49
    /**
50
     * @var bool
51
     */
52
    public $minifyJs = true;
53
54
    /**
55
     * @var bool
56
     */
57
    public $minifyOutput = false;
58
59
    /**
60
     * @var bool
61
     */
62
    public $removeComments = true;
63
64
    /**
65
     * @deprecated
66
     * @var string path alias to web base (in url)
67
     */
68
    public $web_path = '@web';
69
70
    /**
71
     * @var string path alias to web base (in url)
72
     */
73
    public $webPath;
74
75
    /**
76
     * @deprecated
77
     * @var string path alias to web base (absolute)
78
     */
79
    public $base_path = '@webroot';
80
81
    /**
82
     * @var string path alias to web base (absolute)
83
     */
84
    public $basePath;
85
86
    /**
87
     * @deprecated
88
     * @var string path alias to save minify result
89
     */
90
    public $minify_path = '@webroot/minify';
91
92
    /**
93
     * @var string path alias to save minify result
94
     */
95
    public $minifyPath;
96
97
    /**
98
     * @deprecated
99
     * @var array positions of js files to be minified
100
     */
101
    public $js_position = [self::POS_END, self::POS_HEAD];
102
103
    /**
104
     * @var array positions of js files to be minified
105
     */
106
    public $jsPosition;
107
108
    /**
109
     * @var array options of minified js files
110
     */
111
    public $jsOptions = [];
112
113
    /**
114
     * @deprecated
115
     * @var bool|string charset forcibly assign, otherwise will use all of the files found charset
116
     */
117
    public $force_charset = false;
118
119
    /**
120
     * @var bool|string charset forcibly assign, otherwise will use all of the files found charset
121
     */
122
    public $forceCharset;
123
124
    /**
125
     * @deprecated
126
     * @var bool whether to change @import on content
127
     */
128
    public $expand_imports = true;
129
130
    /**
131
     * @var bool whether to change @import on content
132
     */
133
    public $expandImports;
134
135
    /**
136
     * @deprecated
137
     * @var int
138
     */
139
    public $css_linebreak_pos = 2048;
140
141
    /**
142
     * @var int
143
     */
144
    public $cssLinebreakPos;
145
146
    /**
147
     * @deprecated
148
     * @var int|bool chmod of minified file. If false chmod not set
149
     */
150
    public $file_mode = 0664;
151
152
    /**
153
     * @var int|bool chmod of minified file. If false chmod not set
154
     */
155
    public $fileMode;
156
157
    /**
158
     * @var array schemes that will be ignored during normalization url
159
     */
160
    public $schemas = ['//', 'http://', 'https://', 'ftp://'];
161
162
    /**
163
     * @deprecated
164
     * @var bool do I need to compress the result html page.
165
     */
166
    public $compress_output = false;
167
168
    /**
169
     * @deprecated
170
     * @var array options for compressing output result
171
     *   * extra - use more compact algorithm
172
     *   * no-comments - cut all the html comments
173
     */
174
    public $compress_options = ['extra' => true];
175
176
    /**
177
     * @var array options for compressing output result
178
     *   * extra - use more compact algorithm
179
     *   * no-comments - cut all the html comments
180
     */
181
    public $compressOptions;
182
183
    /**
184
     * @var array
185
     */
186
    public $excludeBundles = [];
187
188
    /**
189
     * @var array
190
     */
191
    public $excludeFiles = [];
192
193
    /*
194
     *  Updated params by Jaimin MosLake to give more functionality
195
     */
196
197
    /**
198
     * @var boolean
199
     * whether you want to use S3Bucket or not
200
     * By default it will be false
201
     */
202
    public $S3Upload = false ;
203
204
    /**
205
     * @var boolean
206
     * Name of awsBucket
207
     */
208
    public $awsBucket = null ;
209
210
    /**
211
     * @var boolean
212
     * It is for linking Resource folder to asset files
213
     * if Resources like images above one folder it should be "../" if two folders above "../../"
214
     * You want to load all the images from s3 now you have images folder in root and you have dev , qa , prod folder
215
     * and css and js inside those folder now you have to link images in those css file.
216
     * You have to use this option to do that.
217
     */
218
    public $assetsFolderPathPatch = null ;
219
220
    /*
221
     * boolean
222
     * backend checke will help take asset from root/minify folder for backedn instead of root/backend/minifiy
223
     * If backend and frontend has same assets and you want to use same location to store asset
224
     * you can make thi true. By this it will use root/minify other then root/backedn/minify
225
     * to copy files to S3
226
     */
227
    public $backendCheck = false ;
228
229
    /*
230
     * Folder name where minified files will be kept
231
     * Here i have devided it will be used when $backendCheck is true
232
     * as if we are doing from web from backend / frontend to have same file hashes
233
     * as I am using same assets folder in root for backend / frontend as to have files from same assets folder
234
     */
235
    public $folderName = 'minify' ;
236
237
    /*
238
     * will be used at _getSummaryFilesHash will fix path to have same hash value as frontend or backend when files generated from console.
239
     * At console level this will be used as when generating from console path will be different so some adjustment path should be decalared t make path same as
240
     * of running in web browser as console has path from console folder script
241
     *
242
     * so when you generate from console  make modifyPath true
243
     * and modifyPathData regarding your assets folder to console folder
244
     */
245
    public $modifyPath = false  ;
246
    public $modifyPathData = "" ;
247
248
249
    /*
250
     * It helps if you want to add prefix to any file as it will mostly create file name
251
     * as {prefix}-all-in-one-{HASH}.{js/css}
252
     * so if you want to give a prefix for certain layout
253
     * then you can do it by this option.
254
     * Pass layout name as array key and pass prefix name as array value
255
     * ex :  for main layout if you want newmain prefix
256
     * you have to pass array like ["main"=>"newmain"]
257
     * if you do not wont prefix do not do anything just live it a blank array
258
     */
259
    public $layoutPrefixArray = [] ;
260
261
    /*
262
     * Use layoutPrefixArray option for css true/false
263
     */
264
    public $layoutPrefixCss = false ;
265
266
    /*
267
     * Use layoutPrefixArray option for Js true/false
268
     */
269
    public $layoutPrefixJS = false ;
270
271
272
    /**
273
     * @throws \processfast\yii\minify\Exception
274
     */
275
    public function init()
276
    {
277
        parent::init();
278
279
        $this->webPath = empty($this->webPath) ? $this->web_path : $this->webPath;
0 ignored issues
show
Deprecated Code introduced by
The property processfast\yii\minify\View::$web_path has been deprecated.

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.

Loading history...
280
        $this->basePath = empty($this->basePath) ? $this->base_path : $this->basePath;
0 ignored issues
show
Deprecated Code introduced by
The property processfast\yii\minify\View::$base_path has been deprecated.

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.

Loading history...
281
        $this->minifyPath = empty($this->minifyPath) ? $this->minify_path : $this->minifyPath;
0 ignored issues
show
Deprecated Code introduced by
The property processfast\yii\minify\View::$minify_path has been deprecated.

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.

Loading history...
282
        $this->jsPosition = empty($this->jsPosition) ? $this->js_position : $this->jsPosition;
0 ignored issues
show
Deprecated Code introduced by
The property processfast\yii\minify\View::$js_position has been deprecated.

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.

Loading history...
283
        $this->forceCharset = empty($this->forceCharset) ? $this->force_charset : $this->forceCharset;
0 ignored issues
show
Deprecated Code introduced by
The property processfast\yii\minify\View::$force_charset has been deprecated.

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.

Loading history...
284
        $this->expandImports = empty($this->expandImports) ? $this->expand_imports : $this->expandImports;
0 ignored issues
show
Deprecated Code introduced by
The property processfast\yii\minify\View::$expand_imports has been deprecated.

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.

Loading history...
285
        $this->cssLinebreakPos = empty($this->cssLinebreakPos) ? $this->css_linebreak_pos : $this->cssLinebreakPos;
0 ignored issues
show
Deprecated Code introduced by
The property processfast\yii\minify\View::$css_linebreak_pos has been deprecated.

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.

Loading history...
286
        $this->fileMode = empty($this->fileMode) ? $this->file_mode : $this->fileMode;
0 ignored issues
show
Deprecated Code introduced by
The property processfast\yii\minify\View::$file_mode has been deprecated.

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.

Loading history...
287
        $this->compressOptions = empty($this->compressOptions) ? $this->compress_options : $this->compressOptions;
0 ignored issues
show
Deprecated Code introduced by
The property processfast\yii\minify\View::$compress_options has been deprecated.

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.

Loading history...
288
289
        if( $this->backendCheck )
290
        {
291
            $appId = \Yii::$app->id ;
292
            if( $appId == "app-frontend" )
293
            {
294
                $this->minifyPath = $this->minifyPath."/".$this->folderName;
295
            }
296
            else if( $appId == "app-backend" )
297
            {
298
                $this->minifyPath = $this->minifyPath."/../".$this->folderName;
299
            }
300
        }
301
302
        $excludeBundles = $this->excludeBundles;
303
        if (!empty($excludeBundles)) {
304
            foreach ($excludeBundles as $bundle) {
305
                if (!class_exists($bundle)) {
306
                    continue;
307
                }
308
309
                /** @var AssetBundle $Bundle */
310
                $Bundle = new $bundle;
311
312
                if (!empty($Bundle->css)) {
313
                    $this->excludeFiles = array_merge($this->excludeFiles, $Bundle->css);
314
                }
315
316
                if (!empty($Bundle->js)) {
317
                    $this->excludeFiles = array_merge($this->excludeFiles, $Bundle->js);
318
                }
319
            }
320
        }
321
322
        $minify_path = $this->minifyPath = (string)\Yii::getAlias($this->minifyPath);
323
        if (!file_exists($minify_path)) {
324
            FileHelper::createDirectory($minify_path);
325
        }
326
327
        if (!is_readable($minify_path)) {
328
            throw new Exception('Directory for compressed assets is not readable.');
329
        }
330
331
        if (!is_writable($minify_path)) {
332
            throw new Exception('Directory for compressed assets is not writable.');
333
        }
334
335
        if (true === $this->enableMinify && (true === $this->minifyOutput || true === $this->compress_output)) {
0 ignored issues
show
Deprecated Code introduced by
The property processfast\yii\minify\View::$compress_output has been deprecated.

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.

Loading history...
336
            \Yii::$app->response->on(Response::EVENT_BEFORE_SEND, function (Event $Event) {
337
                /** @var Response $Response */
338
                $Response = $Event->sender;
339
340
                if ($Response->format === Response::FORMAT_HTML) {
341
                    if (!empty($Response->data)) {
342
                        $Response->data = HtmlCompressor::compress($Response->data, $this->compressOptions);
343
                    }
344
345
                    if (!empty($Response->content)) {
346
                        $Response->content = HtmlCompressor::compress($Response->content, $this->compressOptions);
347
                    }
348
                }
349
            });
350
        }
351
    }
352
353
    /**
354
     * @inheritdoc
355
     */
356
    public function endBody()
357
    {
358
        $this->trigger(self::EVENT_END_BODY);
359
        echo self::PH_BODY_END;
360
361
        foreach (array_keys($this->assetBundles) as $bundle) {
362
            $this->registerAssetFiles($bundle);
363
        }
364
365
        if (true === $this->enableMinify) {
366
            (new components\CSS($this))->export();
367
            (new components\JS($this))->export();
368
        }
369
    }
370
}
371