|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
|
3
|
|
|
// __________ __ ________ __________ |
|
4
|
|
|
// \______ \ |__ ______ / _____/ ____ _____ ______\______ \ _______ ___ |
|
5
|
|
|
// | ___/ | \\____ \/ \ ____/ __ \\__ \\_ __ \ | _// _ \ \/ / |
|
6
|
|
|
// | | | Y \ |_> > \_\ \ ___/ / __ \| | \/ | ( <_> > < |
|
7
|
|
|
// |____| |___| / __/ \______ /\___ >____ /__| |______ /\____/__/\_ \ |
|
8
|
|
|
// \/|__| \/ \/ \/ \/ \/ |
|
9
|
|
|
// ----------------------------------------------------------------------------- |
|
10
|
|
|
// Designed and Developed by Brad Jones <brad @="bjc.id.au" /> |
|
11
|
|
|
// ----------------------------------------------------------------------------- |
|
12
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
|
13
|
|
|
|
|
14
|
|
|
namespace Gears\Asset\Minifiers; |
|
15
|
|
|
|
|
16
|
|
|
use SplFileInfo; |
|
17
|
|
|
use Gears\String\Str; |
|
18
|
|
|
use Gears\Asset\Contracts\Minifier; |
|
19
|
|
|
|
|
20
|
|
|
abstract class Base implements Minifier |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* This contains the source code to be minfied. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $source; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* We represent the source file that needs to be minified. |
|
31
|
|
|
* |
|
32
|
|
|
* @var SplFileInfo |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $file; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct(SplFileInfo $file, string $source) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->file = $file; |
|
39
|
|
|
$this->source = $source; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function minify(): string |
|
43
|
|
|
{ |
|
44
|
|
|
$min = $this->lookForPreMinifiedAsset(); |
|
45
|
|
|
|
|
46
|
|
|
if ($min === false) |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->mini(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $min; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* This must be defined in any child classes. |
|
56
|
|
|
* |
|
57
|
|
|
* @return string The minfied source code. |
|
58
|
|
|
*/ |
|
59
|
|
|
abstract protected function mini(): string; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* If we can find a pre-minified version of the file lets use that, |
|
63
|
|
|
* no point doing more work than we have to. Plus the vendor supplied |
|
64
|
|
|
* minified versions will probably be better optimised. |
|
65
|
|
|
* |
|
66
|
|
|
* @return string|bool Either preminified source code or false. |
|
67
|
|
|
*/ |
|
68
|
|
|
private function lookForPreMinifiedAsset() |
|
69
|
|
|
{ |
|
70
|
|
|
$min_path = (string)Str::s($this->file->getRealPath())->replace |
|
71
|
|
|
( |
|
72
|
|
|
'.'.$this->file->getExtension(), |
|
73
|
|
|
'.min.'.$this->file->getExtension() |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
if (!file_exists($min_path)) return false; |
|
77
|
|
|
|
|
78
|
|
|
return file_get_contents($min_path); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|