1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ArjanSchouten\HtmlMinifier; |
4
|
|
|
|
5
|
|
|
use ArjanSchouten\HtmlMinifier\Minifiers\Html\AttributeQuoteMinifier; |
6
|
|
|
use ArjanSchouten\HtmlMinifier\Minifiers\Html\BooleanAttributeMinifier; |
7
|
|
|
use ArjanSchouten\HtmlMinifier\Minifiers\Html\CommentMinifier; |
8
|
|
|
use ArjanSchouten\HtmlMinifier\Minifiers\Html\EmptyAttributeMinifier; |
9
|
|
|
use ArjanSchouten\HtmlMinifier\Minifiers\Html\JavascriptEventsMinifier; |
10
|
|
|
use ArjanSchouten\HtmlMinifier\Minifiers\Html\OptionalElementMinifier; |
11
|
|
|
use ArjanSchouten\HtmlMinifier\Minifiers\Html\RedundantAttributeMinifier; |
12
|
|
|
use ArjanSchouten\HtmlMinifier\Minifiers\Html\WhitespaceMinifier; |
13
|
|
|
use ArjanSchouten\HtmlMinifier\Minifiers\MinifierInterface; |
14
|
|
|
use ArjanSchouten\HtmlMinifier\Placeholders\CommentPlaceholder; |
15
|
|
|
use ArjanSchouten\HtmlMinifier\Placeholders\Php\PhpPlaceholder; |
16
|
|
|
use ArjanSchouten\HtmlMinifier\Placeholders\PlaceholderInterface; |
17
|
|
|
use ArjanSchouten\HtmlMinifier\Placeholders\WhitespacePlaceholder; |
18
|
|
|
use InvalidArgumentException; |
19
|
|
|
|
20
|
|
|
class Minify |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var \ArjanSchouten\HtmlMinifier\Placeholders\PlaceholderInterface[] |
24
|
|
|
*/ |
25
|
|
|
protected $placeholders = [ |
26
|
|
|
PhpPlaceholder::class, |
27
|
|
|
CommentPlaceholder::class, |
28
|
|
|
WhitespacePlaceholder::class, |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \ArjanSchouten\HtmlMinifier\Minifiers\MinifierInterface[] |
33
|
|
|
*/ |
34
|
|
|
protected $minifiers = [ |
35
|
|
|
CommentMinifier::class, |
36
|
|
|
WhitespaceMinifier::class, |
37
|
|
|
AttributeQuoteMinifier::class, |
38
|
|
|
EmptyAttributeMinifier::class, |
39
|
|
|
OptionalElementMinifier::class, |
40
|
|
|
BooleanAttributeMinifier::class, |
41
|
|
|
JavascriptEventsMinifier::class, |
42
|
|
|
RedundantAttributeMinifier::class, |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param \ArjanSchouten\HtmlMinifier\MinifyContext $context |
47
|
|
|
* @param array $options |
48
|
|
|
* |
49
|
|
|
* @return \ArjanSchouten\HtmlMinifier\MinifyContext |
50
|
|
|
*/ |
51
|
3 |
|
public function run(MinifyContext $context, $options = []) |
52
|
|
|
{ |
53
|
3 |
|
$context->addMinificationStep($context->getContents(), 'Initial step'); |
54
|
3 |
|
$context = $this->placeholders($context); |
55
|
3 |
|
$context = $this->minifiers($context, $options); |
56
|
|
|
|
57
|
3 |
|
return $this->restore($context); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param \ArjanSchouten\HtmlMinifier\MinifyContext $context |
62
|
|
|
* |
63
|
|
|
* @return \ArjanSchouten\HtmlMinifier\MinifyContext |
64
|
|
|
*/ |
65
|
3 |
|
protected function placeholders(MinifyContext $context) |
66
|
|
|
{ |
67
|
3 |
|
foreach ($this->placeholders as $placeholder) { |
68
|
3 |
|
$placeholder = new $placeholder(); |
69
|
3 |
|
$context = $placeholder->process($context); |
70
|
|
|
} |
71
|
|
|
|
72
|
3 |
|
return $context; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param \ArjanSchouten\HtmlMinifier\MinifyContext $context |
77
|
|
|
* @param array $options |
78
|
|
|
* |
79
|
|
|
* @return \ArjanSchouten\HtmlMinifier\MinifyContext |
80
|
|
|
*/ |
81
|
3 |
|
protected function minifiers(MinifyContext $context, $options = []) |
82
|
|
|
{ |
83
|
3 |
|
foreach ($this->minifiers as $minifier) { |
84
|
3 |
|
$minifier = new $minifier(); |
85
|
|
|
|
86
|
3 |
|
$provides = $minifier->provides(); |
87
|
3 |
|
if ($this->runAll($options) || $this->isOptionSet($provides, $options)) { |
88
|
3 |
|
$context = $minifier->process($context); |
89
|
3 |
|
$context->addMinificationStep($context->getContents(), $this->getClassName($minifier).'|'.$minifier->provides()); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
3 |
|
return $context; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Checks if all minifiers should be runned. |
98
|
|
|
* |
99
|
|
|
* @param array $options |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
3 |
|
protected function runAll($options = []) |
104
|
|
|
{ |
105
|
3 |
|
return isset($options[Options::ALL]) && $options[Options::ALL]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Check whether an option is set in the options aray. |
110
|
|
|
* |
111
|
|
|
* @param string $provides |
112
|
|
|
* @param array $options |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
2 |
|
protected function isOptionSet($provides, $options = []) |
117
|
|
|
{ |
118
|
2 |
|
if (isset($options[$provides]) && $options[$provides] === true) { |
119
|
2 |
|
return true; |
120
|
2 |
|
} elseif (!isset($options[$provides]) && Options::options()[$provides]->isDefault()) { |
121
|
2 |
|
return true; |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Restore placeholders with their original content. |
129
|
|
|
* |
130
|
|
|
* @param \ArjanSchouten\HtmlMinifier\MinifyContext $context |
131
|
|
|
* |
132
|
|
|
* @return \ArjanSchouten\HtmlMinifier\MinifyContext |
133
|
|
|
*/ |
134
|
3 |
|
protected function restore(MinifyContext $context) |
135
|
|
|
{ |
136
|
3 |
|
$withoutPlaceholders = $context->getPlaceholderContainer()->restorePlaceholders($context->getContents()); |
137
|
|
|
|
138
|
3 |
|
return $context->setContents($withoutPlaceholders); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return \ArjanSchouten\HtmlMinifier\Placeholders\PlaceholderInterface[] |
143
|
|
|
*/ |
144
|
1 |
|
public function getPlaceholders() |
145
|
|
|
{ |
146
|
1 |
|
return $this->placeholders; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Add a placeholder strategy to the registered placeholders. |
151
|
|
|
* |
152
|
|
|
* @param string $placeholder |
153
|
|
|
* |
154
|
|
|
* @return \ArjanSchouten\HtmlMinifier\Placeholders\PlaceholderInterface[] |
|
|
|
|
155
|
|
|
*/ |
156
|
1 |
View Code Duplication |
public function addPlaceholder($placeholder) |
|
|
|
|
157
|
|
|
{ |
158
|
1 |
|
if (!isset(class_implements($placeholder)[PlaceholderInterface::class])) { |
159
|
1 |
|
throw new InvalidArgumentException('The class ['.$placeholder.'] should be a member of the ['.PlaceholderInterface::class.']'); |
160
|
1 |
|
} elseif (in_array($placeholder, $this->placeholders)) { |
161
|
|
|
throw new InvalidArgumentException('The placeholder ['.$placeholder.'] is already added to the minifier!'); |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
$this->placeholders[] = $placeholder; |
165
|
|
|
|
166
|
1 |
|
return $this->placeholders; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return \ArjanSchouten\HtmlMinifier\Minifiers\MinifierInterface[] |
171
|
|
|
*/ |
172
|
1 |
|
public function getMinifiers() |
173
|
|
|
{ |
174
|
1 |
|
return $this->minifiers; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Add a placeholder strategy to the registered placeholders. |
179
|
|
|
* |
180
|
|
|
* @param string $minifier |
181
|
|
|
* |
182
|
|
|
* @return \ArjanSchouten\HtmlMinifier\Minifiers\MinifierInterface[] |
|
|
|
|
183
|
|
|
*/ |
184
|
1 |
View Code Duplication |
public function addMinifier($minifier) |
|
|
|
|
185
|
|
|
{ |
186
|
1 |
|
if (!isset(class_implements($minifier)[MinifierInterface::class])) { |
187
|
|
|
throw new InvalidArgumentException('The class ['.$minifier.'] should be a member of the ['.MinifierInterface::class.']'); |
188
|
1 |
|
} elseif (in_array($minifier, $this->minifiers)) { |
189
|
|
|
throw new InvalidArgumentException('The minifier ['.$minifier.'] is already added to the minifier!'); |
190
|
|
|
} |
191
|
|
|
|
192
|
1 |
|
$this->minifiers[] = $minifier; |
193
|
|
|
|
194
|
1 |
|
return $this->minifiers; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Get the classname without the namespace. |
199
|
|
|
* |
200
|
|
|
* @param $object |
201
|
|
|
* |
202
|
|
|
* @return string |
203
|
|
|
*/ |
204
|
3 |
|
private function getClassName($object) |
205
|
|
|
{ |
206
|
3 |
|
$class = get_class($object); |
207
|
3 |
|
if (($index = strrpos($class, '\\'))) { |
208
|
3 |
|
$fixForTrailingBackslash = 1; |
209
|
|
|
|
210
|
3 |
|
return substr($class, $index + $fixForTrailingBackslash); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $class; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.