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