Completed
Push — master ( 371f09...65d63b )
by Revin
03:01
created

View::init()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 8

Importance

Changes 10
Bugs 0 Features 2
Metric Value
c 10
b 0
f 2
dl 0
loc 33
ccs 20
cts 20
cp 1
rs 5.3846
cc 8
eloc 17
nc 8
nop 0
crap 8
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 6
    /**
75
     * @var bool|string charset forcibly assign, otherwise will use all of the files found charset
76 6
     */
77
    public $force_charset = false;
78 6
79 6
    /**
80 6
     * @var bool whether to change @import on content
81 6
     */
82
    public $expand_imports = true;
83 6
84
    /**
85
     * @var int
86
     */
87 6
    public $css_linebreak_pos = 2048;
88
89
    /**
90
     * @var int|bool chmod of minified file. If false chmod not set
91 6
     */
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 6
     *   * extra - use more compact algorithm
107
     *   * no-comments - cut all the html comments
108
     */
109
    public $compress_options = ['extra' => true];
110
111 4
    /**
112
     * @throws \rmrevin\yii\minify\Exception
113 4
     */
114
    public function init()
115 4
    {
116 4
        parent::init();
117 4
118 4
        $minify_path = $this->minify_path = (string)\Yii::getAlias($this->minify_path);
119
        if (!file_exists($minify_path)) {
120 4
            FileHelper::createDirectory($minify_path);
121 4
        }
122 4
123 4
        if (!is_readable($minify_path)) {
124
            throw new Exception('Directory for compressed assets is not readable.');
125 4
        }
126 4
127 4
        if (!is_writable($minify_path)) {
128 4
            throw new Exception('Directory for compressed assets is not writable.');
129
        }
130 4
131 4
        if (true === $this->compress_output) {
132
            \Yii::$app->response->on(\yii\web\Response::EVENT_BEFORE_SEND, function (\yii\base\Event $Event) {
133 4
                /** @var \yii\web\Response $Response */
134 4
                $Response = $Event->sender;
135 4
                if ($Response->format === \yii\web\Response::FORMAT_HTML) {
136
                    if (!empty($Response->data)) {
137 4
                        $Response->data = HtmlCompressor::compress($Response->data, $this->compress_options);
138
                    }
139 4
140 4
                    if (!empty($Response->content)) {
141
                        $Response->content = HtmlCompressor::compress($Response->content, $this->compress_options);
0 ignored issues
show
Documentation Bug introduced by
It seems like \rmrevin\yii\minify\Html...this->compress_options) can also be of type false. However, the property $content is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
142
                    }
143
                }
144
            });
145
        }
146
    }
147
148
    /**
149
     * @inheritdoc
150
     */
151
    public function endPage($ajaxMode = false)
152
    {
153
        $this->trigger(self::EVENT_END_PAGE);
154
155
        $content = ob_get_clean();
156
        foreach (array_keys($this->assetBundles) as $bundle) {
157
            $this->registerAssetFiles($bundle);
158
        }
159
160
        if (true === $this->enableMinify) {
161
            (new components\CSS($this))->export();
162
            (new components\JS($this))->export();
163
        }
164
165
        echo strtr(
166
            $content,
167
            [
168
                self::PH_HEAD => $this->renderHeadHtml(),
169
                self::PH_BODY_BEGIN => $this->renderBodyBeginHtml(),
170
                self::PH_BODY_END => $this->renderBodyEndHtml($ajaxMode),
171
            ]
172
        );
173
174
        $this->clear();
175
    }
176
}
177