|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JK\Sam\Filter\Minify; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use JK\Sam\Filter\Filter; |
|
7
|
|
|
use MatthiasMullie\Minify\CSS; |
|
8
|
|
|
use MatthiasMullie\Minify\JS; |
|
9
|
|
|
use SplFileInfo; |
|
10
|
|
|
|
|
11
|
|
|
class MinifyFilter extends Filter |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @param SplFileInfo[] $sources |
|
15
|
|
|
* @param SplFileInfo[] $destinations |
|
16
|
|
|
* @return SplFileInfo[] |
|
17
|
|
|
*/ |
|
18
|
|
|
public function run(array $sources, array $destinations) |
|
19
|
|
|
{ |
|
20
|
|
|
$cssMinifier = new CSS(); |
|
21
|
|
|
$jsMinifier = new JS(); |
|
22
|
|
|
$hasJs = false; |
|
23
|
|
|
$hasCss = false; |
|
24
|
|
|
|
|
25
|
|
|
// add source files to the minifier |
|
26
|
|
|
foreach ($sources as $source) { |
|
27
|
|
|
|
|
28
|
|
|
if ($source->getExtension() == 'js') { |
|
29
|
|
|
$this->addNotification('Add '.$source.' to js minification'); |
|
30
|
|
|
$jsMinifier->add($source); |
|
31
|
|
|
$hasJs = true; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
if ($source->getExtension() == 'css') { |
|
35
|
|
|
$this->addNotification('add '.$source.' to css minification'); |
|
36
|
|
|
$cssMinifier->add($source); |
|
37
|
|
|
$hasCss = true; |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
$cssMinPath = $this->getCacheDir().'minified.css'; |
|
41
|
|
|
$jsMinPath = $this->getCacheDir().'minified.js'; |
|
42
|
|
|
$updatedSources = []; |
|
43
|
|
|
|
|
44
|
|
|
// js minification if required |
|
45
|
|
|
if ($hasJs) { |
|
46
|
|
|
$this->addNotification($jsMinPath.' js minification'); |
|
47
|
|
|
$jsMinifier->minify($jsMinPath); |
|
48
|
|
|
$updatedSources[] = $jsMinPath; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// css minification if required |
|
52
|
|
|
if ($hasCss) { |
|
53
|
|
|
$this->addNotification($cssMinPath.' css minification'); |
|
54
|
|
|
// avoid css rewriting by not passing an argument to the minify() method and dump the content into the file |
|
55
|
|
|
// ourselves |
|
56
|
|
|
$content = $cssMinifier->minify(); |
|
57
|
|
|
file_put_contents($cssMinPath, $content); |
|
58
|
|
|
|
|
59
|
|
|
$updatedSources[] = $cssMinPath; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $updatedSources; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @throws Exception |
|
67
|
|
|
*/ |
|
68
|
|
|
public function checkRequirements() |
|
69
|
|
|
{ |
|
70
|
|
|
if (!class_exists('MatthiasMullie\Minify\CSS')) { |
|
71
|
|
|
throw new Exception( |
|
72
|
|
|
'MatthiasMullie\Minify\CSS should exists. '. |
|
73
|
|
|
'Install the MatthiasMullie\Minify library (composer require matthiasmullie/minify)' |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Return the file extensions supported by this filter. |
|
80
|
|
|
* |
|
81
|
|
|
* @return string[] |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getSupportedExtensions() |
|
84
|
|
|
{ |
|
85
|
|
|
return [ |
|
86
|
|
|
'css', |
|
87
|
|
|
'js', |
|
88
|
|
|
]; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|