1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staticka\Filters; |
4
|
|
|
|
5
|
|
|
use Staticka\Contracts\FilterContract; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Inline Minifier |
9
|
|
|
* |
10
|
|
|
* @package Staticka |
11
|
|
|
* @author Rougin Gutib <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class InlineMinifier implements FilterContract |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $tagname = ''; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Initializes the filter instance. |
22
|
|
|
* |
23
|
|
|
* @param string $tagname |
24
|
|
|
*/ |
25
|
6 |
|
public function __construct($tagname = '') |
26
|
|
|
{ |
27
|
6 |
|
$tagname !== '' && $this->tagname = $tagname; |
28
|
6 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Filters the specified code. |
32
|
|
|
* |
33
|
|
|
* @param string $code |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
6 |
|
public function filter($code) |
37
|
|
|
{ |
38
|
6 |
|
$elements = (array) $this->elements($code); |
39
|
|
|
|
40
|
6 |
|
foreach ((array) $elements as $element) |
41
|
|
|
{ |
42
|
6 |
|
$original = (string) $element->nodeValue; |
43
|
|
|
|
44
|
6 |
|
$minified = $this->minify($original); |
45
|
|
|
|
46
|
6 |
|
$minified = preg_replace('/\s+/', ' ', $minified); |
47
|
|
|
|
48
|
6 |
|
$minified = $this->minify($minified); |
49
|
|
|
|
50
|
6 |
|
$code = str_replace($original, $minified, $code); |
51
|
2 |
|
} |
52
|
|
|
|
53
|
6 |
|
return $code; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns elements by a tag name. |
58
|
|
|
* |
59
|
|
|
* @param string $code |
60
|
|
|
* @return \DOMNodeList |
61
|
|
|
*/ |
62
|
6 |
|
protected function elements($code) |
63
|
|
|
{ |
64
|
6 |
|
libxml_use_internal_errors(true); |
65
|
|
|
|
66
|
6 |
|
$doc = new \DOMDocument; |
67
|
|
|
|
68
|
6 |
|
$doc->loadHTML((string) $code); |
69
|
|
|
|
70
|
6 |
|
$tag = (string) $this->tagname; |
71
|
|
|
|
72
|
6 |
|
$items = $doc->getElementsByTagName($tag); |
73
|
|
|
|
74
|
6 |
|
$items = iterator_to_array($items); |
75
|
|
|
|
76
|
6 |
|
return count($items) > 0 ? $items : array(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Minifies the specified code. |
81
|
|
|
* |
82
|
|
|
* @param string $code |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
6 |
|
protected function minify($code) |
86
|
|
|
{ |
87
|
6 |
|
$pattern = (string) '!/\*[^*]*\*+([^/][^*]*\*+)*/!'; |
88
|
|
|
|
89
|
6 |
|
$minified = preg_replace('/^\\s+/m', '', $code); |
90
|
|
|
|
91
|
6 |
|
$minified = preg_replace($pattern, '', $minified); |
92
|
|
|
|
93
|
6 |
|
$minified = preg_replace('/( )?}( )?/', '}', $minified); |
94
|
|
|
|
95
|
6 |
|
$minified = preg_replace('/( )?{( )?/', '{', $minified); |
96
|
|
|
|
97
|
6 |
|
$minified = preg_replace('/( )?:( )?/', ':', $minified); |
98
|
|
|
|
99
|
6 |
|
$minified = preg_replace('/( )?;( )?/', ';', $minified); |
100
|
|
|
|
101
|
6 |
|
$minified = preg_replace('/( )?,( )?/', ',', $minified); |
102
|
|
|
|
103
|
6 |
|
return $minified; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|