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