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\Compilers; |
15
|
|
|
|
16
|
|
|
use SplFileInfo; |
17
|
|
|
use RuntimeException; |
18
|
|
|
use Gears\String\Str; |
19
|
|
|
use Gears\Asset\Contracts\Compiler; |
20
|
|
|
use Gears\Asset\Contracts\Minifier; |
21
|
|
|
|
22
|
|
|
class Base implements Compiler |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* We represent the source file that needs to be compiled. |
26
|
|
|
* |
27
|
|
|
* @var SplFileInfo |
28
|
|
|
*/ |
29
|
|
|
protected $file; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* We represent the final destination asset file that will be created. |
33
|
|
|
* |
34
|
|
|
* @var SplFileInfo |
35
|
|
|
*/ |
36
|
|
|
protected $destination; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* If the $file is indeed a file we will read it's contents into this |
40
|
|
|
* property. Remember it's possible that we are given a folder... |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $source = ''; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* This basically tells us if we are allowed to minify the compiled source. |
48
|
|
|
* |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
protected $debug; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Only applies when building css assets. |
55
|
|
|
* Used in conjuction with: ```vladkens/autoprefixer-php```. |
56
|
|
|
* |
57
|
|
|
* @var bool |
58
|
|
|
*/ |
59
|
|
|
protected $autoprefix; |
60
|
|
|
|
61
|
|
|
public function __construct(SplFileInfo $file, SplFileInfo $destination, bool $debug, bool $autoprefix) |
62
|
|
|
{ |
63
|
|
|
$this->file = $file; |
64
|
|
|
|
65
|
|
|
if (!$this->file->isDir()) |
66
|
|
|
{ |
67
|
|
|
$this->source = file_get_contents($this->file->getPathname()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->destination = $destination; |
71
|
|
|
|
72
|
|
|
$this->debug = $debug; |
73
|
|
|
|
74
|
|
|
$this->autoprefix = $autoprefix; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritDoc |
79
|
|
|
* |
80
|
|
|
* This implementation caters for both standard native Css and Js files |
81
|
|
|
* that don't need any compiling as such. The less and sass compilers |
82
|
|
|
* extend the css compiler. |
83
|
|
|
*/ |
84
|
|
|
public function compile(): string |
85
|
|
|
{ |
86
|
|
|
if ($this->doWeNeedToMinify($this->file)) |
87
|
|
|
{ |
88
|
|
|
$src = $this->getMinifier($this->file, $this->source)->minify(); |
89
|
|
|
|
90
|
|
|
// Remove any source mappings, they cause 404 errors. |
91
|
|
|
// One of the benefits of using this Robo Task is that it is super |
92
|
|
|
// easy to switch between a minifed asset and a non minified asset. |
93
|
|
|
$src = preg_replace('/^\/\/# sourceMappingURL.*$/m', '', $src); |
94
|
|
|
|
95
|
|
|
// TODO: generate our own source maps... sounds like a challenge :) |
|
|
|
|
96
|
|
|
} |
97
|
|
|
else |
98
|
|
|
{ |
99
|
|
|
$src = $this->source; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $src."\n\n"; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Creates the minfier object. |
107
|
|
|
* |
108
|
|
|
* @param SplFileInfo $file The original source file. |
109
|
|
|
* @param string $source The source code to minify. |
110
|
|
|
* @return Minifier A minifier for the given destination type. |
111
|
|
|
*/ |
112
|
|
|
protected function getMinifier(SplFileInfo $file, string $source): Minifier |
113
|
|
|
{ |
114
|
|
|
$minifier = '\Gears\Asset\Minifiers\\'; |
115
|
|
|
$minifier .= ucfirst($this->destination->getExtension()); |
116
|
|
|
|
117
|
|
|
if (!class_exists($minifier)) |
118
|
|
|
{ |
119
|
|
|
throw new RuntimeException |
120
|
|
|
( |
121
|
|
|
'Minification is not supported for type: '. |
122
|
|
|
$this->destination->getExtension() |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return new $minifier($file, $source); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Based on if we are in debug mode and if the file is already minfied or |
131
|
|
|
* not, this tells us if we actually need to perform any minification. |
132
|
|
|
* |
133
|
|
|
* @param SplFileInfo $file The original source file. |
134
|
|
|
* @return bool If true we will run the minifier. |
135
|
|
|
*/ |
136
|
|
|
protected function doWeNeedToMinify(SplFileInfo $file): bool |
137
|
|
|
{ |
138
|
|
|
return |
139
|
|
|
( |
140
|
|
|
!$this->debug && |
141
|
|
|
!Str::s($file->getFilename())->contains('.min.') |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|