1 | <?php |
||
2 | /** |
||
3 | * Minify plugin for Craft CMS |
||
4 | * |
||
5 | * @link https://nystudio107.com/ |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
6 | * @copyright Copyright (c) nystudio107 |
||
0 ignored issues
–
show
|
|||
7 | * @license MIT License https://opensource.org/licenses/MIT |
||
8 | */ |
||
0 ignored issues
–
show
|
|||
9 | |||
10 | namespace nystudio107\minify\services; |
||
11 | |||
12 | use Craft; |
||
13 | use craft\base\Component; |
||
14 | use JSMin\JSMin; |
||
15 | use Minify_CSSmin; |
||
16 | use Minify_HTML; |
||
17 | use nystudio107\minify\Minify; |
||
18 | |||
19 | /** |
||
20 | * Minify service |
||
21 | * |
||
22 | * @author nystudio107 |
||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||
23 | * @package Minify |
||
0 ignored issues
–
show
|
|||
24 | * @since 1.2.0 |
||
0 ignored issues
–
show
|
|||
25 | */ |
||
0 ignored issues
–
show
|
|||
26 | class MinifyService extends Component |
||
27 | { |
||
28 | private bool $shouldMinify = true; |
||
0 ignored issues
–
show
|
|||
29 | |||
30 | /** |
||
0 ignored issues
–
show
|
|||
31 | * @inheritdoc |
||
32 | */ |
||
0 ignored issues
–
show
|
|||
33 | public function init(): void |
||
34 | { |
||
35 | if (Minify::$plugin->getSettings()->disableTemplateMinifying) { |
||
36 | $this->shouldMinify = false; |
||
37 | } |
||
38 | |||
39 | if (Craft::$app->getConfig()->getGeneral()->devMode |
||
40 | && Minify::$plugin->getSettings()->disableDevModeMinifying) { |
||
0 ignored issues
–
show
|
|||
41 | $this->shouldMinify = false; |
||
42 | } |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Minify all the things |
||
47 | * |
||
48 | * @param string $htmlText |
||
0 ignored issues
–
show
|
|||
49 | * |
||
50 | * @return string |
||
51 | */ |
||
52 | public function minify(string $htmlText = ""): string |
||
53 | { |
||
54 | if ($this->shouldMinify) { |
||
55 | $options = [ |
||
56 | 'cssMinifier' => '\Minify_CSSmin::minify', |
||
57 | 'jsMinifier' => '\JSMin\JSMin::minify', |
||
58 | ]; |
||
59 | $htmlText = Minify_HTML::minify($htmlText, $options); |
||
60 | } |
||
61 | |||
62 | return $htmlText; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Minify the passed in HTML |
||
67 | * |
||
68 | * @param string $htmlText |
||
0 ignored issues
–
show
|
|||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | public function htmlMin(string $htmlText = ""): string |
||
73 | { |
||
74 | if ($this->shouldMinify) { |
||
75 | $htmlText = Minify_HTML::minify($htmlText); |
||
76 | } |
||
77 | |||
78 | return $htmlText; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Minify the passed in CSS |
||
83 | * |
||
84 | * @param string $cssText |
||
0 ignored issues
–
show
|
|||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | public function cssMin(string $cssText = ""): string |
||
89 | { |
||
90 | if ($this->shouldMinify) { |
||
91 | $cssText = Minify_CSSmin::minify($cssText); |
||
92 | } |
||
93 | |||
94 | return $cssText; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Minify the passed in JS |
||
99 | * |
||
100 | * @param string $jsText |
||
0 ignored issues
–
show
|
|||
101 | * |
||
102 | * @return string |
||
103 | */ |
||
104 | public function jsMin(string $jsText = ""): string |
||
105 | { |
||
106 | if ($this->shouldMinify) { |
||
107 | $jsText = JSMin::minify($jsText); |
||
108 | } |
||
109 | |||
110 | return $jsText; |
||
111 | } |
||
112 | } |
||
113 |