|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Enjoys\AssetsCollector\Content\Minify\Adapters; |
|
6
|
|
|
|
|
7
|
|
|
use Enjoys\AssetsCollector\Content\Minify\MinifyInterface; |
|
8
|
|
|
use Enjoys\Traits\Options; |
|
9
|
|
|
use tubalmartin\CssMin\Minifier as CSSmin; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class CssMinify |
|
13
|
|
|
* @package Enjoys\AssetsCollector\Content\Minify\Adapters |
|
14
|
|
|
*/ |
|
15
|
|
|
class CssMinify implements MinifyInterface |
|
16
|
|
|
{ |
|
17
|
|
|
use Options; |
|
18
|
|
|
|
|
19
|
|
|
private string $content = ''; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* CssMinify constructor. |
|
23
|
|
|
* @param array $minifyOptions |
|
24
|
|
|
*/ |
|
25
|
9 |
|
public function __construct(array $minifyOptions = []) |
|
26
|
|
|
{ |
|
27
|
9 |
|
$this->setOptions($minifyOptions); |
|
28
|
9 |
|
} |
|
29
|
|
|
|
|
30
|
4 |
|
public function setContent(string $content): void |
|
31
|
|
|
{ |
|
32
|
4 |
|
$this->content = $content; |
|
33
|
4 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return string |
|
37
|
|
|
*/ |
|
38
|
3 |
|
public function getContent(): string |
|
39
|
|
|
{ |
|
40
|
3 |
|
$compressor = new CSSMin(); |
|
41
|
3 |
|
$compressor->keepSourceMapComment((bool)$this->getOption('keepSourceMapComment', false)); |
|
42
|
|
|
// Remove important comments from output. |
|
43
|
3 |
|
$compressor->removeImportantComments((bool)$this->getOption('removeImportantComments', true)); |
|
44
|
|
|
// Split long lines in the output approximately every 1000 chars. |
|
45
|
3 |
|
$compressor->setLineBreakPosition((int)$this->getOption('setLineBreakPosition', 1000)); |
|
46
|
3 |
|
$compressor->setMaxExecutionTime((int)$this->getOption('setMaxExecutionTime', 60)); |
|
47
|
|
|
// Override any PHP configuration options before calling run() (optional) |
|
48
|
3 |
|
$compressor->setMemoryLimit((string)$this->getOption('setMemoryLimit', '256M')); |
|
49
|
3 |
|
$compressor->setPcreBacktrackLimit((int)$this->getOption('setPcreBacktrackLimit', 1000000)); |
|
50
|
3 |
|
$compressor->setPcreRecursionLimit((int)$this->getOption('setPcreRecursionLimit', 500000)); |
|
51
|
|
|
// Compress the CSS code! |
|
52
|
3 |
|
return $compressor->run($this->content); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|