1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Makehappen\AutoMinifier; |
4
|
|
|
|
5
|
|
|
use MatthiasMullie\Minify as Minifier; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Minify |
9
|
|
|
* |
10
|
|
|
* Automatically Minify and Concatenate your JS and CSS files and libraries into single files |
11
|
|
|
* for improved application performance. |
12
|
|
|
* |
13
|
|
|
* @package Makehappen\AutoMinifier |
14
|
|
|
*/ |
15
|
|
|
class Minify |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Environment |
19
|
|
|
*/ |
20
|
|
|
protected $objEnvironment; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Cache bust file path |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $strCacheBustFile; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Public folder path |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $strPublicFolderPath; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* File types supported |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $arrFileTypes; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Destination file extension |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $strDestinationExtension; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Destination folder |
52
|
|
|
* |
53
|
|
|
* @var |
54
|
|
|
*/ |
55
|
|
|
protected $strDestinationFolder; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Destination file |
59
|
|
|
* |
60
|
|
|
* @var |
61
|
|
|
*/ |
62
|
|
|
protected $strDestinationFile; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set env to dev |
66
|
|
|
* |
67
|
|
|
* @var |
68
|
|
|
*/ |
69
|
|
|
protected $blnIsDev; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Create a new Minifier Instance |
73
|
|
|
* |
74
|
|
|
* Minify constructor. |
75
|
|
|
*/ |
76
|
|
|
public function __construct() |
77
|
|
|
{ |
78
|
|
|
// set class variables |
79
|
|
|
$this->init(); |
80
|
|
|
|
81
|
|
|
// set environment |
82
|
|
|
$this->objEnvironment = new Environment(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set Class Variables |
88
|
|
|
* |
89
|
|
|
* @return null |
90
|
|
|
*/ |
91
|
|
|
public function init() |
92
|
|
|
{ |
93
|
|
|
// set public folder location |
94
|
|
|
$this->setPublicFolder(); |
95
|
|
|
|
96
|
|
|
// set file types we want minified |
97
|
|
|
$this->arrFileTypes = [ |
98
|
|
|
'js' => ['js'], |
99
|
|
|
'css' => ['css', 'sass', 'scss'] |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
return null; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Build minified file |
107
|
|
|
* |
108
|
|
|
* @var $strFolder string |
109
|
|
|
* @var $strFile string |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function js($strFolder = '/js', $strFile = 'app.min.js') |
113
|
|
|
{ |
114
|
|
|
return $this->getMinifiedFile($strType = 'js', $strFolder, $strFile); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Build CSS minified file |
119
|
|
|
* |
120
|
|
|
* @var $strFolder string |
121
|
|
|
* @var $strFile string |
122
|
|
|
* @return bool|null|string |
123
|
|
|
*/ |
124
|
|
|
public function css($strFolder = '/css', $strFile = 'app.min.css') |
125
|
|
|
{ |
126
|
|
|
return $this->getMinifiedFile($strType = 'css', $strFolder, $strFile); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Generate minified file |
131
|
|
|
* |
132
|
|
|
* @param string $strType |
133
|
|
|
* @param string $strFolder |
134
|
|
|
* @param string $strFile |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
public function getMinifiedFile($strType = 'css', $strFolder = '/css', $strFile = 'app.min.css') |
138
|
|
|
{ |
139
|
|
|
$this->setDestinationExtensionType($strType); |
140
|
|
|
$this->setDestinationFolder($strFolder); |
141
|
|
|
$this->setDestinationFile($strFile); |
142
|
|
|
$this->setCacheBustFile(); |
143
|
|
|
return $strFolder . '/' . $strFile .'?' . $this->process(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Process Built |
148
|
|
|
* |
149
|
|
|
* @return bool|null|string |
150
|
|
|
*/ |
151
|
|
|
public function process() |
152
|
|
|
{ |
153
|
|
|
// return last cache bust in non development environments |
154
|
|
|
if (!$this->objEnvironment->isDevEnv()) { |
155
|
|
|
return $this->getCacheBust(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// build application file |
159
|
|
|
$strApplicationFileContents = $this->build(); |
160
|
|
|
|
161
|
|
|
// save application file |
162
|
|
|
file_put_contents($this->getAppFileName(), $strApplicationFileContents); |
163
|
|
|
|
164
|
|
|
// save new cache bust |
165
|
|
|
return $this->saveCacheBust($strApplicationFileContents); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Build file |
170
|
|
|
* |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
|
|
public function build() |
174
|
|
|
{ |
175
|
|
|
// files folder |
176
|
|
|
$strFilesFolder = $this->getFilesFolder(); |
177
|
|
|
|
178
|
|
|
// abort if folder not found |
179
|
|
|
if (!is_dir($strFilesFolder)) { |
180
|
|
|
return $strFilesFolder . ' folder not found'; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
// get all files |
184
|
|
|
$arrFiles = scandir($strFilesFolder); |
185
|
|
|
|
186
|
|
|
// loop through all files |
187
|
|
|
$strMinifiedFileContents = ''; |
188
|
|
|
foreach ($arrFiles as $strFileName) { |
189
|
|
|
// get minified content |
190
|
|
|
$strFileContents = $this->getMinifiedContent($strFileName); |
191
|
|
|
|
192
|
|
|
// don't include empty files |
193
|
|
|
if (!$strFileContents) { |
194
|
|
|
continue; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
// add new minified file to concatenated version |
198
|
|
|
$strMinifiedFileContents .= "\n/* $strFileName */\n" . $strFileContents; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
// returned concatenated version of minifired files |
202
|
|
|
return $strMinifiedFileContents; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Get minified Content |
207
|
|
|
* |
208
|
|
|
* @param $strFileName |
209
|
|
|
* @return bool|string |
210
|
|
|
*/ |
211
|
|
|
public function getMinifiedContent($strFileName) |
212
|
|
|
{ |
213
|
|
|
// get file extension |
214
|
|
|
$arrFileName = explode('.', $strFileName); |
215
|
|
|
$strFileExtension = array_pop($arrFileName); |
216
|
|
|
|
217
|
|
|
// must be a listed file type |
218
|
|
|
if (!in_array($strFileExtension, $this->arrFileTypes[$this->getDestinationExtension()])) { |
219
|
|
|
return ''; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
// must not be the app file |
223
|
|
|
if ($strFileName == $this->getDestinationFile()) { |
224
|
|
|
return ''; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
// build file path and name |
228
|
|
|
$strFile = $this->getFilesFolder() . '/' . $strFileName; |
229
|
|
|
|
230
|
|
|
// if it's minified already return content |
231
|
|
|
if (preg_match('/\.min\./', $strFile)) { |
232
|
|
|
return file_get_contents($strFile); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
// return minified content |
236
|
|
|
return $this->minifyContent($strFile); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Minify content |
241
|
|
|
* |
242
|
|
|
* @param $strFile |
243
|
|
|
* @return bool|string |
244
|
|
|
*/ |
245
|
|
|
public function minifyContent($strFile) |
246
|
|
|
{ |
247
|
|
|
// minify based on file type |
248
|
|
|
switch ($this->getDestinationExtension()) { |
249
|
|
|
case 'js': |
250
|
|
|
return (new Minifier\JS($strFile))->minify(); |
251
|
|
|
case 'css': |
252
|
|
|
return (new Minifier\CSS($strFile))->minify(); |
253
|
|
|
default: |
254
|
|
|
return ''; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Save Cache Bust |
260
|
|
|
* |
261
|
|
|
* @var string $strApplicationFileContents |
262
|
|
|
* @return null |
263
|
|
|
*/ |
264
|
|
|
public function saveCacheBust($strApplicationFileContents = '') |
265
|
|
|
{ |
266
|
|
|
// get contents signature |
267
|
|
|
$strNewCacheBust = md5($strApplicationFileContents); |
268
|
|
|
|
269
|
|
|
// get prior cache bust |
270
|
|
|
$strPriorCacheBust = $this->getCacheBust(); |
271
|
|
|
|
272
|
|
|
// if unchanged, stop here |
273
|
|
|
if ($strPriorCacheBust == $strNewCacheBust) { |
274
|
|
|
return $strPriorCacheBust; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
// set new cache bust |
278
|
|
|
file_put_contents($this->strCacheBustFile, $strNewCacheBust); |
279
|
|
|
|
280
|
|
|
// return new cache bust |
281
|
|
|
return $strNewCacheBust; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Get Cache Bust |
286
|
|
|
* |
287
|
|
|
* @return bool|string |
288
|
|
|
*/ |
289
|
|
|
public function getCacheBust() |
290
|
|
|
{ |
291
|
|
|
// abort if file not found |
292
|
|
|
if (!file_exists($this->strCacheBustFile)) { |
293
|
|
|
return ''; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
// get cache bust file contents |
297
|
|
|
return file_get_contents($this->strCacheBustFile); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Set env to Dev |
302
|
|
|
* |
303
|
|
|
* @param bool $bln |
304
|
|
|
* @return $this |
305
|
|
|
*/ |
306
|
|
|
public function setDev($bln = true) |
307
|
|
|
{ |
308
|
|
|
$this->objEnvironment->setDev($bln); |
309
|
|
|
$this->blnIsDev = $bln; |
310
|
|
|
return $this; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Set Cache bust file |
315
|
|
|
* |
316
|
|
|
* @param string $strFile |
317
|
|
|
* @return $this |
318
|
|
|
*/ |
319
|
|
|
public function setCacheBustFile($strFile = 'autominifier-cache-bust.txt') |
320
|
|
|
{ |
321
|
|
|
$this->strCacheBustFile = $this->getFilesFolder() . "/$strFile"; |
322
|
|
|
return $this; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Set public folder |
327
|
|
|
* |
328
|
|
|
* @param string $strFolder |
329
|
|
|
* @return $this |
330
|
|
|
*/ |
331
|
|
|
public function setPublicFolder($strFolder = '/../../../../public') |
332
|
|
|
{ |
333
|
|
|
// set application public path relative to package location |
334
|
|
|
$this->strPublicFolderPath = __DIR__ . $strFolder; |
335
|
|
|
return $this; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Set test folder |
340
|
|
|
* |
341
|
|
|
* @return $this |
342
|
|
|
*/ |
343
|
|
|
public function setTest() |
344
|
|
|
{ |
345
|
|
|
// set application public path relative to package location |
346
|
|
|
$this->setPublicFolder('/../build'); |
347
|
|
|
|
348
|
|
|
// cache bust file |
349
|
|
|
$this->setCacheBustFile(); |
350
|
|
|
|
351
|
|
|
return $this; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Set destination folder |
356
|
|
|
* |
357
|
|
|
* @param $strFolder |
358
|
|
|
* @return $this |
359
|
|
|
*/ |
360
|
|
|
public function setDestinationFolder($strFolder) |
361
|
|
|
{ |
362
|
|
|
$this->strDestinationFolder = $strFolder; |
363
|
|
|
return $this; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Set destination file |
368
|
|
|
* |
369
|
|
|
* @param $strFile |
370
|
|
|
* @return $this |
371
|
|
|
*/ |
372
|
|
|
public function setDestinationFile($strFile) |
373
|
|
|
{ |
374
|
|
|
$this->strDestinationFile = $strFile; |
375
|
|
|
return $this; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Set destination file extension |
380
|
|
|
* |
381
|
|
|
* @param $strDestinationExtension |
382
|
|
|
*/ |
383
|
|
|
public function setDestinationExtensionType($strDestinationExtension) |
384
|
|
|
{ |
385
|
|
|
$this->strDestinationExtension = $strDestinationExtension; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Get Public folder |
390
|
|
|
* |
391
|
|
|
* @return string |
392
|
|
|
*/ |
393
|
|
|
public function getPublicFolder() |
394
|
|
|
{ |
395
|
|
|
return $this->strPublicFolderPath; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Get destination folder |
400
|
|
|
* |
401
|
|
|
* @return mixed |
402
|
|
|
*/ |
403
|
|
|
public function getDestinationFolder() |
404
|
|
|
{ |
405
|
|
|
return $this->strDestinationFolder; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Set destination file |
410
|
|
|
* |
411
|
|
|
* @return mixed |
412
|
|
|
*/ |
413
|
|
|
public function getDestinationFile() |
414
|
|
|
{ |
415
|
|
|
return $this->strDestinationFile; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Get application file name |
420
|
|
|
* |
421
|
|
|
* @return string |
422
|
|
|
*/ |
423
|
|
|
public function getAppFileName() |
424
|
|
|
{ |
425
|
|
|
return $this->getFilesFolder() . '/' . $this->getDestinationFile(); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* Get destination file type extension |
430
|
|
|
* |
431
|
|
|
* @return mixed |
432
|
|
|
*/ |
433
|
|
|
public function getDestinationExtension() |
434
|
|
|
{ |
435
|
|
|
return $this->strDestinationExtension; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* Get files folder |
440
|
|
|
* |
441
|
|
|
* @return string |
442
|
|
|
*/ |
443
|
|
|
public function getFilesFolder() |
444
|
|
|
{ |
445
|
|
|
return $this->getPublicFolder() . '/' . $this->getDestinationFolder(); |
446
|
|
|
} |
447
|
|
|
} |
448
|
|
|
|