1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace voku\twig; |
6
|
|
|
|
7
|
|
|
use Twig\Environment; |
8
|
|
|
use Twig\Extension\AbstractExtension; |
9
|
|
|
use Twig\TwigFilter; |
10
|
|
|
use Twig\TwigFunction; |
11
|
|
|
use voku\cache\Cache; |
12
|
|
|
use voku\helper\HtmlMin; |
13
|
|
|
|
14
|
|
|
class MinifyHtmlExtension extends AbstractExtension |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $options = [ |
20
|
|
|
'is_safe' => ['html'], |
21
|
|
|
'needs_environment' => true, |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var callable |
26
|
|
|
*/ |
27
|
|
|
private $callable; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var HtmlMin |
31
|
|
|
*/ |
32
|
|
|
private $minifier; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
private $forceCompression = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* MinifyHtmlExtension constructor. |
41
|
|
|
* |
42
|
|
|
* @param HtmlMin $htmlMin |
43
|
|
|
* @param bool $forceCompression Default: false. Forces compression regardless of Twig's debug setting. |
44
|
|
|
*/ |
45
|
9 |
|
public function __construct(HtmlMin $htmlMin, bool $forceCompression = false) |
46
|
|
|
{ |
47
|
9 |
|
$this->forceCompression = $forceCompression; |
48
|
9 |
|
$this->minifier = $htmlMin; |
49
|
9 |
|
$this->callable = [$this, 'compress']; |
50
|
9 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Environment $twig |
54
|
|
|
* @param string $html |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
9 |
|
public function compress(Environment $twig, $html) |
59
|
|
|
{ |
60
|
9 |
|
if ($this->isCompressionActive($twig)) { |
61
|
6 |
|
static $cache = null; |
62
|
6 |
|
if ($cache === null) { |
63
|
1 |
|
$cache = new Cache(null, null, false); |
64
|
|
|
} |
65
|
6 |
|
$cacheKey = 'HtmlMin::hash' . \md5($html); |
66
|
|
|
|
67
|
|
|
if ( |
68
|
6 |
|
$cache->getCacheIsReady() === true |
69
|
|
|
&& |
70
|
6 |
|
$cache->existsItem($cacheKey) === true |
71
|
|
|
) { |
72
|
5 |
|
return $cache->getItem($cacheKey); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
$html = $this->minifier->minify($html); |
76
|
|
|
|
77
|
1 |
|
if ($cache->getCacheIsReady() === true) { |
78
|
1 |
|
$cache->setItem($cacheKey, $html, 3600); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
return $html; |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
return $html; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection */ |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
6 |
|
public function getFilters(): array |
93
|
|
|
{ |
94
|
|
|
return [ |
95
|
6 |
|
new TwigFilter('htmlcompress', $this->callable, $this->options), |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection */ |
100
|
6 |
|
public function getFunctions(): array |
101
|
|
|
{ |
102
|
|
|
return [ |
103
|
6 |
|
new TwigFunction('htmlcompress', $this->callable, $this->options), |
104
|
|
|
]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection */ |
108
|
6 |
|
public function getTokenParsers(): array |
109
|
|
|
{ |
110
|
|
|
return [ |
111
|
6 |
|
new MinifyHtmlTokenParser(), |
112
|
|
|
]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param Environment $twig |
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
9 |
|
public function isCompressionActive(Environment $twig): bool |
121
|
|
|
{ |
122
|
9 |
|
return $this->forceCompression |
123
|
|
|
|| |
124
|
9 |
|
!$twig->isDebug(); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|